问题
Is there an equivalent of declaring a variable similar to this Sql Server (TSQL) code in teradata's sql dialect?
DECLARE @Somedate Date = GETUTCDATE();
SELECT TOP 10 * FROM SOMETABLE WHERE SOMEDATE = @Somedate;
回答1:
Yes and no. You cannot declare variables in a typical SQL Statement that you are running directly from your code editor. You can (as one example) declare variables inside of a stored procedure. In your code editor window, you would be more likely to create a volatile table and use it to store the value of your "variable".
回答2:
Is it possible to just use the following syntax on Teradata? The Current_Date
part can be replaced with any scalar query. This will work with Teradata SQL Assistant / BTEQ tool
SELECT TOP 10 * FROM SOMETABLE WHERE SOMEDATE = Current_Date;
SELECT TOP 10 * FROM SOMETABLE WHERE SOMEDATE = (SELECT MAX(SameDate) FROM SomeOtherTable);
One way to achieve this with date stored as some variable in stored procedures on Teradata is:
DECLARE v_SQL_Text VARCHAR(32000);
DECLARE v_Somedate Date;
SELECT Current_Date INTO :v_Somedate;
SET v_SQL_Text = 'SELECT TOP 10 * FROM SOMETABLE WHERE SOMEDATE = ' || v_Somedate || ';'
CALL DBC.SysExecSQL(v_SQL_Text);
来源:https://stackoverflow.com/questions/39697044/declaration-of-variable-in-teradata