am creating a GLOBAL TEMPORARY TABLE
in DB2. and when i surfed i got a two way to create
1. Declare
2. Create.
1. DECLARE GLOBAL TEMPORARY TABLE SESSION.TEMP_EMP
(EMPNO CHAR(6) NOT NULL,
SALARY DECIMAL(9, 2),
BONUS DECIMAL(9, 2),
COMM DECIMAL(9, 2)) WITH REPLACE ON COMMIT PRESERVE ROWS ;
2. CREATE GLOBAL TEMPORARY TABLE TMPDEPT
(TMPDEPTNO CHAR(3) NOT NULL,
TMPDEPTNAME VARCHAR(36) NOT NULL,
TMPMGRNO CHAR(6),
TMPLOCATION CHAR(16) ) ON COMMIT PRESERVE ROWS ;
and from IBM site i got a info that create is the best since its being persistent , allowing all user sessions to access the same table definition without having to declare it at startup and many more advantages.
Link : http://www.ibm.com/developerworks/data/library/techarticle/dm-0912globaltemptable/
and i had few queries in using create over declare:
I couldn't find the Replace
keyword while using CREATE GLOBAL TEMPORARY TABLE
.
consider one scenario,
am opening a connection and executing a Stored Procedure,
within that Stored Procedure am creating Global temp table
and with in that Stored Procedure am calling Another Stored Procedure
which again have same
Create Temp table statement .. what will happen in this case..
does it throw any error since both table naes are same and within the single connection?
Declare have session and create doesn't have?? does this related to persistant??
in performace wise which is better? Declare temp or create temp?
Suggest some scenarioes for the best usage of declare / create !!
There are two varieties of temp tables. Local temp tables are only accessible from their creation context, such as the connection. Global temp tables are accessible from other connection contexts. Both local and global temp tables reside in the tempdb database.
Creation of Global Temporary TablesThe data in a global temporary table is private, such that data inserted by a session can only be accessed by that session. The session-specific rows in a global temporary table can be preserved for the whole session, or just for the current transaction.
The CREATE GLOBAL TEMPORARY TABLE statement creates a description of a temporary table at the current server. Each session that selects from a created temporary table retrieves only rows that the same session has inserted. When the session terminates, the rows of the table associated with the session are deleted.
There are 2 types of Temporary Tables: Local Temporary Table, and Global Temporary Table.
Craig S. Mullins article applies to DB2 for OS/390 platform. In DB2 9.7 you actually can create indexes for both DGTT and CGTT. Also you can enable logging in CGTT. For 9.7 read here: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.7.0/com.ibm.db2.luw.admin.dbobj.doc/doc/r0054491.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With