You are on page 1of 1

How To Create a Temporary/Session Variable in Teradata.

Written by admin in Friday, June 22nd 2007 under IT


3 Comments received

There is no direct answer to this question as this concept is present and used o
nly in Stored Procedures and Macros in Teradata. But there are ways to replicate
this functionality in the command prompt.
As said before create a macro and pass a variable. Remember you can not use dyna
mic SQLs in a Macro. Also you can create a volatile (temporary) table which live
s until the session is active, so here is the code on how to create a volatile t
able. Suppose you want to hold todays date in a temp variable/table here is the w
ay to do it.
CREATE VOLATILE TABLE today_date AS
(SELECT DATE) WITH DATA ON COMMIT PRESERVE ROWS;
In case you do not want to see the results after you run the query, then use DEL
ETE in place of PRESERVE which deletes the row as soon as the query finishes. Th
is is helpful if you are doing any transactional query.
If you are wondering how to create macro, here is the way.
CREATE MACRO macro_name (<param1> datatype, <param2> datatype) AS
([INSERT;][UPDATE;][DELETE;][SELECT;] );
For example;
CREATE MACRO my_macro( InVal INTEGER)
( Update mytable set mycolumn = mycolumn *5/100
WHERE myothercolumn = :InVal;
SELECT mycolumn, myothercolumn
From myothertable
WHERE myothercolumn = :InVal);

You might also like