Explicitly declares a local temporary table. The metadata for a declared local temporary table is not stored in the database and cannot be shared by other modules. These tables are sometimes called scratch tables. The data stored in the table cannot be shared between SQL sessions or modules in a single session. Unlike persistent base tables, the metadata and data do not persist beyond an SQL session. In addition to declared local temporary tables, there are two other types of temporary tables: o Global temporary tables o Local temporary tables See the CREATE TABLE statement for additional information on global and local temporary tables.
1 – Environment
You can use the DECLARE LOCAL TEMPORARY TABLE statement: o In interactive SQL o In dynamic SQL as a statement to be dynamically executed o In a stored module
2 – Format
(B)0[m[1;4mDECLARE[m[1m [1;4mLOCAL[m[1m [1;4mTEMPORARY[m[1m [1;4mTABLE[m[1m qwqqqqqqqqqqqqqqqqqw> [1;4mMODULE .[m[1m qk[m [1m mq> alias-name . qj x[m [1mlqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj[m [1mmq> <table-name> qq> dec-local-table-body qqqqqqqqqqqqqqqqqqqk [m [1mlqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1mmqqqwqqqqwqq>[m [1;4mCOMPRESSION[m[1m IS qwq>[m [1;4mENABLED[m [1mqqwqqqqqqqwqqqqqwqqqq>[m [1mx[m [1mx[m [1mmq>[m [1;4mDISABLED[m[1m qj[m [1mx[m [1mx[m [1mx[m [1mmqq> [1;4mON[m[1m [1;4mCOMMIT[m[1m qwq> [1;4mDELETE[m [1mqqqqwqq> [1;4mROWS[m[1m qqj[m [1mx[m [1m [m [1m x[m [1m [m [1m [m [1mmq> [1;4mPRESERVE[m [1mqqj [m [1m x [m [1mmqqqqqqqqqqqqqqqqqqqqqqqqqqq<qqqqqqqqqqqqqqqqqqqqqqqqqj[m [1m [m (B)0[m[1mdec_local_table_body [m [1m [m [1mqwq> (dec_local_col_list) [mqqqqq[1mqqqqqqqqqqqqqqqqqqqqqqqqqqqqwq>[m [1mx[m [1m [m [1mx[m [1mmq> [1;4mLIKE[m[1m <other-table-name>[m [1mqqwqqqqqqqqqqqqqqqqqqqqqqqqqqwj[m [1m mqq> (dec_local_col_list) qj[m [1m [m (B)0[m[1mdec-local-col-list = [m [1m [m [1mqqw[mq[1m> <column-name> qqk [m [1m x lqqqqqqqqqqqqqqqqqj [m [1m x tqwq> data-type qqqqwwqqqqqqqqqqqqqqqqqqqqqqqqqqqwqwqqwq>[m [1m x x mq> <domain-name> jmq> [1;4mDEFAULT[m[1m default-value qqj x x [m [1m x mqq> [1;4mCOMPUTED[m[1m [1;4mBY[m[1m value-expr qqqqqqqqqqqqqqqqqqqqqqqj x [m [1m mqqqqqqqqqqqqqqqqqqqqqqq,[m [1m<qqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m (B)0[m[1mdata-type = [m [1m [m [1m qwq> char-data-types qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqq> [m [1m tq> [1;4mTINYINT[m[1m qqqqqqqqqqqqqqwqqqqqwqqqqqqqqqqqqwqqqqqqqqqqqqqqqu [m [1m tq> [1;4mSMALLINT[m[1m qqqqqqqqqqqqqu mq> ( <n> ) qj x [m [1m tq> [1;4mINTEGER[m[1m qqqqqqqqqqqqqqu x [m [1m tq> [1;4mBIGINT[m[1m qqqqqqqqqqqqqqqu x [m [1m tq> [1;4mFLOAT[m[1m qqqqqqqqqqqqqqqqj x [m [1m tq> [1;4mNUMBER[m[1m qwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqu [m [1m x mq> ( qwq> <p> qwqwqqqqqqqqqqwq> ) j x [m [1m x mq> * qqqj mq> , <d> qj x [m [1m tq> [1;4mLIST[m[1m [1;4mOF[m[1m [1;4mBYTE[m[1m [1;4mVARYING[m[1m qqwqqqqqqqqqqqqwqqwqqqqqqqqqqqqqqwqqu [m [1m x mq> ( <n> ) qj tq> [1;4mAS[m[1m [1;4mBINARY[m[1m qu x [m [1m x mq> [1;4mAS[m[1m [1;4mTEXT[m[1m qqqj x [m [1m tq> [1;4mDECIMAL[m[1m qwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqu [m [1m tq> [1;4mNUMERIC[m[1m qjmq> ( qq> <n> wqqqqqqqqqqwq> ) j x [m [1m x mq> , <n> qj x [m [1m tq> [1;4mREAL[m[1m qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m tq> [1;4mDOUBLE[m[1m [1;4mPRECISION[m[1m qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m mq> date-time-data-types qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1m [m (B)0[m[1mchar-data-types = [m [1m [m [1mqwq> [1;4mCHAR[m[1m qqqqqqqqqqqqqwwqqqqqqqqqqqqwwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqwq>[m [1m tq> [1;4mCHARACTER[m[1m qqqqqqqqumq> ( <n> ) qjmq> [1;4mCHARACTER[m[1m [1;4mSET[m[1m char[m-[1mset-name qj x [m [1mtq> [1;4mCHAR[m[1m [1;4mVARYING[m[1m qqqqqu[m [1m [m [1mx [m [1mtq> [1;4mCHARACTER[m[1m [1;4mVARYING[m[1m j[m [1mx [m [1mtq> [1;4mVARCHAR[m[1m qqw>[m [1m( <n> ) qqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqu [m [1mtq> [1;4mVARCHAR2[m[1m qj[m [1m mq> [1;4mCHARACTER[m[1m [1;4mSET[m[1m char-set-name qj [m [1mx[m [1m tq> [1;4mLONG[m[1m [1;4mVARCHAR[m[1m qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m tq> [1;4mNCHAR[m[1m qqqqqqqqqqqqqqwqwqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m tq> [1;4mNATIONAL[m[1m [1;4mCHAR[m[1m qqqqqqu mq> ( <n> ) qj [m [1m [m [1mx [m [1m tq> [1;4mNATIONAL[m[1m [1;4mCHARACTER[m[1m qj [m [1m [m [1mx [m [1m tq> [1;4mNCHAR[m[1m [1;4mVARYING[m[1m qqqqqqqqqqqqqqwqwqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqu[m [1m tq> [1;4mNATIONAL[m[1m [1;4mCHAR[m[1m [1;4mVARYING[m[1m qqqqqqu mq> ( <n> ) qj [m [1m [m [1mx [m [1m tq> [1;4mNATIONAL[m[1m [1;4mCHARACTER[m[1m [1;4mVARYING[m[1m qj [m [1m [m [1mx [m [1mtq> [1;4mRAW[m[1m q> ( <n> ) q[mqqq[1mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu[m [1mmq> [1;4mLONG[m[1m qwqqqqqqqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1mmq> [1;4mRAW[m[1m qj[m (B)0[m[1mdate-time-data-types = [m [1m [m [1mqqwq> [1;4mDATE[m[1m qwqqqqqqqqqqwqqqqqqqqqqqqqqqqqwqq> [m [1m x tq> [1;4mANSI[m[1m qu x [m [1m x mq> [1;4mVMS[m[1m qqqj x [m [1m tq> [1;4mTIME[m[1m qqq> frac qqqqqqqqqqqqqqqqqqqqu [m [1m tq> [1;4mTIMESTAMP[m[1m qq> frac qqqqqqqqqqqqqqqqu [m [1m mq> [1;4mINTERVAL[m[1m qqq> interval-qualifier qqj [m [1m [m (B)0[m[1mfrac = [m [1m [m [1mqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqwq> [m [1m mqq> ( <numeric-literal> ) qj [m [1m [m (B)0[m[1minterval-qualifier = [m [1m [m [1mqqwq> [1;4mYEAR[m[1m qqq> prec qqwqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqwq> [m [1m x mq> [1;4mTO[m[1m [1;4mMONTH[m[1m qj x [m [1m tq> [1;4mMONTH[m[1m qq> prec qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m tq> [1;4mDAY[m[1m qqqq> prec qqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m x mq> [1;4mTO[m[1m qwq> [1;4mHOUR[m[1m qqqqqqqqqqqqqqqu [m [1m x tq> [1;4mMINUTE[m[1m qqqqqqqqqqqqqu [m [1m x mq> [1;4mSECOND[m[1m q> frac qqqqqu [m [1m tq> [1;4mHOUR[m[1m qqq> prec qqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m x mq> [1;4mTO[m[1m qwq> [1;4mMINUTE[m[1m qqqqqqqqqqqqqu [m [1m x mq> [1;4mSECOND[m[1m q> frac qqqqqu [m [1m tq> [1;4mMINUTE[m[1m q> prec qqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m x mq> [1;4mTO[m[1m [1;4mSECOND[m[1m qqqqqq> frac qqqqqu [m [1m mq> [1;4mSECOND[m[1m q> seconds-prec qqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1m [m (B)0[m[1mprec = [m [1m [m [1mqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqwq> [m [1m mqq> ( <numeric-literal> ) qj [m [1m [m (B)0[m[1mseconds-prec = [m [1m [m [1mqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqq> [m [1m mq> ( <numeric-literal-1> qqqk x [m [1m lqqqqqqqqqqqqqqqqqqqqqqqqqj x [m [1m mwqqqqqqqqqqqqqqqqqqqqqqqqqqwq> ) qqj [m [1m m> , <numeric-literal-2> qqj [m [1m [m
3 – Arguments
3.1 – dec-local-col-definition
The definition for a column in the table. SQL gives you two ways to specify column definitions: o By directly specifying a data type to associate with a column name o By naming a domain that indirectly specifies a data type to associate with a column name See the CREATE TABLE for more information about column definitions. See the Data_Types HELP topic for more information about data types.
3.2 – COMPRESSION_IS
Syntax options: COMPRESSION IS ENABLED | COMPRESSION IS DISABLED Specifies whether run-length compression is enabled or disabled for rows inserted into the declared local temporary table. In some cases, the data inserted into a local temporary table may not compress and so incur only overhead in the row. This overhead is used by Rdb to describe the sequence of uncompressible data. Use COMPRESSION IS DISABLED to prevent Rdb from attempting the compression of such data. The default is COMPRESSION IS ENABLED.
3.3 – ON_COMMIT
Syntax options: ON COMMIT PRESERVE ROWS | ON COMMIT DELETE ROWS Specifies whether data is preserved or deleted after a COMMIT statement for declared local temporary tables. The default, if not specified, is ON COMMIT DELETE ROWS.
3.4 – table-name
The name of the table you want to declare. You can optionally precede the table-name with an alias-name and a period (.). You must, however, precede the table-name with the keyword MODULE and a period (.), for example, MODULE.EMPL_PAYROLL.
4 – Examples
Example 1: Declaring and using a declared local temporary table in interactive SQL SQL> DECLARE LOCAL TEMPORARY TABLE MODULE.PAYCHECK_DECL_INT cont> (EMPLOYEE_ID ID_DOM, cont> LAST_NAME CHAR(14), cont> HOURS_WORKED INTEGER, cont> HOURLY_SAL INTEGER(2), cont> WEEKLY_PAY INTEGER(2)) cont> ON COMMIT PRESERVE ROWS; SQL> -- SQL> INSERT INTO MODULE.PAYCHECK_DECL_INT cont> (EMPLOYEE_ID, LAST_NAME, HOURS_WORKED, HOURLY_SAL, WEEKLY_PAY) cont> SELECT P.EMPLOYEE_ID, E.LAST_NAME, P.HOURS_WORKED, cont> P.HOURLY_SAL, P.HOURS_WORKED * P.HOURLY_SAL cont> FROM EMPLOYEES E, PAYROLL P cont> WHERE E.EMPLOYEE_ID = P.EMPLOYEE_ID cont> AND P.WEEK_DATE = DATE '1995-08-01'; 100 rows inserted SQL> SELECT * FROM MODULE.PAYCHECK_DECL_INT LIMIT TO 2 ROWS; EMPLOYEE_ID LAST_NAME HOURS_WORKED HOURLY_SAL WEEKLY_PAY 00165 Smith 40 30.50 1220.00 00166 Dietrich 40 36.00 1440.00 2 rows selected Example 2: Creating a stored module that contains the following: o A declared local temporary table, MODULE.PAYCHECK_DECL_TAB o A procedure, PAYCHECK_INS_DECL, that inserts weekly salary records into the declared local temporary table, MODULE.PAYCHECK_DECL_TAB o A procedure, LOW_HOURS_DECL, that counts the number of employees with less than 40 hours worked The following example also demonstrates that you can access the declared local temporary table only from within the module. SQL> -- Create the module containing a declared temporary table. SQL> -- SQL> CREATE MODULE PAYCHECK_DECL_MOD cont> LANGUAGE SQL cont> DECLARE LOCAL TEMPORARY TABLE MODULE.PAYCHECK_DECL_TAB cont> (EMPLOYEE_ID ID_DOM, cont> LAST_NAME CHAR(14) , cont> HOURS_WORKED INTEGER, HOURLY_SAL INTEGER(2), cont> WEEKLY_PAY INTEGER(2)) cont> ON COMMIT PRESERVE ROWS cont> -- cont> -- Create the procedure to insert rows. cont> -- cont> PROCEDURE PAYCHECK_INS_DECL; cont> BEGIN cont> INSERT INTO MODULE.PAYCHECK_DECL_TAB cont> (EMPLOYEE_ID, LAST_NAME, HOURS_WORKED, HOURLY_SAL, WEEKLY_PAY) cont> SELECT P.EMPLOYEE_ID, E.LAST_NAME, P.HOURS_WORKED, cont> P.HOURLY_SAL, P.HOURS_WORKED * P.HOURLY_SAL cont> FROM EMPLOYEES E, PAYROLL P cont> WHERE E.EMPLOYEE_ID = P.EMPLOYEE_ID cont> AND P.WEEK_DATE = DATE '1995-08-01'; cont> END; cont> -- cont> -- Create the procedure to count the low hours. cont> -- cont> PROCEDURE LOW_HOURS_DECL (:cnt INTEGER); cont> BEGIN cont> SELECT COUNT(*) INTO :cnt FROM MODULE.PAYCHECK_DECL_TAB cont> WHERE HOURS_WORKED < 40; cont> END; cont> END MODULE; SQL> -- SQL> -- Call the procedure to insert the rows. SQL> -- SQL> CALL PAYCHECK_INS_DECL(); SQL> -- SQL> -- Declare a variable and call the procedure to count records with SQL> -- low hours. SQL> -- SQL> DECLARE :low_hr_cnt integer; SQL> CALL LOW_HOURS_DECL(:low_hr_cnt); LOW_HR_CNT 2 SQL> -- SQL> -- Because the table is a declared local temporary table, you cannot SQL> -- access it from outside the stored module that contains it. SQL> -- SQL> SELECT * FROM MODULE.PAYCHECK_DECL_TAB; %SQL-F-RELNOTDCL, Table PAYCHECK_DECL_TAB has not been declared in module or environment Example 3: Disabling Compression for a Declard Local Temporary Table The following example shows a declared local temporary table that will not benefit from compression. The clause COMPRESSION IS DISABLED is used to reduce the CPU overhead for the table as well as preventing a possible row size increase because of compression notations. SQL> declare local temporary table module.scratch0 cont> (averages double precision) cont> compression is DISABLED cont> on commit PRESERVE rows cont> ; SQL> SQL> insert into module.scratch0 cont> select avg (char_length (a)) from module.scratch1; 1 row inserted SQL> SQL> select * from module.scratch0; AVERAGES 2.100000000000000E+001