Fundamentals of DB2 SQL Procedures: Philip K. Gunning, DGI
Fundamentals of DB2 SQL Procedures: Philip K. Gunning, DGI
Anaheim, CA
Outline
Background Installation and Preparation Setup Development Center (SPB, et al) Building SQL Procedures Case Study Performance Considerations Summary
IBM Corporation 2002
Background
Background
First appeared in DB2 UDB V7.1
DB2 for OS/390 V6.1
Other Database Vendors have had their own proprietary stored procedure languages for some time Search on YAHOO! found 78,000 hits on Oracle Stored Procedures Search on YAHOO! Found 31,600 hits on Sybase Stored Procedures
IBM Corporation 2002
Background
Search on YAHOO! Found 25,200 hits on DB2 Stored Procedures Search on YAHOO! Found 44,500 hits on Transact SQL Search on YAHOO! Found 11 hits on PL/SQL
Background
Many applications have been written in their entirety in a stored procedure language
Flexibility Enable Rapid Application Development Minor learning curve since similar to SQL
Need not be set if environment variables for compiler set as SYSTEM variables
IBM Corporation 2002
http://www7b.boulder.ibm.com/dmdd/librar y/techarticle/0204milligan/0204milligan.ht ml
IBM Corporation 2002
SQL
Procedure created via CREATE PROCEDURE COMMAND CREATE PROCEDURE update_salary ( IN employee_number CHAR(6), IN rating INT )
Attendee Notes
Create procedure may fail in the precompile or compile stage. DB2 will create a log file that contains error messages. This log will be placed in the /sqlproc/db_name/schema_name/tmp or \sqlproc\db_name\schema_name\tmp directory on Windows.
SQL
SQL-Procedure Body keyword of CREATE PROCEDURE statement specifies the SQL statement that is the body of the SQL Procedure Multiple SQL Procedure statements can be specified within a compound statement
SQL
CREATE PROCEDURE bump_salary (IN deptnumber SMALLINT) LANGUAGE SQL BEGIN DECLARE SQLSTATE CHAR(5); DECLARE v_salary DOUBLE; DECLARE v_id SMALLINT; DECLARE v_years SMALLINT; DECLARE at_end INT DEFAULT 0; DECLARE not_found CONDITION FOR SQLSTATE '02000'; DECLARE C1 CURSOR FOR SELECT id, CAST(salary AS DOUBLE), years FROM staff WHERE dept = deptnumber; DECLARE CONTINUE HANDLER FOR not_found SET at_end = 1;
OPEN C1; FETCH C1 INTO v_id, v_salary, v_years; WHILE at_end = 0 DO CASE WHEN (v_salary < 2000 * v_years) THEN UPDATE staff SET salary = 2150 * v_years WHERE id = v_id; WHEN (v_salary < 5000 * v_years) THEN CASE WHEN (v_salary < 3000 * v_years) THEN UPDATE staff SET salary = 3000 * v_years WHERE id = v_id; ELSE UPDATE staff SET salary = v_salary * 1.10 WHERE id = v_id; END CASE; ELSE UPDATE staff SET job = 'PREZ' WHERE id = v_id; END CASE; FETCH C1 INTO v_id, v_salary, v_years; END WHILE; CLOSE C1; END @
Copyright IBM Corp. Sample Procedure
SQL
Attendee Notes
The procedure body consists of a procedure-name, that names the procedure being defined. In dynamic SQL, CURRENT SCHEMA is used as a qualifier for an unqualified name and the QUALIFIER bind option for static SQL. The unqualified name with the number of parameters is unique within its schema, but does not need to be unique across schemas. This is know as overloading in other languages. IN, OUT, or INOUT identifies the parameters of the procedure and specifies the mode, name and data type of each parameter. Specific-name is used for commenting on or dropping the procedure. It can never be used to invoke the procedure. Dynamic results is an upper bound for returned result sets. Contains, Reads, and Modifies SQL DATA LANGUAGE SQL FOR SQL PROCEDURES SQL PROCEDURE BODY specifies the SQL statement that is the body of the SQL procedure.
IBM Corporation 2002
SQL
CALL statement invokes stored procedure
Has a procedure signature which is a combination of schema, procedure name and number of parameters
SQL
If result sets are required then RETURN to CALLER or RETURN TO CLIENT must be specified
RETURN TO CALLER results are only visible to program at previous nesting level RETURN TO CLIENT results are visible only if procedure was invoked from a set of nested procedures, function or method invalidates this Only visible to client application that made the call
IBM Corporation 2002
SQL
See Appendix N, DB2 UDB SQL Reference for complete list of SQL statements allowed in SQL routines See DB2 for Z/OS SQL Reference for that platform
SQL
Condition Handlers
Control passed to condition handler if a condition is raised and you have declared a handler for the respective condition If no handler, DB2 passes control to next statement in procedure body
SQLCODE and SQLSTATE will contain corresponding values if they were declared
Attendee Notes
If a statement raises an SQLWARNING or NOTFOUND condition, DB2 passes control to the handler you declared. If a statement raises an SQLEXCEPTION condition control is passed to the handler for that specific condition.
SQL
SIGNAL and RESIGNAL Statements
can be used to explicitly raise a specific SQLSTATE
CALL is now a compiled statement and can be run from a command line
Development Tools
Development Center
SQL Procedure
SQL Procedure
SQL Procedure
Deploy
Deploy
Case Study
Texas Department of Public Safety Combined 8 legacy applications into 1 using all SQL Procedures Started out on DB2 UDB V7.1
JAVA Stored Procedures Got proficient with SPB and development on Windows
Case Study
Developed concurrently on DB2 for Windows whilst connectivity to OS/390 was being established
TCP/IP DDF CICS Workload Manager (WLM) WLM Stored Procedures
Case Study
After DB2 V6.1 and OS/390 configured developed over 300 SQL Procedures Implemented Texas Crime Information Center application totally in SQL Procedures Credited flexibility and ease of development in streamlining the typical development timeline
IBM Corporation 2002
Case Study
Used DB2 Management Clients package DB2 Control Center DB2 Stored Procedure Builder DB2 Visual Explain
Case Study
Still did good Logical and Physical Design Extensive use of Identity Columns Developed good indexes via DB2 Explain and SQL Reviews Attaining sub-second response Continue to add new features/functionality using SQL Procedures
IBM Corporation 2002
Case Study
Lessons Learned
Want to be leading-edge but not necessarily bleeding-edge! Lots of time spent getting TCP/IP, DB2 for OS/390 V6.1, DSNTSMP stored procedure working along with WLM Ran into 32k limitation and had to use multiple stored procedures Starting/Stopping/Purging WLM procedures took some time to develop
IBM Corporation 2002
References
SC09-4848-00, Whats New, V8.1 DB2 UDB SQL Reference, Volume 1 and 2 SC09-4825-00, DB2 UDB Application Building Guide: Building and Running Applications V8.1 SC09-4827-00, DB2 Application Development Guide: Programming Server Applications
IBM Corporation 2002
References
DB2 UDB Command Reference, V8.1 http://www.ibm.com/software/data/db2/d ata/udb/ad ftp://ftp.software.ibm.com/ps/db2/info SC26-9944-01 DB2 for Z/OS SQL Reference SC26-9933-01 DB2 for Z/OS Application Programming and SQL Guide
IBM Corporation 2002
References
http://www.ibm.com/software/data/db2/os3 90/spb
References
F15 Thanks!
Fundamentals of DB2 SQL Procedures
[email protected]
Anaheim, CA