Chapter -09 Creating Procedures 00
Objectives
After competing this lesson,you should be able to do the following:
- Identify the benefits of modualarized and layered subprogram design.
- Create and call procedures.
- Use formal and actual parameters
- Use positional,named ,or mixed notation for passing parameters.
- Identify the available parameter-passing modes.
- Handle exceptions in procedures.
- Remove a procedure
- Display the prcedures`s information.
Lesson Agenda
- Using a modularized and layered subprogram desing and identifying the benefits of subprograms.
- Working with procedures
- -Creating and calling procedures
- -Identifying the available parameter-passing modes
- -Using formal and actual parameters
- -Using positional,named,or mixed notation
- Handing execptions in procedures,removing a procedure,and displaying the procedures`s information.
Creating a Modularized Subprogram Desgin
Modularize code into subprograms.
1、Locate code sequences repeated more than onec.
2、Create subprogram P containing the repeated code.
3、Modify original code to invoke the new subprogram.
Creating a Layered Sumprogram Desgin
Create subprogram layers for you application.
- Data access subprogram layer with SQL logic.
- Business logic subprogram layer,which may or may not use the data access layer.
Modularizing Development with PL/SQL Blocks
- PL/SQL is a block-structed language.The PL/SQL code block helps modularize code by using:
- -Anonymous blocks
- -Procedures and Functions
- -Packages
- -Database triggers
- The benefits of using modular program constructs are:
- -Easy maintenance
- -Improved data security and integrity
- -Improved performance
- -Improved code clarity
Anonymous Blocks:Overview
Anonymous blocks:
- Form the basic PL/SQL block structure
- Initiate PL/SQL processing tasks from applications
- Can be nested within the executable section of any PL/SQL block.
[DECLARE -- Declarationg Section (Optional) variable declarations;...] BEGIN --Executable Section(Mandatory) SQL or PL/SQL statements; [EXCEPTION --Exception Section (Optional) WHEN exception THEN statements;] END; --End of BLock(Mandatory)
PL/SQL Run-time Architecture
What Are PL/SQL Subprograms?
- A PL/SQL subprogram is a named PL/SQL block that can be called with a set fo prameters.
- You can declare and define a subprogram within either a PL/SQL block or another subprogram.
- A subprogram consists of a specification and a body
- A subprogram can be a procedure or a function.
- Typically,you use a procedure to perform an action and a fuction to compute and return a value.
- Subprograms can be grouped into PL/SQL packages.
The benefits of Using PL/SQL Subprograms
Differences Between Anonymous Blocks and Subprograms
Anonymous Blocks | Subprograms |
Unnamed PL/SQL blocks | Named PL/SQL blocks |
Compiled every time | Compiled only once |
Not stored in the database | Stored in the database |
Cannot be invoked by other applicatons | Names and,therefore,can be invoked by other applicaitons |
Do not return values | Subprograms called functions must return values. |
Cannot take parameters | Can take parameters |