Dynamic? What's that all about?
A lot of new things came out in the IBM i Technology Refresh for 7.1 that was released this past fall. And some of those enhancements have been for DB2, the DDL-based database that's part of the i operating system. The only one of those enhancements that we're going to look at today is the one for Compound Dynamic SQL statements, which were added as part of PTF Group SF99701, Level 26.
A Review: Compound SQL Statements
Compound SQL statements have been part of SQL for quite some time. In short, they allow you to bind a set of SQL statements into a single entity that can then be triggered as a unit.
The compound statement starts with a BEGIN statement and ends with an END statement. You can include any executable SQL statement you want into the mix.
There are two basic types of compound (and compound dynamic) statements: atomic and non-atomic. And yes, I had the same public safety reaction you are having but don't go mental on me; neither is seriously radioactive. Atomic means that the whole thing is treated like a unit. If something in it blows up, then the whole thing ends and any results are backed out. Non-atomic means that if one statement ends in error, then the compound block will keep going and the next statement will be executed with no back out occurring.
Why would you use compound statements? Efficiency mostly. By putting things in this envelope, you reduce overhead on the data manager. And, it also means fewer messages being sent to any remote clients. Plus it's just kind of nice to be able to encapsulate a series of related statements into a pseudo script.
But it's not perfect, and if you have something that's going to be executed lots and lots of times, you might be better off putting it in a procedure, which is more efficient still.
Compound Dynamic SQL Statements
The topic of this tip, however, is not compound statements but compound dynamic statements. And this is what has just been added to DB2 with this TR7 PTF. So what is the difference? Dude. They're dynamic. I mean dynamic. Can you dig it, man?
First off, dynamic CSes are compiled within SQL as a single statement. This is done outside of the compile for the program as a whole. The system actually creates an ILE C program for this script, named QTEMP/QCMPDnnnnn, where nnnnnn is a sequential number given to the program by Star Fleet Command. This program's existence is only temporary, and after the program is completed it will be automatically deleted.
Second, within the statement itself, you can use any of the statements that can be used in a normal compound statement (executable, variables, handlers, For, If, While, etc.). Both non-atomic and atomic are supported, and you can have nested compound dynamic statements. Nesting can be used to help define scope (and so simplify other nesting levels). In other words, you have the power within these statements to make things as complicated and devious as you would like. Parameters, however, cannot be used on a compound dynamic statement. If you need that kind of functionality, you should use global variables instead.
Third, how do you kick this thing off? Well, it can be kicked off via any dynamic SQL option, like through Ops Nav or even RUNSQLSMT. Want to run it from inside a program? Then you have to set it up using the PREPARE and EXECUTE statements (this is where the temporary program is created by DB2), but that's another topic. In addition, it can be added to a trigger, an SQL method, or an SQL function.
Fourth, even though this results in a compiled program, the word on the street is that it's still more efficient if it's going to be run lots and lots to do this within a stored procedure. It's ideally designed for something that will not be run all that often but could have a lot of data involved.
Fifth, no special authority is required to do this, but you have to make sure that the authorization ID of the compound dynamic statement as a whole is sufficient to run every statement in the grouping.
And That's It
And that's it. Except for all of the other stuff. If you want to see the layout of the compound dynamic statement or really understand all the twists and turns involved (like the fact that the options for the SQL variable declaration clause are SQL variable name, data type, array type name, constant, null, or not null), then you should check out the DB2 for i SQL Reference. It's a good read.
LATEST COMMENTS
MC Press Online