declaration of variable in teradata

拟墨画扇 提交于 2019-12-11 04:30:10

问题


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

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