< Day Day Up > |
Stored procedures are specialized programs that are executed under the control of the relational database management system. You can think of stored procedures as similar to other database objects such as tables, views, and indexes because they are managed and controlled by the RDBMS. But you can think of stored procedures as similar to application programs, too, because they are coded using a procedural programming language. Depending on the particular implementation, stored procedures can also physically reside in the DBMS. However, a stored procedure is not "physically" associated with any other object in the database. It can access and/or modify data in one or more tables. Basically, you can think of stored procedures as "programs" that "live" in the RDBMS. A stored procedure must be directly and explicitly invoked before it can be executed. In other words, stored procedures are not event-driven. Contrast this concept with the concept of triggers, which are event-driven and never explicitly called. Instead, triggers are automatically executed (sometimes referred to as " fired ") by the RDBMS as the result of an action. Stored procedures are never automatically invoked. DB2 has provided stored procedure support since V4, and IBM continues to enhance the functionality of stored procedures with each successive DB2 release. The major motivating reason for stored procedure support is to move SQL code off the client and on the database server. Implementing stored procedures can result in less overhead than alternative development methods because one client request can invoke multiple SQL statements. DB2's Stored Procedure ImplementationDB2's implementation of stored procedures is a bit different from the stored procedure support available using other DBMS products. For example, both Microsoft SQL Server and Oracle require you to code stored procedures using procedural extensions to SQL: Microsoft provides Transact-SQL, and Oracle provides PL/SQL. DB2, on the other hand, enables you to write stored procedures using traditional programming languages. You can use any LE/370-supported language to code stored procedures. The supported languages are Assembler, C, C++, COBOL, OO COBOL, and PL/I. Additionally, IBM provides support for REXX and Java stored procedures, as well as their own extended SQL Procedure Language (procedural SQL) for developing stored procedures. A description of the procedural SQL option is provided in the "Procedural SQL" section later in this chapter. NOTE The language of the calling program can be different from the language used to write the stored procedure. For example, a COBOL program can CALL a C stored procedure.
CAUTION Because it will become difficult to manage and control what exactly is being committed (or rolled back), it is best not to issue COMMIT and ROLLBACK statements in your stored procedures if you can avoid it. Additionally, a stored procedure can issue DB2 commands and IFI (Instrumentation Facility Interface) calls. Stored procedures can access flat files, VSAM files, and other files, as well as DB2 tables. Additionally, stored procedures can access resources in CICS, IMS, and other MVS address spaces, but no commit coordination exists between DB2 and the other resources. NOTE DB2 stored procedures can connect to an IMS DBCTL or IMS DB/DC system using IMS Open Database Access (ODBA) support. The stored procedure can issue DL/I calls to access IMS databases. IMS ODBA supports the use of OS/390 RRSAF for syncpoint control of DB2 and IMS resources. Stored procedures that use ODBA can run only in WLM-established stored procedures address spaces, not a DB2-established address space. DB2 stored procedures run under the control of the Work Load Manager (WLM). WLM effectively fences off user-developed code from running in DB2 address spaces with IBM-developed code. This layer of protection is useful to prohibit a stored procedure from causing an entire DB2 subsystem to fail. NOTE When stored procedures were first introduced, a DB2-managed address space known as the stored procedure address space , or SPAS for short, was used to fence stored procedures from DB2. Although the SPAS is still supported as of V8, IBM is phasing it out and it will be eliminated completely in a future version of DB2. Start migrating your DB2-managed stored procedures from the SPAS to WLM as soon as possible. To further clarify, you cannot create a DB2-managed stored procedure in V8. V8 only tolerates existing DB2 managed stored procedures. Even altering an existing SP will force it to be WLM managed. Why Use Stored Procedures?DB2 stored procedures have many potentially time-saving and useful applications. The major uses can be broken down into six categories: reuseability, consistency, data integrity, maintenance, performance, and security, as described here.
Stored procedures provide a myriad of other useful benefits including:
|
< Day Day Up > |