About seven years ago, in an entry posted to the blog I maintained while working as an independent DB2 consultant (prior to rejoining IBM), I wrote of the evolution of DB2 for z/OS SQL from merely a data manipulation language to a programming language. That evolution continues with DB2 Version 11, in part through the introduction in that DB2 release of global variable functionality. If you want to know more about this new application enabling feature of DB2 for z/OS, read on.
As SQL (in a DB2 for z/OS context) has become an ever-richer language, the scenarios in which lines of code written in some other language (e.g., COBOL or C or Java) have to be interspersed with SQL statements to accomplish some objective have decreased in number. A few more such scenarios were eliminated thanks to global variables. In particular, consider the situation in which you want to use one query to obtain a value from a DB2 table, which you then plug into a predicate of a second query. You could certainly write a simple routine (in COBOL or some other programming language) that declares a host variable, populates it with a value retrieved by a query of a table, and then issues a second query with the host variable referenced in a predicate. The pseudo-code would look something like this:
DECLARE VARIABLE :hvar AS CHARACTER STRING
SELECT COL1 FROM T1 INTO :hvar WHERE COL2 = '0123'
SELECT COL3 FROM T2 WHERE COL4 = :hvar
Now, you might say, "I don't need COBOL (or Java or whatever) to do that. I can write a native SQL procedure that gets this done using nothing but SQL statements." True enough, but there are scenarios in which use of a SQL PL routine would not be appropriate -- or maybe not even possible. Suppose, for example, that you're working with a tool (Data Studio, perhaps, or the SPUFI interface that comes with DB2) through which one interactively issues SQL statements for execution by a DB2 subsystem. What then? That's where a DB2 11 global variable (if you're in a DB2 11 new-function mode environment) comes in handy. Here are some statements that I executed on one of our IBM test DB2 systems, by way of SPUFI:
CREATE VARIABLE ROBSBLOG VARCHAR(30) DEFAULT 'HELLO';
SET ROBSBLOG = 'PRODUCT_SALES';
SELECT CAST(NAME AS VARCHAR(20)) AS NAME, COLNO
WHERE TBNAME = ROBSBLOG
ORDER BY COLNO;
And the result of that SELECT is:
Easy breezy. And, no coding required beyond the SQL statements.
Now, there are a few informational items of which you should be aware regarding the use of DB2 global variables:
The value of a global variable is associated with the thread of an application process; thus, if the value of a global variable is set by a transaction program, the global variable's value will be discarded when the transaction ends and its thread is deallocated. If a global variable's value is set in a batch job, the variable's value will persist across COMMITs issued by the batch job (though it can of course be subsequently changed by the batch job), and will be discarded when the job completes and its thread is deallocated. In the case of my SPUFI exercise mentioned above, to make those SQL statements work as desired I had to set AUTOCOMMIT in my SPUFI session to NO. Had I not done that then the auto-COMMIT after my SET statement (referring to the statement that assigned the value 'PRODUCT_SALES' to the global variable) would have resulted in the assigned value being discarded, and then the statement
SELECT ROBSBLOG FROM SYSIBM.SYSDUMMY1;
would have returned the value 'HELLO' (the default value specified in my CREATE VARIABLE statement).
A DB2 global variable, once created, can be used by any authorization ID or role with the requisite privilege. An ID or role can be granted the READ or WRITE privilege (or both) for a given global variable. The owner of a global variable has, implicitly, all privileges on that variable.
Though a created global variable can be used by any authorization ID or role with the requisite privilege, a global variable's value does not extend beyond a given session (i.e., a given thread). So, if I create a global variable called VAR_X, and I set that variable to the value 'ABC' (assuming that it's a character string variable), another application process (with the right privilege) can use VAR_X, but the value 'ABC' that I assigned to VAR_X will not be picked up in any other session that references VAR_X; so, a global variable itself is globally available within a DB2 subsystem, but a value assigned to the global variable within a session does not extend to any other session.
Some global variables come with DB2 11 for z/OS. Besides global variables that you might create and use, there are several that are already there in a DB2 11 system. These built-in global variables are:
The first of these provides the IP address associated with a network-connected DB2 client application, or of a requester DB2 subsystem that establishes a connection to a remote DB2 host (for a local-to-DB2 application, the value of SYSIBM.CLIENT_IPADDR will be NULL). The other two built-in global variables are for use with the DB2-managed archiving (aka transparent archiving) feature of DB2 11.
There you have it. Yet another in a long line of DB2 for z/OS enhancements that have truly made SQL a programming language. When you get to DB2 11 in new-function mode (or if you're already there), give global variables a try-out, and think about how you might put them to work at your site.
NOTE: When I originally posted this entry yesterday, I stated that the value assigned to a global variable will not persist beyond a commit scope. This is true when the commit scope is associated with a transaction whose DB2 thread is deallocated with a commit driven by transaction completion; however, it is not true for a batch job, because a global variable's value pertains to its thread, and a batch job's thread is deallocated at end-of-job. That being the case, a batch job can set the value of a global variable, and that value will persist across commits issued by the batch job, because those in-job commits will not lead to deallocation of the batch job's thread.ReplyDelete
I corrected the wording of the blog post accordingly, in the section starting with the bold-type phrase "The value of a global variable is associated with the thread of an application process."
Hi Robert, I was waiting for v11 to use user global variables to hold some common data for applications, instead of a control table. E.g: batch window execution date.ReplyDelete
But I can't alter the default value. And if I drop/create I supposes that packages are invalidated.
Is there any reason why the value can not be modified?
I assume that you are telling me that you tried to use a SET statement (referring to the SQL statement SET) to assign a value to a global variable, and that SET statement failed. Did you receive a SQL error code when trying to execute that SET statement?Delete
Note that, in order to use SET with a global variable, an authorization ID needs one of the following privileges or authorities: the WRITE privilege on the global variable, or ownership of the global variable, or DATAACCESS authority, or SYSADM authority.
Robert, I know SET value has a thread scope. What I want is a global value for all applications. So I thougth in the default value of a uder global variable. But it cant be altered.Delete
Right - there is no ALTER VARIABLE statement. You could request that as an enhancement for Db2 for z/OS. The URL for the Request for Enhancement (RFE) page for products in the IBM Analytics Platform category (including Db2 for z/OS) is https://www.ibm.com/developerworks/rfe/?BRAND_ID=184.Delete