Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Chapter 58 SQL command %INTRANSACTION

Displays transaction status.

The outline

%INTRANSACTION
%INTRANS
Copy the code

parameter

There is no

describe

The %INTRANSACTION statement sets the SQLCODE to indicate the transaction status:

  • If it is currently in a transactionSQLCODE=0.
  • If not in a transactionSQLCODE=100.

%INTRANSACTION returns SQLCODE=0 while a transaction is in progress. This TRANSACTION can be an SQL TRANSACTION initiated by START TRANSACTION or SAVEPOINT. It can also be an ObjectScript transaction initiated by TSTART.

Transaction nesting has no effect on %INTRANSACTION. SET TRANSACTION has no effect on %INTRANSACTION.

You can also use $TLEVEL to determine the transaction state. %INTRANSACTION only indicates whether a transaction is in progress. $TLEVEL indicates whether a transaction is in progress and the current number of transaction levels.

The sample

The following embedded SQL example shows how %INTRANSACTION sets SQLCODE:

ClassMethod %INTRANSACTION()
{
	n SQLCODE
	&sql(%INTRANSACTION)
	w "Before %INTRANS SQLCODE=",SQLCODE," TL=",$TLEVEL,!
	&sql(SET TRANSACTION %COMMITMODE EXPLICIT)
	n SQLCODE
	&sql(%INTRANSACTION)
	w "SetTran %INTRANS SQLCODE=",SQLCODE," TL=",$TLEVEL,!
	&sql(START TRANSACTION)
	n SQLCODE
	&sql(%INTRANSACTION)
	w "StartTran %INTRANS SQLCODE=",SQLCODE," TL=",$TLEVEL,!
	&sql(SAVEPOINT a)
	n SQLCODE
	&sql(%INTRANSACTION)
	w "Savepoint %INTRANS SQLCODE=",SQLCODE," TL=",$TLEVEL,!
	&sql(ROLLBACK TO SAVEPOINT a)
	n SQLCODE
	&sql(%INTRANSACTION)
	w "Rollback to Savepoint %INTRANS SQLCODE=",SQLCODE," TL=",$TLEVEL,!
	&sql(COMMIT)
	n SQLCODE
	&sql(%INTRANSACTION)
	w "After Commit %INTRANS SQLCODE=",SQLCODE," TL=",$TLEVEL
}
Copy the code