How to execute dynamic SQL in Teradata

夙愿已清 提交于 2019-11-28 14:08:36

You may find success putting this in a stored procedure using the DBC.SysExecSQL command.

Here is an overly simplified example of a stored procedure in Teradata. Obviously in production would want an error handler defined to address things like invalid database objects. Furthermore, you could return the SQLSTATE back as a parameter to test for whether the stored procedure completed successfully or not.

CREATE PROCEDURE SYSDBA.CommentDatabase
(
  IN P_Database VARCHAR(30),
  IN P_Comment VARCHAR(255), 
  OUT MSG
)
MAIN:  --Label
BEGIN

  DECLARE  P_SQL_TEXT     VARCHAR(4000);

  SET P_SQL_TEXT='COMMENT ON DATABASE '||P_DBNAME||' AS '''||P_COMMENT||'''';
  CALL dbc.SysExecSQL (:P_SQL_TEXT);

  SET MSG = 'Database '||P_DBNAME||' commented successfully!';
END;
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!