SQL Server supports the execution of SQL not within the context of a stored procedure or trigger. This is quite a handy feature when debugging as it allows one to write thowaway code without the overhead of declaring a new stored procedure that later has to be dropped.
Although it is possible to do this in DB2 too, it isn’t as transparent as it is in SQL Server (because – I expect – DB2 is more ANSI SQL compliant than SQL Server). The trick is to declare the code that one wants executed in an ATOMIC block:
begin atomic declare var_a char(1); set var_a = (select ibmreqd from sysibm.sysdummy1); end @
This may be executed from a Command Center session, or (having connected to a database, and the above dumped into a file called "script.sql") via the CLP:
$ db2 -td@ -vf script.sql
Thanks to DHRUV for a detailed post on the subject.
0 Responses to “anonymous (sql) blocks in db2”