Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DECLARE GLOBAL TEMPORARY TABLE Vs CREATE GLOBAL TEMPORARY TABLE in DB2

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:

  1. I couldn't find the Replace keyword while using CREATE GLOBAL TEMPORARY TABLE .

  2. 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?

  3. Declare have session and create doesn't have?? does this related to persistant??

  4. in performace wise which is better? Declare temp or create temp?

  5. Suggest some scenarioes for the best usage of declare / create !!

like image 216
A Programmer Avatar asked Mar 29 '13 06:03

A Programmer


People also ask

What is the difference between temp table and global temp table?

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.

What is create global temporary table?

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.

What is use of global temporary table in DB2?

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.

What are the two main types of temporary tables?

There are 2 types of Temporary Tables: Local Temporary Table, and Global Temporary Table.


1 Answers

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

like image 183
Vika A. Avatar answered Sep 20 '22 15:09

Vika A.