Declared Temporary Tables Pam Odden
Objectives <ul><li>How to declare a temporary table </li></ul><ul><li>Tips on using a temporary table </li></ul><ul><li>Du...
Declared Temporary Tables <ul><li>When you need a table only for the life of an application process, you can create a temp...
Declaring Temporary Tables <ul><li>An instance of a declared temporary table is created using the SQL statement DECLARE GL...
Declaring Temporary Tables, cont. <ul><li>Temporary tables can be declared in the following ways: </li></ul><ul><ul><li>Sp...
Declaring Temporary Tables, cont. <ul><ul><li>Use a LIKE clause to copy the definition of a base table, created temporary ...
Declaring Temporary Tables, cont. <ul><li>Use a select statement to choose specific columns from a base table, created tem...
Using Temporary Tables <ul><li>When DB2 executes the DECLARE GLOBAL TEMPORARY TABLE statement, DB2 creates an empty instan...
Using Temporary Tables, cont. <ul><li>When a COMMIT statement is executed in an application with a declared temporary tabl...
Identity Columns <ul><li>Provides a generated key for a table  </li></ul><ul><li>A table can have only one column specifie...
Identity Columns Optional Clauses <ul><li>START WITH  numeric-constant  – specifies the first value.  Default is MINVALUE ...
Identity Columns Optional Clauses, cont. <ul><li>CYCLE OR NO CYCLE – specifies whether values will continue to be generate...
Summary <ul><li>Use  DECLARE GLOBAL TEMPORARY TABLE  to create a temporary table that will exist for the duration of a sin...
Upcoming SlideShare
Loading in …5
×

Declared Temporary Tables

901 views
751 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
901
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • No one has used global temp tables in v5. If info fits in an internal cobol table, these may not be needed. Might be handy if sql would be helpful in accessing tabled data. Maybe using an order by would avoid an internal sort, or writing 2 programs passing a data file. Temporary tables can also return result sets from stored procedures.
  • Jan wouldn’t have to worry about whether someone else on her team was declaring a table with the same name.
  • Can use whatever name you want.
  • This would be a lot easier, if you are declaring the temp table like one you already have. We can experiment to see whether doing this would speed up processing if we are using a small subset of a large table. To not use identity col attributes, you would just get an integer, or whatever numeric type it is. Can use defaults with not null.
  • I think you could name just the cols you want from the template table. Doco didn’t exactly say that. Didn’t mention using WHERE clause either.
  • Qualifier SESSION is used as we would use the db name, ie. MSPAYDB1 If you need help with creating indexes, see Kathy or myself .
  • We don’t usually put commit points in our batch processes (although we might do this in the future), so not a problem. But consider the effect in an online pgm that commits during each iteration of the program, and use ON COMMIT PRESERVE ROWS, if necessary. When the program ends, DB2 drops TEMPPROD
  • This is a way of letting DB2 create a unique key for you. Such as adding orders to an order table and you want an order number that starts with a particular value and increases by one. Saves contention of many applications trying to find out what the next order number is.
  • Book did not say an error would occur when NO CYCle runs out of values. However, the identity column cannot be null, so I think there would be an error.
  • Declared Temporary Tables

    1. 1. Declared Temporary Tables Pam Odden
    2. 2. Objectives <ul><li>How to declare a temporary table </li></ul><ul><li>Tips on using a temporary table </li></ul><ul><li>Duration of a temporary table </li></ul><ul><li>Identity columns </li></ul>
    3. 3. Declared Temporary Tables <ul><li>When you need a table only for the life of an application process, you can create a temporary table. </li></ul><ul><li>There are two kinds of temporary tables: </li></ul><ul><ul><li>Created temporary tables, which you define using a CREATE GLOBAL TEMPORARY TABLE statement (We looked at these when we went to version 5. They are created in advance of being used in a program.) </li></ul></ul><ul><ul><li>Declared temporary tables, which you define using a DECLARE GLOBAL TEMPORARY TABLE statement Available in version 7 and created “on the fly”. </li></ul></ul><ul><li>SQL statements that use temporary tables can run faster because: </li></ul><ul><ul><li>DB2 does little or no logging or locking for temporary tables </li></ul></ul>
    4. 4. Declaring Temporary Tables <ul><li>An instance of a declared temporary table is created using the SQL statement DECLARE GLOBAL TEMPORARY TABLE. </li></ul><ul><li>That instance is known only to the application process in which the table is declared, so temporary tables with the same name may be declared in different applications. </li></ul><ul><li>Before a declared temporary table can be defined, a special database and table spaces must be created for them. </li></ul><ul><li>The DBA does that by executing the CREATE DATABASE statement with the AS TEMP clause, and then creating segmented table spaces in that database. A DB2 subsystem can have only one database for declared temporary tables, but that database can contain more than one table space. </li></ul><ul><li>When you declare and use a temporary table, the system decides which TEMP table space to use. </li></ul>
    5. 5. Declaring Temporary Tables, cont. <ul><li>Temporary tables can be declared in the following ways: </li></ul><ul><ul><li>Specify all the columns in the table. </li></ul></ul><ul><ul><ul><ul><li>DECLARE GLOBAL TEMPORARY TABLE TEMPPROD </li></ul></ul></ul></ul><ul><ul><ul><ul><li>(SERIAL CHAR(8) NOT NULL WITH DEFAULT '99999999', </li></ul></ul></ul></ul><ul><ul><ul><ul><li>DESCRIPTION VARCHAR(60) NOT NULL, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>PRODCOUNT INTEGER GENERATED ALWAYS AS IDENTITY, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>MFGCOST DECIMAL(8,2), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>MFGDEPT CHAR(3), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>MARKUP SMALLINT, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>SALESDEPT CHAR(3), </li></ul></ul></ul></ul><ul><ul><ul><ul><li>CURDATE DATE NOT NULL); </li></ul></ul></ul></ul><ul><ul><li>WITH DEFAULT clause may be used </li></ul></ul><ul><ul><li>Identity columns may be defined (more about identity columns on a later screen) </li></ul></ul>
    6. 6. Declaring Temporary Tables, cont. <ul><ul><li>Use a LIKE clause to copy the definition of a base table, created temporary table, or view. </li></ul></ul><ul><ul><ul><ul><li>DECLARE GLOBAL TEMPORARY TABLE TEMPPROD LIKE BASEPROD </li></ul></ul></ul></ul><ul><ul><ul><ul><li>INCLUDING IDENTITY COLUMN ATTRIBUTES; </li></ul></ul></ul></ul><ul><ul><li>If the table from which you select columns has identity columns, you can specify that the corresponding columns in the declared temporary table are also identity columns by specifying the INCLUDING IDENTITY COLUMN ATTRIBUTES clause </li></ul></ul><ul><ul><li>If you want the declared temporary table columns to inherit the column defaults, specify the INCLUDING COLUMN DEFAULTS clause. </li></ul></ul><ul><ul><li>If you want the declared temporary table columns to have default values that correspond to their data types, specify the USING TYPE DEFAULTS clause. </li></ul></ul>
    7. 7. Declaring Temporary Tables, cont. <ul><li>Use a select statement to choose specific columns from a base table, created temporary table, or view. </li></ul><ul><ul><ul><li>DECLARE GLOBAL TEMPORARY TABLE TEMPPROD AS </li></ul></ul></ul><ul><ul><ul><li>(SELECT * FROM PRODVIEW) </li></ul></ul></ul><ul><ul><ul><li>DEFINITION ONLY </li></ul></ul></ul><ul><ul><ul><li>INCLUDING IDENTITY COLUMN ATTRIBUTES </li></ul></ul></ul><ul><ul><ul><li>INCLUDING COLUMN DEFAULTS; </li></ul></ul></ul><ul><ul><li>Definition only clause is required. </li></ul></ul>
    8. 8. Using Temporary Tables <ul><li>When DB2 executes the DECLARE GLOBAL TEMPORARY TABLE statement, DB2 creates an empty instance of the temporary table. </li></ul><ul><li>You can populate the declared temporary table using INSERT statements, modify the table using searched or positioned UPDATE or DELETE statements, and query the table using SELECT statements. </li></ul><ul><li>You can also create indexes on the declared temporary table. </li></ul><ul><li>The qualifier, SESSION, must be specified in any statement that references the table </li></ul><ul><ul><ul><li>EXEC SQL INSERT INTO SESSION.TEMPPROD . . . </li></ul></ul></ul>
    9. 9. Using Temporary Tables, cont. <ul><li>When a COMMIT statement is executed in an application with a declared temporary table, DB2 deletes all the rows from the table or keeps the rows, depending on the ON COMMIT clause that you specify in the DECLARE statement. </li></ul><ul><ul><li>ON COMMIT DELETE ROWS, which is the default, causes all rows to be deleted from the table at a commit point, unless there is a held cursor open on the table at the commit point. </li></ul></ul><ul><ul><li>ON COMMIT PRESERVE ROWS causes the rows to remain past the commit point. </li></ul></ul><ul><li>The definition of the declared temporary table exists as long as the application process runs. </li></ul><ul><li>If you need to delete the definition before the application process completes, you can do so with the DROP TABLE statement. </li></ul><ul><ul><ul><li>DROP TABLE SESSION.TEMPPROD; </li></ul></ul></ul>
    10. 10. Identity Columns <ul><li>Provides a generated key for a table </li></ul><ul><li>A table can have only one column specified AS IDENTITY </li></ul><ul><li>Data type for the column must be an exact numeric type (INTEGER, SMALLINT, DECIMAL with a scale of zero) </li></ul><ul><li>An Identity Column is implicitly NOT NULL </li></ul><ul><li>An Identity Column may be specified as GENERATED ALWAYS or GENERATED BY DEFAULT. ALWAYS is recommended unless you are using data propagation. </li></ul>
    11. 11. Identity Columns Optional Clauses <ul><li>START WITH numeric-constant – specifies the first value. Default is MINVALUE for an ascending sequence and MAXVALUE for a descending sequence. </li></ul><ul><li>INCREMENT BY numeric-constant – specifies the interval between numbers. If positive, sequence is ascending, if negative, sequence is descending. Default is 1. </li></ul><ul><li>MAXVALUE numeric-constant – specifies the maximum value to be generated. Can be positive or negative, but must be larger than MINVALUE. Default is the largest value for the data type for an ascending sequence. For a descending sequence, default is START WITH if specified, or –1 if no START WITH is specified. </li></ul><ul><li>MINVALUE numeric-constant – any value less than MAXVALUE that is legal for the data type. Default is START WITH or 1 (if no START WITH) for ascending sequence, and MINVALUE for a descending sequence. </li></ul>
    12. 12. Identity Columns Optional Clauses, cont. <ul><li>CYCLE OR NO CYCLE – specifies whether values will continue to be generated once the MAXVALUE or MINVALUE has been reached </li></ul><ul><ul><li>CYCLE reuses the range from MINVALUE to MAXVALUE </li></ul></ul><ul><ul><li>This can create duplicate values for the identity column, and will cause an error if there is a unique key on the column </li></ul></ul><ul><ul><li>NO CYCLE stops generating values for the identity column once the MAXVALUE or MINVALUE has been reached. This is the default. </li></ul></ul><ul><li>CACHE OR NO CACHE – specifies whether to keep some preallocated values in memory. Preallocating and storing values in the cache improves the performance of inserting rows into the table. </li></ul>
    13. 13. Summary <ul><li>Use DECLARE GLOBAL TEMPORARY TABLE to create a temporary table that will exist for the duration of a single process </li></ul><ul><li>A LIKE clause or SELECT statement can make defining the temporary table easier by using the format of an existing table or view </li></ul><ul><li>Issue SQL statements against the temp table as you would any other table, using the qualifier SESSION . </li></ul><ul><li>Use ON COMMIT PRESERVE ROWS to keep the contents of your temporary table after a COMMIT statement </li></ul>

    ×