如何在Teradata SQL宏中使用变量

时间:2010-04-22 02:11:16

标签: sql stored-procedures macros teradata

我想在Teradata上的宏SQL中使用变量。

我以为我可以做以下事情:

REPLACE MACRO DbName.MyMacro  
(  
   MacroNm   VARCHAR(50)  
)  
AS  
(  

   /* Variable to store last time the macro was run */  

   DECLARE V_LAST_RUN_DATE TIMESTAMP;  


   /* Get last run date and store in V_LAST_RUN_DATE */  

   SELECT LastDate  
   INTO V_LAST_RUN_DATE  
   FROM DbName.RunLog  
   WHERE MacroNm = :MacroNm;  


   /* Update the last run date to now and save the old date in history */  

   EXECUTE MACRO DbName.RunLogUpdater(  
      :MacroNm  
     ,V_LAST_RUN_DATE  
     ,CURRENT_TIMESTAMP  
   );  

);  

然而,这没有用,所以我想到了这一点:

REPLACE MACRO DbName.MyMacro  
(  
   MacroNm   VARCHAR(50)  
)  
AS  
(  

   /* Variable to store last time the macro was run */  

   CREATE VOLATILE TABLE MacroVars AS  
   (  
         SELECT  LastDate AS V_LAST_RUN_DATE  
           FROM  DbName.RunLog  
          WHERE  MacroNm = :MacroNm;  
   )  
   WITH DATA ON COMMIT PRESERVE ROWS;  


   /* Update the last run date to now and save the old date in history */  

   EXECUTE MACRO DbName.RunLogUpdater(  
      :MacroNm  
     ,SELECT V_LAST_RUN_DATE FROM MacroVars  
     ,CURRENT_TIMESTAMP  
   );  

);  

我可以使用存储过程执行我正在寻找的操作,但是我希望避免使用存储过程。

您对此有何看法?
还有什么我可以尝试的吗?

干杯

1 个答案:

答案 0 :(得分:1)

您无法在宏内部声明变量。如果您愿意,可以使用UPDATE语句处理您要完成的任务。

UPDATE TGT
FROM <dbname>.<target table> TGT
   , (SELECT MacroName
           , LastRunDate
      FROM <dname>.<source table>
     ) SRC
SET LastRunDate = SRC.LastRunDate
  , EffectiveTimestamp = CURRENT_TIMESTAMP(0)
WHERE TGT.MacroName = SRC.MacroName
;