< Day Day Up > |
The major difference between DB2's stored procedure support and the other RDBMS vendors is the manner in which the stored procedure is coded. As I mentioned at the beginning of this chapter, other popular RDBMS products require procedural dialects of SQL for stored procedure creation. Oracle uses PL/SQL and Sybase, and Microsoft SQL Server uses Transact SQL. Each of these languages is proprietary, and they cannot interoperate with one another. As of DB2 V6, IBM supports a procedural dialect of SQL based on the ANSI standard. The IBM DB2 version of procedural SQL is called SQL procedures language , or SPL for short. NOTE SQL/PSM is the ANSI standard specification for developing stored procedures and routines using SQL. PSM is an acronym for Persistent Stored Modules. IBM's implementation of its SQL Stored Procedure Language is based on SQL/PSM, but is not a complete implementation of the ANSI SQL/PSM standard. But what is procedural SQL? One of the biggest benefits derived from SQL (and relational technology in general) is the capability to operate on sets of data with a single line of code. By using a single SQL statement, you can retrieve, modify, or remove multiple rows. However, this capability also limits SQL's functionality. A procedural dialect of SQL eliminates this drawback through the addition of looping, branching, and flow of control statements. Procedural SQL has major implications on database design. Procedural SQL will look familiar to anyone who has ever written any type of SQL or coded using any type of programming language. Typically, procedural SQL dialects contain constructs to support looping ( WHILE or REPEAT ), exiting ( LEAVE ), conditional processing ( IF...THEN...ELSE ), blocking ( BEGIN...END ), and variable definition and use. IBM's SQL Procedure LanguageStored procedure language for creating SQL stored procedures was added after the general availability of DB2 V6. SQL stored procedures are like other stored procedures in that the SQL stored procedure must have a name and a schema, as well as the definition of the stored procedure characteristics and the actual code for the stored procedure. The code, however, is written in SQL alone ”no 3GL program is required. SQL stored procedures differ from external stored procedures in the way that the code is defined. SQL stored procedures include the actual SQL procedural source code in the CREATE PROCEDURE statement, whereas external stored procedures specify only the definition of the stored procedure in the CREATE PROCEDURE statement. The actual code of an external stored procedure is developed independently and is not included in the CREATE statement. SQL stored procedures are developed entirely in IBM's SQL procedures language but must be converted to C before they can be executed. This process is described later in this chapter in the section titled "Creating SQL Stored Procedures." The actual SQL code in the SQL stored procedure is referred to as the body of the SQL stored procedure. The body of an SQL stored procedure can include most valid SQL statements, but also extended, procedural SQL statements. The procedure body consists of a single simple or compound statement. The following statements can be included in an SQL stored procedure body.
NOTE When coding a compound statement, you must code the component statements in the following specific order:
Sample SQL Stored ProceduresThe following SQL code implements an SQL stored procedure that accepts an employee number and a rate as input. The stored procedure raises the salary of the specified employee by the specified rate. However, using an IF statement, the stored procedure also checks to make sure that no raise exceeds 50%. CREATE PROCEDURE UPDATE_SALARY (IN EMPLOYEE_NUMBER CHAR(10), IN RATE DECIMAL(6,2)) LANGUAGE SQL WLM ENVIRONMENT SAMP1 COMMIT ON RETURN YES IF RATE <= 0.50 THEN UPDATE EMP SET SALARY = SALARY * RATE WHERE EMPNO = EMPLOYEE_NUMBER; ELSE UPDATE EMP SET SALARY = SALARY * 0.50 WHERE EMPNO = EMPLOYEE_NUMBER; END IF Another sample stored procedure follows : CREATE PROCEDURE PROC1(OUT NOROWS INT) LANGUAGE SQL BEGIN DECLARE var_firstnme VARCHAR(12); DECLARE var_midinit CHAR(1); DECLARE var_lastname VARCHAR(15); DECLARE at_end INT DEFAULT 0; DECLARE not_found CONDITION FOR '02000' DECLARE cempname CURSOR FOR SELECT FIRSTNME, MIDINIT, LASTNAME FROM EMP ORDER BY LASTNAME; DECLARE CONTINUE HANDLER FOR not_found SET NOROWS=1; OPEN cempname; FETCH cempname INTO var_firstnme, var_midinit, var_lastname; CLOSE cempname; END This SQL stored procedure declares a cursor on the EMP table and fetches a row from the cursor. The condition handler is used to handle the row-not-found condition. You could code a loop construct to fetch all rows from a cursor until no more rows are found. For example, . . . fetch_loop: REPEAT FETCH cempname INTO var_firstnme, var_midinit, var_lastname; UNTIL SQLCODE <> 0 END REPEAT fetch_loop . . . Of course, a similar effect could be achieved using the LOOP construct with a LEAVE statement. Creating SQL Stored ProceduresThere are three steps to creating SQL stored procedures:
There are two different ways for you to accomplish these three steps to create an SQL procedure:
NOTE
Using JCL to Create SQL Stored ProceduresUse the following steps to prepare an SQL procedure using JCL. This JCL is similar to the JCL used for program preparation presented in Chapter 13, "Program Preparation," with an additional step to generate C source code. First, preprocess the CREATE PROCEDURE statement using the DSNHPSM program. The output from this step is
Next, precompile the generated C language program. This produces a DBRM and modified C language source statements. Ensure that the DBRM name is the same as the name of the load module for the SQL procedure. The third step is to compile and link-edit the modified C source statements, producing an executable C language program. The default name for the C language program is the first eight bytes of the SQL procedure name. Finally, BIND the DBRM into a package and define the stored procedure to DB2. Using DSNTPSMP to Create SQL Stored ProceduresDSNTPSMP , also known as the SQL procedure processor, is a REXX stored procedure that you can use to prepare an SQL procedure for execution. You can also use DSNTPSMP to perform selected steps in the preparation process or delete an existing SQL procedure. The following sections contain information on invoking DSNTPSMP . DSNTPSMP can be executed only by issuing a CALL statement inside an application program or through DB2 Stored Procedure Builder or DB2 Development Center. Before you can run DSNTPSMP , you need to ensure that the appropriate PTFs and APARs have been applied to DB2, install the REXX language support feature, and code a program that issues a CALL statement for DSNTPSMP . Use the Sample Programs Provided by IBMIBM provides quite a few sample programs and jobs to assist you in developing SQL stored procedures. The samples can be found in the SDSNSAMP data set. Examine these for examples of how to implement effective DB2 SQL stored procedures. The SQL stored procedure samples that ship with DB2 are listed in Table 15.1. Table 15.1. SQL Stored Procedure Samples
The SQL Procedures Language "Catalog"SQL procedures language requires two additional supportive tables, similar to DB2 Catalog tables. These tables contain information such as the source code of the SQL stored procedure code and the options used to develop the SQL stored procedure. The two tables are SYSIBM.SYSPSM and SYSIBM.SYSPSMOPTS . SYSIBM.SYSPSM holds the source code for SQL stored procedures. The table contains one or more rows for each SQL stored procedure prepared by DSNTPSMP , Stored Procedure Builder, or DB2 Development Center. If the SQL stored procedure consists of more than 3,800 bytes, more than one row is required to hold the source code for the SQL procedure. Refer to Table 15.2 for a definition of SYSIBM.SYSPSM . Table 15.2. SYSIBM.SYSPSM (SQL Procedure Source Table)
The SYSIBM.SYSPSM table has two indexes defined on it: DSNPSMX1 ( nonunique ) and DSNPSMX2 (unique). SYSIBM.SYSPSMOPTS holds the program preparation options for SQL stored procedures. The table contains one row for each SQL stored procedure prepared by DSNTPSMP , Stored Procedure Builder, or DB2 Development Center. Refer to Table 15.3 for a definition of SYSIBM.SYSPSMOPTS . The SYSIBM.SYSPSMOPTS table has one unique index, DSNPSMOX1 defined on it. This index must be defined before DSNTPSMP is executed. Table 15.3. SYSIBM.SYSPSMOPTS (SQL Procedure Options Table)
The Benefits of Procedural SQLThe most useful procedural extension to SQL is the addition of procedural flow control statements. Flow control within procedural SQL is handled by typical programming constructs that you can mix with standard SQL statements. These typical constructs enable programmers to
The addition of procedural commands to SQL provides a more flexible environment for application developers. Often, major components of an application can be delivered using nothing but SQL. You can code stored procedures and complex triggers using procedural SQL, thereby reducing the amount of host language (COBOL, C, Visual Basic, and so on) programming required. Additionally, when stored procedures can be written using just SQL, more users will be inclined to use these features. DB2 requires stored procedures to be written in a host language. This requirement may scare off many potential developers. Most DBAs I know avoid programming ( especially in COBOL) like the plague. In addition to SQL stored procedures, procedural SQL extensions also enable more complicated business requirements to be coded using nothing but SQL. For example, an independent SQL statement cannot examine each row of a result set during processing. Procedural SQL can accomplish this task quite handily using cursors and looping. The Drawbacks of Procedural SQLThe biggest drawback to procedural SQL is that it is late getting into the ANSI standard. Although DB2's stored procedure support is based on the ANSI SQL3 standard, other DBMS vendors support different flavors of procedural SQL because they were developed before the ANSI standard. If your shop has standardized on one particular DBMS or does not need to scale applications across multiple platforms, you may not have this problem. But, then again, how many shops does this description actually describe? Probably not very many! The bottom line is that scalability will suffer when applications are coded using non- standard extensions ”such as procedural SQL. Recoding applications that were designed to use stored procedures and triggers written using procedural SQL constructs is a non-trivial task. If an application needs to be scaled to a platform which uses a DBMS that does not support procedural SQL, a complete rewrite is exactly what must be done. Performance drawbacks can be realized when using procedural SQL if the developer is not careful. For example, improper cursor specification can cause severe performance problems. Of course, this problem can happen just as easily when cursors are used inside a host language. The problem is more inherent to application design than it is to procedural SQL. One final drawback is that even procedural SQL dialects are not computationally complete. Most dialects of procedural SQL lack programming constructs to control the users' screens and mechanisms for data input/output (other than to relational tables). |
< Day Day Up > |