Now that you're able to define and use the temporary table; you'll
discover that, as declared, you won't be able to use it as the target of
a select statement in a stored procedure. This is caused by the
requirement that the table exist at the time the stored procedure is
compiled. It can't exist before compilation because you are defining it
in the procedure.
The technique for doing this is to define a real table GLOBAL.TEMP in
the database before you define the stored procedure. The predefined
table must contain the same column names and type definitions that your
temporary table contains.
When the stored procedure is built; the predefined table is used by UDB
to determine the characteristics TEMP. When the stored procedure is
invoked, the declaration for TEMP replaces the reference to the
predefined table. This also means that if multiple connections to the
database are invoking the same stored procedure, each will get its own
"copy" of the TEMP table, independent of the others. This also means
that a predefined table containing a single integer column could be used
by multiple stored procedures for different purposes!
The declared table TEMP is not necessarily dropped when the stored
procedure terminates. Options available in the 'DECLARE GLOBAL TEMPORARY
TABLE' statement can assist controlling its persistance across stored
procedures within the same thread. One caution - if you have a
persistant temporary table and you try to DECLARE it again, the DECLARE
will fail. Again, there is an option to avoid this problem.
The writeup in the manual about global temporary tables is excellent and
should answer any additional questions that arise.
Phil Sherman
Thomasb wrote:
Thank you Philip. It worked.
Philip Sherman <ps******@ameritech.net> wrote in message news:<XG**********************@newssvr28.news.prod igy.com>...
Stored procedures require all definitions to be declared before
executable statements. The declaration for the global temporary table is
actually an executable statement because it creates the table.
Swap the order of the two DECLARE statements and try it again. It's
worked fine for me on V8.1.
Phil Sherman
Thomasb wrote:
With a background in MS SQL Server programming I'm used to temporary
tables. Have just started to work with DB2 ver 7 on z/OS and stumbled
into the concept of GLOBAL TEMPORARY TABLE.
I have created a temporary database with a tables space. Verified that
DECLARE GLOBAL TEMPORARY TABLE TEMP (A INTEGER);
INSERT INTO SESSION.TEMP VALUES(10);
SELECT A FROM SESSION.TEMP;
works from a query tool.
When I try to compile the following I get errors:
CREATE PROCEDURE TEST()
RESULT SETS 1
LANGUAGE SQL
COLLID TEST
WLM ENVIRONMENT WLM2
RUN OPTIONS ''
P1: BEGIN
DECLARE GLOBAL TEMPORARY TABLE TEMP (A INTEGER);
DECLARE SP_SQL1 CURSOR WITH RETURN FOR
SELECT A FROM SESSION.TEMP;
INSERT INTO SESSION.TEMP VALUES(10);
OPEN SP_SQL1;
END P1
And the errors are:
DSNH016I E DSNHPARS LINE 9 COL 9 "GLOBAL" REQUIRED
DSNH104I E DSNHPARS LINE 9 COL 9 ILLEGAL SYMBOL "SP_SQL1". SOME
SYMBOLS THAT MIGHT BE LEGAL ARE: GLOBAL
Anyone who can correct me on this one?
Cheers Thomas