10.12.2012 Views

Application Programming and SQL Guide - Kmlinux

Application Programming and SQL Guide - Kmlinux

Application Programming and SQL Guide - Kmlinux

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

|<br />

|<br />

Example: Suppose that you create a definition of TEMPPROD <strong>and</strong> then run an<br />

application that contains the following statements:<br />

EXEC <strong>SQL</strong> DECLARE C1 CURSOR FOR SELECT * FROM TEMPPROD;<br />

EXEC <strong>SQL</strong> INSERT INTO TEMPPROD SELECT * FROM PROD;<br />

EXEC<br />

.<br />

<strong>SQL</strong> OPEN C1;<br />

.<br />

EXEC<br />

.<br />

<strong>SQL</strong> COMMIT;<br />

.<br />

EXEC <strong>SQL</strong> CLOSE C1;<br />

When you run the INSERT statement, DB2 creates an instance of TEMPPROD <strong>and</strong><br />

populates that instance with rows from table PROD. When the COMMIT statement<br />

is run, DB2 deletes all rows from TEMPPROD. However, assume that you change<br />

the declaration of cursor C1 to the following declaration:<br />

EXEC <strong>SQL</strong> DECLARE C1 CURSOR WITH HOLD<br />

FOR SELECT * FROM TEMPPROD;<br />

In this case, DB2 does not delete the contents of TEMPPROD until the application<br />

ends because C1, a cursor defined WITH HOLD, is open when the COMMIT<br />

statement is run. In either case, DB2 drops the instance of TEMPPROD when the<br />

application ends.<br />

Working with declared temporary tables<br />

You create an instance of a declared temporary table using the <strong>SQL</strong> statement<br />

DECLARE GLOBAL TEMPORARY TABLE. That instance is known only to the<br />

application process in which the table is declared, so you can declare temporary<br />

tables with the same name in different applications. The qualifier for a declared<br />

temporary table is SESSION.<br />

Before you can define declared temporary tables, you must create a special<br />

database <strong>and</strong> table spaces for them. You do that by running the CREATE<br />

DATABASE statement with the AS TEMP clause, <strong>and</strong> then creating segmented<br />

table spaces in that database. A DB2 subsystem can have only one database for<br />

declared temporary tables, but that database can contain more than one table<br />

space. There must be at least one table space with a 8-KB page size in the TEMP<br />

database to declare a temporary table.<br />

Example: The following statements create a database <strong>and</strong> table space for declared<br />

temporary tables:<br />

CREATE DATABASE DTTDB AS TEMP;<br />

CREATE TABLESPACE DTTTS IN DTTDB<br />

SEGSIZE 4;<br />

You can define a declared temporary table in any of the following ways:<br />

v Specify all the columns in the table.<br />

Unlike columns of created temporary tables, columns of declared temporary<br />

tables can include the WITH DEFAULT clause.<br />

v Use a LIKE clause to copy the definition of a base table, created temporary<br />

table, or view.<br />

If the base table or created temporary table that you copy has identity columns,<br />

you can specify that the corresponding columns in the declared temporary table<br />

are also identity columns. Do that by specifying the INCLUDING IDENTITY<br />

COLUMN ATTRIBUTES clause when you define the declared temporary table.<br />

v Use a fullselect to choose specific columns from a base table, created temporary<br />

table, or view.<br />

Chapter 2. Working with tables <strong>and</strong> modifying data 23

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!