buku chapter 2

26
11 C HAPTER 2 Basic SQL Procedure Structure In this chapter, you will learn The fundamental structure of an SQL procedure The various clauses for the CREATE PROCEDURE statement The structure of the procedure body The statements that can be coded in the procedure body Stored procedures are used to encapsulate multiple SQL statements with flow logic. They are data- base objects that serve as sub-routines to applications. SQL procedures are defined in a database using the CREATE PROCEDURE statement. This chapter introduces the syntax of the CREATE PROCE- DURE statement and uses examples to illustrate how to create simple SQL procedures. The CREATE PROCEDURE Statement The CREATE PROCEDURE statement defines a procedure in the database. The clauses define the name and parameters as well as the procedure body consisting of one or more SQL PL state- ments. Figure 2.1 describes the syntax of the CREATE PROCEDURE statement and the clauses that apply to SQL procedures. NOTE We discuss the differences in the CREATE PROCEDURE statement for iSeries and zSeries platforms at the end of this chapter. Many clauses are available for the CREATE PROCEDURE statement; however, it is often appropriate to just use the defaults.

Upload: afif-nandya

Post on 19-Nov-2015

15 views

Category:

Documents


2 download

DESCRIPTION

a good book to everyone

TRANSCRIPT

  • 11

    C H A P T E R 2

    Basic SQL ProcedureStructure

    In this chapter, you will learn

    The fundamental structure of an SQL procedure

    The various clauses for the CREATE PROCEDURE statement

    The structure of the procedure body

    The statements that can be coded in the procedure body

    Stored procedures are used to encapsulate multiple SQL statements with flow logic. They are data-base objects that serve as sub-routines to applications. SQL procedures are defined in a databaseusing the CREATE PROCEDURE statement. This chapter introduces the syntax of the CREATE PROCE-DURE statement and uses examples to illustrate how to create simple SQL procedures.

    The CREATE PROCEDURE Statement The CREATE PROCEDURE statement defines a procedure in the database. The clauses define thename and parameters as well as the procedure body consisting of one or more SQL PL state-ments. Figure 2.1 describes the syntax of the CREATE PROCEDURE statement and the clauses thatapply to SQL procedures.

    NOTEWe discuss the differences in the CREATE PROCEDURE statement for iSeries and zSeriesplatforms at the end of this chapter.Many clauses are available for the CREATE PROCEDURE statement; however, it is oftenappropriate to just use the defaults.

    Janmohamed_CH02.qxd 10/15/04 6:07 PM Page 11

  • >>-CREATE PROCEDURE--procedure-name----------------------------->

    >--+----------------------------------------------------+--*---->

    '-(--+------------------------------------------+--)-'

    | .-,------------------------------------. |

    | V .-IN----. | |

    '---+-------+--parameter-name--data-type-+-'

    +-OUT---+

    '-INOUT-'

    >--+-------------------------+--*------------------------------->

    '-SPECIFIC--specific-name--'

    .-DYNAMIC RESULT SETS 0--------. .-MODIFIES SQL DATA-.

    >--+------------------------------+--*--+-------------------+--->

    '-DYNAMIC RESULT SETS--integer-' +-CONTAINS SQL------+

    '-READS SQL DATA----'

    .-NOT DETERMINISTIC-. .-CALLED ON NULL INPUT-.

    >--*--+-------------------+--*--+----------------------+--*----->

    '-DETERMINISTIC-----'

    .-INHERIT SPECIAL REGISTERS-. .-OLD SAVEPOINT LEVEL-.

    >--+---------------------------+--*--+---------------------+---->

    '-NEW SAVEPOINT LEVEL-'

    .-LANGUAGE SQL-. .-EXTERNAL ACTION----.

    >--*--+--------------+--*--+--------------------+--*------------>

    '-NO EXTERNAL ACTION-'

    >--+------------------------------+--*-------------------------->

    '-PARAMETER CCSID--+-ASCII---+-'

    '-UNICODE-'

    >--| SQL-procedure-body |-------------------------------------->>-+---------+--BEGIN----+-------------+------------------------>

    '-label:--' '-ATOMIC------'

    >-----+-----------------------------------------------+--------->

    | .-----------------------------------------. |

    | V | |

    '-----+-| SQL-variable-declaration |-+---;---+--'

    +-| condition-declaration |----+

    '-| return-codes-declaration |-'

    >--+----------------------------------+------------------------->

    | .------------------------------. |

    | V | |

    '---| statement-declaration |--;-+-'

    20 Chapter 2 Basic SQL Procedure Structure

    Janmohamed_CH02.qxd 10/15/04 6:07 PM Page 20

  • >-----+-------------------------------------+------------------->

    | .-------------------------------. |

    | V | |

    '----DECLARE-CURSOR-statement--;---+--'

    >-----+------------------------------------+-------------------->

    | .------------------------------. |

    | V | |

    '----| handler-declaration |--;---+--'

    .-------------------------------.

    V |

    >--------SQL-procedure-statement--;---+---END--+--------+------>>-CREATE PROCEDURE---procedure-name --------------------------->

    >--+----------------------------------------------------------+->

    '--(--+----------------------------------------------+--)--'->

    | .-,---------------------------------------. |

    | V .-IN----. | |

    '----+-------+---parameter-name--data-type---+-'

    +-OUT---+

    '-INOUT-'

    >--LANGUAGE SQL-----------------------------------------------+->

    .-NOT DETERMINISTIC--. .-MODIFIES SQL DATA--.

    >--+--------------------+-----+--------------------+------------>

    '-DETERMINISTIC------' +-CONTAINS SQL-------+

    '-READS SQL DATA-----'

    .-CALLED ON NULL INPUT-.

    >--+----------------------+-----+--------------------------+---->

    '-SPECIFIC--specific-name--'

    .-DYNAMIC RESULT SETS 0---------. .-FENCED-----.

    >--+-------------------------------+-----+------------+--------->

    '-DYNAMIC RESULT SETS--integer--' '-NOT FENCED-'

    .-COMMIT ON RETURN NO--. .-OLD SAVEPOINT LEVEL-.

    >--+----------------------+-----+---------------------+--------->

    '-COMMIT ON RETURN YES-' '-NEW SAVEPOINT LEVEL-'

    .-INHERIT SPECIAL RESGISTERS-.

    >--+----------------------------+------------------------------->

    >--+---------------------+---SQL-routine-body------------------>>-CREATE PROCEDURE--------------------------------------------->

    >----procedure-name--(--+----------------------------------------------+---)->

    | .-,---------------------------------------. |

    | V .-IN----. | |

    '----+-------+---parameter-name--data-type---+-'

    +-OUT---+

    '-INOUT-'

    .-DYNAMIC RESULT SETS 0--------. .-MODIFIES SQL DATA-.

    >--+------------------------------+--*--+-------------------+--->

    '-DYNAMIC RESULT SETS--integer-' +-CONTAINS SQL------+

    '-READS SQL DATA----'

    .-NOT DETERMINISTIC-. .-CALLED ON NULL INPUT-.

    >--*--+-------------------+--*--+----------------------+--*----->

    '-DETERMINISTIC-----'

    .-INHERIT SPECIAL REGISTERS-.

    >--+---------------------------+--*-------LANGUAGE SQL---------->

    '-DEFAULT SPECIAL REGISTERS-'

    .-FENCED-. .-NO DBINFO-.

    >--+------------------------------+---+--------+---+-----------+->

    '-PARAMETER CCSID--+-ASCII---+-'

    '-EBCDIC -'

    '-UNICODE-'

    .-NO COLLID ------------.

    >--+-----------------------+---+-------------------------------+->

    '-COLLID Collection-Id -' '-WLM ENVIRONMENT--+--name----+-'

    '-(name,*)-'

    .-ASUTIME NO LIMIT ------. .-STAY RESIDENT NO--.

    >--+------------------------+----+-------------------+----------->

    '-ASUTIME LIMIT integer--' +-STAY RESIDENT YES-'

    .-PROGRAM TYPE MAIN--. .-SECURITY DB2-----.

    DB2 UDB for zSeries Considerations 33

    Janmohamed_CH02.qxd 10/15/04 6:07 PM Page 33

  • >--+--------------------+----+------------------+---------------->

    '-PROGRAM TYPE SUB---' +-SECURITY USER ---+

    '-SECURITY DEFINER-'

    .-COMMIT ON RETURN NO---.

    >--+-------------------------------+----+-----------------------+->

    '-RUN OPTIONS run time options--' '-COMMIT ON RETURN YES--'

    .-STOP AFTER SYSTEM DEFAULT FAILURES --.

    >--+--------------------------------------+---------------------->

    +-STOP AFTER integer FAILURES ---------+

    '-CONTINUE AFTER FAILURE---------------'

    >-----| SQL-procedure-body |------------------------------------>