I have a volatile table (say, vtTempTableForPI
) that I am creating inside a procedure. Once the volatile table gets created, I am inserting rows into it.
Once I have records in the volatile table, I need to take MAX(ModifiedDatetime)
from this volatile table (vtTempTableForPI
) into a local variable. However, I am unable to find a way to do so.
Here is a piece of code from my procedure for some context:
SELECT MAX(ModifiedDatetime)
FROM vtTempTableForPI
INTO lvMaxUpdateDateTime
WHERE Template = 'Schedule_Stream'
;
And then I want to use this local variable to insert into another volatile table, like below:
SET lvQuery = '
INSERT INTO vtScheduleVersionUpdatedTime
SELECT
''Schedule_Stream''
, ''' || CAST(lvMaxUpdateDateTime AS VARCHAR(19)) || '''
';
EXECUTE IMMEDIATE lvQuery;
I can't use this cursor on volatile table since its definition will not be present in DBC when we compile the procedure, so it will give an error. If I try to use dynamic query in order to avoid this error, I run into another error like below:
SET lvMaxModifiedDateQuery = '
SELECT MAX(ModifiedDatetime)
FROM vtTempTableForPI
WHERE Template = ''Schedule_Tank''
';
PREPARE stMaxModifiedDateQuery03 FROM lvMaxModifiedDateQuery;
OPEN crGetMaxModifiedDate03;
FETCH crGetMaxModifiedDate03 INTO lvMaxUpdateDateTime;
--CLOSE crGetMaxModifiedDate;
SET lvQuery = '
INSERT INTO vtScheduleVersionUpdatedTime
SELECT
''Schedule_Tank''
, ''' || CASt(lvMaxUpdateDateTime AS VARCHAR(19)) || '''
';
EXECUTE IMMEDIATE lvQuery;
Below is the error that I am getting:
Failure occured while Creating Dynamic Query
SQL State:T7688,
SQL Code:7688,
SQL SESSION: 252898254,
Execution Start Time:2019-01-13 21:44:44,
Execution End Time:2019-01-13 21:44:54,
ERROR Message: Error occurred generating Evl code for dynamic fetch.
Need help!
What's your Teradata release? This is working fine for me:
REPLACE PROCEDURE SP_test(
IN v_CALC_BASIS VARCHAR(100))
BEGIN
DECLARE lvMaxUpdateDateTime INT;
DECLARE v_LogStmt VARCHAR(5000);
SELECT Max(ModifiedDatetime)
FROM vtTempTableForPI
INTO lvMaxUpdateDateTime
WHERE Template = 'Schedule_Stream'
;
END;
CALL SP_test ('bla');
CALL Failed. [3807] SP_TEST:Object 'vtTempTableForPI' does not exist.
Either create the Volatile Table before you compile it or simply switch to a Global Temporary Table instead (recommended) .