How to execute dynamic SQL in Teradata

后端 未结 1 1468
感情败类
感情败类 2020-12-12 00:30

Is there any way to submit dynamically generated SQL to Teradata? I\'ve written a query that will create the code to denormalize a table. Right now, I am pulling the code

相关标签:
1条回答
  • 2020-12-12 01:13

    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_DATABASE||' AS '''||P_COMMENT||'''';
      CALL dbc.SysExecSQL (:P_SQL_TEXT);
    
      SET MSG = 'Database '||P_DBNAME||' commented successfully!';
    END;
    
    0 讨论(0)
提交回复
热议问题