Specifying the rules that apply to SQL behavior at run time
You can specify whether Db2 rules or SQL standard rules apply to SQL behavior at run time.
About this task
Not only does SQLRULES specify the rules under which a type 2 CONNECT statement executes, but it also sets the initial value of the special register CURRENT RULES when the database server is the local Db2 system. When the server is not the local Db2 system, the initial value of CURRENT RULES is DB2. After binding a plan, you can change the value in CURRENT RULES in an application program by using the statement SET CURRENT RULES.
CURRENT
RULES determines the SQL rules, Db2 or
SQL standard, that apply to SQL behavior at run time. For example,
the value in CURRENT RULES affects the behavior of defining check
constraints by issuing the ALTER TABLE statement on a populated table:
- If CURRENT RULES has a value of STD and no existing rows
in the table violate the check constraint, Db2 adds the constraint to the table definition.
Otherwise, an error occurs and Db2 does
not add the check constraint to the table definition.
If the table contains data and is already in a check pending status, the ALTER TABLE statement fails.
- If CURRENT RULES has a value of DB2, Db2 adds the constraint to the table definition, defers the enforcing of the check constraints, and places the table space or partition in CHECK-pending status.
You can use the statement SET CURRENT RULES to control the action that the statement ALTER TABLE
takes. Assuming that the value of CURRENT RULES is initially STD, the following SQL statements
change the SQL rules to DB2, add a check
constraint, defer validation of that constraint, place the table in CHECK-pending status, and
restore the rules to STD.
EXEC SQL
SET CURRENT RULES = 'DB2';
EXEC SQL
ALTER TABLE DSN8D10.EMP
ADD CONSTRAINT C1 CHECK (BONUS <= 1000.0);
EXEC SQL
SET CURRENT RULES = 'STD';
See Check constraints for
information about check constraints.You can also use CURRENT
RULES in host variable assignments. For example, if you want to store
the value of the CURRENT RULES special register at a particular point
in time, you can use assign the value to a host variable, as in the
following statement:
SET :XRULE = CURRENT RULES;
You
can also use CURRENT RULES as the argument of a search-condition.
For example, the following statement retrieves rows where the COL1
column contains the same value as the CURRENT RULES special register. SELECT * FROM SAMPTBL WHERE COL1 = CURRENT RULES;