Reprinted with Permission by Quest Software Nov.  2002

 

Choosing to Use Dynamic SQL
Topic Extracted from Knowledge Xpert for DB2

In several cases, a specific variation of dynamic SQL may be only one of several alternatives available. These alternatives include the use of static SQL with various BIND parameters as well as the deployment of stored procedures. This topic explores these possibilities and introduces some of the issues that must be considered before the appropriate choice is made.

Choosing Between Static and Dynamic SQL

Consider an application program that must return a list of employees for a wide variety of user requirements. For example, the user may wish to list all employees for a department or a list of all employees whose last name starts with some set of characters. In general, the user requirements can be captured via a screen that looks something like the one shown below.

              FROM      TO        SORT ORDER
LAST NAME     ________  ________  ________
FIRST NAME    ________  ________  ________
WORK DEPT.    ________  ________  ________
HIRE DATE     ________  ________  ________
SALARY        ________  ________  ________
BONUS         ________  ________  ________
COMMISSION    ________  ________  ________
There are three main alternatives to developing an application to process such requests and we discuss the merits of each approach below:

At least in cases like this, where a highly flexible application is needed, dynamic SQL should be seriously considered as a possible solution. You should also consider the following factors before deciding whether or not dynamic SQL is appropriate for your needs.

Choosing Between REOPT(VARS) and Dynamic SQL

The BIND option REOPT(VARS) can work in conjunction with dynamic SQL. Here, we discuss its use with static SQL as an alternative to using dynamic SQL.

To overcome the potentially sub-optimal access path that can result for static SQL in the presence of non-uniform data or highly correlated data, the REOPT(VARS) bind option can be used. This makes static SQL behave like dynamic SQL since the access path is selected at runtime after the values of the host variables are known. If the sole reason why dynamic SQL is being considered is to overcome the performance issues arising from non-uniform distribution of data or column correlation affecting static SQL, you should consider the use of REOPT(VARS) with static SQL first.

Also note that dynamic SQL can be written at the statement level. The REOPT(VARS) bind option generally applies to all statements within a package (unless optimization hints are used).

Choosing Between Dynamic SQL and Stored Procedures

One of the reasons why people are often reluctant to use dynamic SQL is because of the way authorization works. By default, every user needs to be granted access to all objects they access via dynamic SQL. Administering these authorizations can be time consuming and once users have access to the objects, they can access the objects “outside” the application as well.

In some cases, using stored procedures can be an alternative. If the applications needs the use of dynamic SQL, for example, because you cannot code all potential combinations using static SQL, you can get around some of the security issues by using a stored procedure to encapsulate the logic that requires the use of dynamic SQL. When using a stored procedure, with the correct setup, you only have to authorize users to call the stored procedure and no longer grant them access on the individual objects.

Importance of Catalog Statistics for Dynamic SQL

Catalog statistics influence the access of static as well as dynamic SQL. Here we briefly point out why they are especially important for the performance of dynamic SQL.

There are a few important considerations:

On the other hand, it is not just the statistics themselves that should be considered important. During the prepare process, DB2 has to retrieve these statistics from the DB2 catalog. Therefore the performance of queries accessing catalog tables is equally important. Make sure your catalog is in good shape when you use dynamic SQL by executing REORGs of catalog table spaces when appropriate.

Examples of Interactive Dynamic SQL Programs

DB2 comes with a set of programs to process dynamic SQL which are used as a productivity aid. The table below summarizes how they can be used.

 

Program name Language Purpose and types of statements processed Shipped as
DSNTEP2 PL/I All statements (SELECT and non-SELECT) Source and object module
DSNTIAD Assembler Processes all non-SELECT statements Source
DSNTIAUL Assembler Unload data. Also processes all non-SELECT statements1 Source
SPUFI PL/X All statements (SELECT and non-SELECT) Load modules

1 Note that you can also use the REORG UNLOAD EXTERNAL and UNLOAD (Version 7) utility to unload data. Those usually provide much better performance than the sample DSNTIAUL program.

General Recommendations on the Use of Dynamic SQL

These are some recommendations on the use of dynamic SQL: