Jun 23, 2010 9:09 AM
How to return result set from a temporary table in a stored procedure
-
Like (0)
Hi All,
I Just now joined this community so first first a big Hello to all of you.
I have a quick query. As per my understanding a stored procedure in netezza can return a result set. My tried following steps
(1) Create a temprary table i.e abc
CREATE TEMPORARY TABLE abc (i VARCHAR(20), i2 VARCHAR(20));
(2) Create a stored procedure and write sql statement(inside stored procedure) to insert data into temporary table.
CREATE OR REPLACE PROCEDURE returntwo(timestamp) RETURNS REFTABLE(abc) LANGUAGE NZPLSQL AS
BEGIN_PROC
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO ' || REFTABLENAME ||' values (1,1)';
EXECUTE IMMEDIATE 'INSERT INTO ' || REFTABLENAME ||' values (2,2)';
RETURN REFTABLE;
END;
END_PROC;
(3) return result set from temporary table using return reftable command.
EXECUTE PROCEDURE returntwo(now());
When i tried to compile stored procedure, got following error. When i use a normal table instead of temporary table then stored procedure works fine but in case of stored procedure, it doesn't even compile. Pls help . I am using 5.0.8 version.
Error Code: 0, SQL State: ] ERROR: Expected a table for <DB Name>.<User Name>.abc
The store procedure needs the persistent reference table. As the temp tables are automatically get dropped after end of session, those are not allowed to use as reference table in store procedure for returning result set.
The error its giving for temp tables is not correct though.
-AmbarishDon
Hi,
Thanks for clarification, If temp table solution doesn't work then do you have any other alternatives? The reason to ask this because as per requirement , i need to do some complex calculation for each session and keep the data into a table which can be returned to report. If i use a temporary table then i need not to worry about
(1) keeping data based on session id in a normal table.
(2) Deleting data after session is over.
Any suggestions ?
The reference table used by procedure is only to know that design of result set (number and data type of columns). Therefore you must use the normal table. Store procedure does not store the data in the reference table; therefore you need not to bother about data in it. Procedure creates the temp table for every execution and drops it once result set it returned. In your scenario, you just need to have normal user table instead of temp table.
-AmbarishDon

