Special registers such as CURRENT SQLID are IBM-defined session values. DB2 for z/OS also lets you create your own named session values with CREATE VARIABLE. These global variables share a catalog definition across the subsystem, but each session keeps a private copy of the value. They are the SQL way to pass a fact from one statement to the next without a host variable or a work table—useful in SPUFI, SQL PL, and tools that cannot declare COBOL host variables.
A user-defined global variable is created once, stored in SYSIBM.SYSVARIABLES, and then referenced by SQL running at that server. It is not a COBOL host variable, not a special register, and not a column. It lives in a schema. Unqualified CREATE, DROP, COMMENT, GRANT, and REVOKE names follow ordinary authorization-ID qualification. Other references use the SQL path, similar to functions and distinct types.
Typical data types are the built-in types you already use on columns, plus distinct types and array types. Array-typed global variables are a common way to hold a list in SQL PL without a declared temporary table.
CREATE VARIABLE names the variable, gives it a data type, and optionally a DEFAULT. If you omit DEFAULT, the default is NULL.
12345CREATE VARIABLE HR.GV_DEPT CHAR(3) DEFAULT 'A00'; CREATE VARIABLE HR.GV_ASOF DATE DEFAULT CURRENT DATE; CREATE VARIABLE HR.GV_NOTE VARCHAR(100); -- default NULL
Rules beginners hit first:
Naming convention: many shops prefix names with GV_ so a SELECT list does not look like a missing column qualifier. If a column and a global variable share an unqualified name, column resolution usually wins in a table context—qualify the variable.
ALTER VARIABLE changes the definition, most often the default. It does not rewrite every session’s current value; sessions keep what they already SET until they end or they assign again. New sessions pick up the new default.
12ALTER VARIABLE HR.GV_DEPT SET DEFAULT 'B01';
DROP VARIABLE removes the catalog object. Privileges recorded in SYSIBM.SYSVARIABLEAUTH go away with it. Dependent SQL that still names the variable fails to prepare.
1DROP VARIABLE HR.GV_NOTE;
You change the session’s copy with assignment, not with UPDATE of a catalog table.
123456789SET HR.GV_DEPT = 'E21'; SELECT WORKDEPT INTO HR.GV_DEPT FROM HR.EMPLOYEE WHERE EMPNO = '000010'; VALUES CURRENT DATE INTO HR.GV_ASOF;
Assignment requires WRITE privilege on the variable. The new value must be assignable to the declared type (including CCSID and distinct-type casting rules). Setting a variable to NULL is allowed unless the type or default rules forbid it for that object.
Once set (or left at default), you reference the name anywhere an expression of that type is legal in a query or SET statement:
123456789SELECT EMPNO, LASTNAME FROM HR.EMPLOYEE WHERE WORKDEPT = HR.GV_DEPT ORDER BY LASTNAME; SELECT HR.GV_DEPT, COUNT(*) FROM HR.EMPLOYEE WHERE WORKDEPT = HR.GV_DEPT GROUP BY HR.GV_DEPT;
This is the interactive-SQL payoff: run a SET in SPUFI, then several SELECTs that reuse the value, without host variables. Inside SQL PL procedures the same pattern shares a value across statements that are not in one compound block’s DECLARE list.
Places you cannot use a global variable include check constraints, materialized query table definitions, and index key expressions. Those objects must be deterministic with respect to the table, not a session scratchpad.
IBM’s wording is: global variables have a session scope. They are available to all sessions at the current server, but the value is private for each session. Two users who both SET HR.GV_DEPT do not overwrite each other.
| Attachment | Session for global variables |
|---|---|
| Local TSO, batch, CAF (non-DDF) | The Db2 thread |
| DRDA client (DDF) | The logical connection to the server |
| Native REST | The transaction (stateless reset at end) |
Details that surprise people:
Access is not “if you can CREATE you can SET.” After CREATE, control READ and WRITE with GRANT and REVOKE.
12345678GRANT READ ON VARIABLE HR.GV_DEPT TO PUBLIC; GRANT READ, WRITE ON VARIABLE HR.GV_DEPT TO ROLE HR_APP; GRANT ALL PRIVILEGES ON VARIABLE HR.GV_ASOF TO HRDBA WITH GRANT OPTION; REVOKE WRITE ON VARIABLE HR.GV_DEPT FROM PUBLIC;
Catalog table SYSIBM.SYSVARIABLEAUTH records who has which privilege from which grantor. Only an explicitly granted privilege can be revoked (the usual Db2 GRANT/REVOKE rule), unless an authority such as SECADM or ACCESSCTRL revokes using BY.
Treat a global variable like a session memory slot, not like a table with audit columns.
Functions and procedures inherit the session’s global variable values from the invoking environment. A UDF that reads HR.GV_DEPT sees whatever the caller SET, which is useful for “current department” filters and dangerous if the function is SECURED and used in a row permission without careful design.
A global variable is a labeled sticky note on your desk, not on the classroom wall. The teacher prints one pad of sticky notes for everyone (CREATE VARIABLE). Each kid writes their own number on their own note (SET). Your neighbor cannot read your number, and you cannot read theirs. If you leave your desk and someone else sits down (thread reuse), your sticky note might still be there—so wipe it before you go. Putting the note away for the night (COMMIT) does not erase what you wrote.
1. What does CREATE VARIABLE define in Db2 for z/OS?
2. Who sees the value you SET in a global variable?
3. Which privileges control access to a global variable?
4. Do COMMIT and ROLLBACK restore a global variable to its default?
5. Where can you not use a global variable?