2010-04-22 113 views
0

我想在我的Teradata宏SQL中使用變量。如何在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 
    ); 

); 

我可以做什麼我尋找一個存儲過程,但我想避免性能。

你對此有什麼想法?
還有什麼我可以嘗試嗎?

乾杯

+0

出於好奇,存儲過程會導致哪些性能問題? – lins314159 2010-04-22 23:30:35

回答

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 
;