Specifies the characteristics for a default transaction. A transaction is a group of statements whose changes can be made permanent or undone only as a unit. A transaction ends with a COMMIT or ROLLBACK statement. If you end the transaction with the COMMIT statement, all changes made to the database by the statements are made permanent. If you end the transaction with the ROLLBACK statement, the statements do not take effect. The characteristics specified in a DECLARE TRANSACTION statement affect all transactions (except those started by the SET TRANSACTION or START TRANSACTION statement) until you issue another DECLARE TRANSACTION statement. The characteristics specified in a SET TRANSACTION or START TRANSACTION statement affect only that transaction. A DECLARE TRANSACTION statement does not start a transaction. The declarations made in a DECLARE TRANSACTION statement do not take effect until SQL starts a new transaction. SQL starts a new transaction with the first executable data manipulation or data definition statement following a DECLARE TRANSACTION, COMMIT, or ROLLBACK statement. In the latter case (following a COMMIT or ROLLBACK statement), SQL applies the transaction characteristics you declared for the transaction that just ended to the next one you start. In addition to the DECLARE TRANSACTION statement, you can specify the characteristics of a transaction in one of two ways: o If you specify the SET TRANSACTION or START TRANSACTION statement, the declarations in the statement take effect immediately and SQL starts a new transaction. o You can retrieve and update data without declaring or setting a transaction explicitly. If you omit the DECLARE TRANSACTION, SET TRANSACTION or START TRANSACTION statements, SQL automatically starts a transaction (using the read/write option) with the first executable data manipulation or data definition statement following a COMMIT or ROLLBACK statement. See the Oracle Rdb SQL Reference Manual for examples of when you would want to use the DECLARE TRANSACTION statement instead of the SET TRANSACTION or START TRANSACTION statement. You can specify many options with the DECLARE TRANSACTION statement, including: o A transaction mode (READ ONLY/READ WRITE/BATCH UPDATE) o A lock specification clause (RESERVING options) o A wait mode (WAIT/NOWAIT) o An isolation level o A constraint evaluation specification clause o Multiple sets of all the preceding options for each database involved in the transaction (ON clause)
1 – Environment
You can use the DECLARE TRANSACTION statement: o In interactive SQL o Embedded in host language programs to be precompiled o In a context file o As part of the DECLARE section in an SQL module o As part of the module header in a CREATE MODULE statement o In dynamic SQL as a statement to be dynamically executed In host language programs, you can have only a single DECLARE TRANSACTION statement in each separately compiled source file. See the Oracle Rdb SQL Reference Manual for more information. The DECLARE TRANSACTION statement is an extension to standard SQL syntax. If your program must adhere to standard SQL syntax, you can isolate a DECLARE TRANSACTION statement by putting it in a context file. For more information on context files, see the Oracle Rdb Guide to SQL Programming.
2 – Format
(B)0[m[1;4mDECLARE[m[1m [1;4mTRANSACTION[m[1m qqwqqqqqqqqqqqqqqqwq> [m [1m tq> tx-options qu [m [1m mq> db-txns qqqqj [m (B)0[m[1mtx-options = [m [1m [m [1mqqwqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqwq> [m [1mx tq>[m [1;4mNAME[m[1m 'quoted-string' qqqqqqqqqqqqqqqqu[m [1mx[m [1mx[m [1mtq> [1;4mEVALUATING[m[1m qwq evaluating-clause qqwqu[m [1mx[m [1mx[m [1mx [m [1mmqqqqqqq> , <qqqqqqqqqqj[m [1mx[m [1mx[m [1m x[m [1mtq>[m [1;4mRESERVING[m[1m qqwq> reserving-clause qqwqu x [m [1mx[m [1mx[m [1m [m [1mmqqqqqqqq[m [1m, <qqqqqqqqqqj[m [1mx[m [1mx[m [1mx tq>[m [1misolation-level qqqqqqqqqqqqqqqqqqqqqu[m [1mx[m [1mx[m [1mtq> transaction-mode[m [1mqqqqqqqqqqqqqqqqqqqqu[m [1mx[m [1mx[m [1mmq>[m [1mwait-option[m [1mqqqqqqqqqqqqqqqqqqqqqqqqqj[m [1mx[m [1mmqqqqqqqqqqqqqqqqwqqqqqqqwqqqqqqqqqqqqqqqqqqqj[m [1mmqq[m [1m, <qj[m (B)0[m[1mevaluating-clause = [m [1m [m [1mqwqqqqqqqqqqqqqwq> <constraint-name> q> [1;4mAT[m[1m qwq> [1;4mVERB[m[1m [1;4mTIME[m[1m qqqwqq> [m [1m mq> <alias.> qj mq> [1;4mCOMMIT[m[1m [1;4mTIME[m[1m qj [m [1m [m (B)0[m[1mreserving-clause = [m [1m [m [1mqwqwq> <view-name> qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqwqk [m [1m x mq> <table-name> qwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqj x x [m [1m x mq> [1;4mPARTITION[m[1m qq> ( qwq> <part-num> qwq> ) qj x x [m [1m x mqqqqqq , <qqqqqj x x [m [1m mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq , <qqqqqqqqqqqqqqqqqqqqqqqqqqqqj x [m [1m lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1m mq> [1;4mFOR[m[1m qwqqqqqqqqqqqqqqwqqwq> [1;4mREAD[m[1m qqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqqq> [m [1m tq> [1;4mEXCLUSIVE[m[1m qu tq> [1;4mWRITE[m[1m qqqqqqqqqqqu [m [1m tq> [1;4mPROTECTED[m[1m qu mq> [1;4mDATA[m[1m [1;4mDEFINITION[m[1m qj [m [1m mq> [1;4mSHARED[m[1m qqqqj [m [1m [m (B)0[m[1misolation-level =[m [1mqqq> [1;4mISOLATION[m [1;4mLEVEL[m[1m qqwq>[m [1;4mREAD[m [1;4mCOMMITTED[m [1mqqqqwqq>[m [1mtq>[m [1;4mREPEATABLE[m [1;4mREAD[m [1mqqqu[m [1mmq>[m [1;4mSERIALIZABLE[m[1m qqqqqqj[m (B)0[m[1mtransaction-mode =[m [1mqqwq>[m [1;4mREAD[m [1;4mONLY[m [1mqqqqwq>[m [1mtq>[m [1;4mREAD[m [1;4mWRITE[m[1m qqqu[m [1mmq> [1;4mBATCH[m[1m [1;4mUPDATE[m[1m qj[m (B)0[m[1mwait-option =[m q[1mqwq> [1;4mWAIT[m[1m qwqqqqqqqqqqqqqqqqqqqqqwqwq>[m [1mx[m [1mmq> <timeout-value>[m [1mqqj[m [1mx[m [1mmq> [1;4mNOWAIT[m[1m qqqqqqqqqqqqqqqqqqqqqqqj[m (B)0[m[1mdb-txns = [m [1m [m [1mqqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqq>[m [1m mwq> [1;4mON[m[1m qwq> <alias> qwq> [1;4mUSING[m[1m qq> ( wq> tx-options qqwq> ) qwj [m [1m x mqqqq , <qqqqj mq> [1;4mDEFAULTS[m[1m qqqqj x [m [1m mqqqqqqqqqqqqqqqqqqqqqqqqqq [1;4mAND[m[1m <qqqqqqqqqqqqqqqqqqqqqqqqqqqqj [m [1m [m
3 – Arguments
The DECLARE TRANSACTION arguments are the same as the arguments for the SET TRANSACTION statement. See the SET_TRANSACTION statement for more information about the arguments for both statements.
4 – Defaults
The DECLARE TRANSACTION defaults are the same as the defaults for the SET TRANSACTION statement. See the SET_TRANSACTION statement for complete information. In general, you should not rely on default transaction characteristics. Use explicit DECLARE TRANSACTION statements, specifying read/write, read-only, or batch-update options; a list of tables in the RESERVING clause; and a share mode and lock type for each table. The more specific you are in a DECLARE TRANSACTION statement, the more efficient your database operations will be. When a transaction starts using characteristics specified in a DECLARE TRANSACTION statement, any transaction characteristics unspecified in the DECLARE TRANSACTION statement take the SQL defaults. This is true even if the characteristics unspecified in DECLARE TRANSACTION were specified in an earlier SET or DECLARE TRANSACTION statement.
5 – Examples
Example 1: Illustrating DECLARE and SET TRANSACTION differences In the following example, the first executable statement following the DECLARE TRANSACTION statement starts a transaction. In contrast, the subsequent SET TRANSACTION statement itself starts a transaction. SQL> DECLARE TRANSACTION READ WRITE NOWAIT; SQL> -- SQL> -- Notice the "no transaction is in progress" message: SQL> -- SQL> SHOW TRANSACTION Transaction information: Statement constraint evaluation is off On the default alias Transaction characteristics: Nowait Read Write Transaction information returned by base system: no transaction is in progress - session ID number is 80 SQL> -- SQL> -- The first executable statement following the SQL> -- DECLARE TRANSACTION statement starts the transaction. SQL> -- In this case, SELECT is the first executable statement. SQL> -- SQL> SELECT LAST_NAME FROM CURRENT_SALARY; LAST_NAME Toliver Smith Dietrich . . . SQL> -- SQL> -- Note the transaction inherits the read/write characteristics SQL> -- specified in the DECLARE TRANSACTION statement: SQL> -- SQL> SHOW TRANSACTION; Transaction information: Statement constraint evaluation is off On the default alias Transaction characteristics: Nowait Read Write Transaction information returned by base system: a read-write transaction is in progress - updates have not been performed - transaction sequence number (TSN) is 416 - snapshot space for TSNs less than 416 can be reclaimed - session ID number is 80 SQL> -- SQL> ROLLBACK; SQL> -- SQL> -- Again, notice the "no transaction is in progress" message: SQL> -- SQL> SHOW TRANSACTION; Transaction information: Statement constraint evaluation is off On the default alias Transaction characteristics: Nowait Read Write Transaction information returned by base system: no transaction is in progress - transaction sequence number (TSN) 416 is reserved - snapshot space for TSNs less than 416 can be reclaimed - session ID number is 80 SQL> -- SQL> -- Unlike DECLARE TRANSACTION, the SET TRANSACTION statement SQL> -- immediately starts a transaction: SQL> -- SQL> SET TRANSACTION READ ONLY WAIT; SQL> -- SQL> -- Note the transaction characteristics show the SQL> -- read-only characteristics: SQL> -- SQL> SHOW TRANSACTION; Transaction information: Statement constraint evaluation is off On the default alias Transaction characteristics: Wait Read only Transaction information returned by base system: a snapshot transaction is in progress - all transaction sequence numbers (TSNs) less than 416 are visible - TSN 416 is invisible - all TSNs greater than or equal to 417 are invisible - session ID number is 80 Example 2: Using a DECLARE TRANSACTION statement in a context file The following example shows a context file, test_declares.sql, that contains declarations for precompiling source file test.sco: DECLARE ALIAS FOR FILENAME personnel; DECLARE TRANSACTION READ WRITE RESERVING EMPLOYEES FOR PROTECTED WRITE, JOB_HISTORY FOR PROTECTED WRITE, DEPARTMENTS FOR SHARED READ, JOBS FOR SHARED READ; The section in the Oracle Rdb Guide to SQL Programming about program transportability explains when you may need an SQL context file to support a program that includes SQL statements. Example 3: Explicitly setting the isolation level in a DECLARE TRANSACTION statement In this example, you declare the default characteristics for a read/write transaction, which includes changing the default ISOLATION LEVEL SERIALIZABLE to ISOLATION LEVEL REPEATABLE READ. SQL> DECLARE TRANSACTION READ WRITE ISOLATION LEVEL REPEATABLE READ; Example 4: Reserving a Partition SQL> -- Determine the ordinal position of the EMPLOYEES SQL> -- partitions. SQL> SELECT RDB$MAP_NAME, RDB$AREA_NAME, RDB$ORDINAL_POSITION cont> FROM RDB$STORAGE_MAP_AREAS cont> WHERE RDB$MAP_NAME='EMPLOYEES_MAP'; RDB$MAP_NAME RDB$AREA_NAME RDB$ORDINAL_POSITION EMPLOYEES_MAP EMPIDS_LOW 1 EMPLOYEES_MAP EMPIDS_MID 2 EMPLOYEES_MAP EMPIDS_OVER 3 3 rows selected SQL> -- SQL> -- Reserve EMPIDS_MID and EMPIDS_OVER for SQL> -- exclusive write. SQL> -- SQL> DECLARE TRANSACTION cont> RESERVING EMPLOYEES PARTITION (2,3) cont> FOR EXCLUSIVE WRITE;