This lesson provides an overview of how to create and use PL/SQL program units and subprograms using Oracle Procedure Builder.. Integration D PL/SQL plays a central role to both the Orac
Trang 1Overview of PL/SQL
18
Trang 2Schedule: Timing Topic
20 minutes Lecture
20 minutes Total
Trang 3This lesson provides an overview of how to create and use PL/SQL program units and subprograms using Oracle Procedure Builder.
At the end of this lesson, you should be able to
D Determine the benefits of accessing the Oracle7 database with PL/SQL
D Describe basic PL/SQL program constructs
D Describe the Oracle Procedure Builder tool
Trang 4Technical Note:
Examples of procedural programming languages are C and FORTRAN, which are considered 3GLs Procedural languages afford direct control over processing; the programmer must encode exactly how a particular operation
is to be done
SQL is non-procedural and a 4GL Non-procedural languages are generally easier to use, but at the expense of processing control The programmer states the database operation that needs to be done, and the Oracle7 Server determines how to do it
PL/SQL is a hybrid language that uses SQL to access the database, but allows explicit processing control on top of SQL Therefore, PL/SQL affords the best of both worlds
Trang 5What Is PL/SQL?
PL/SQL (Procedural Language/SQL) is an extension to SQL, incorporating many of
the design features of programming languages of recent years It allows the data manipulation and query statements of SQL to be included within block-structured and procedural units of code, making PL/SQL a powerful transaction processing
language
Benefits from PL/SQL
You can take advantage of the procedural capabilities of PL/SQL, which are
unavailable in SQL
Modularize Program Development
D Group logically related statements within blocks
D Nest sub-blocks inside larger blocks to build powerful programs
D Break down a complex problem into a set of manageable, well-defined, logical modules, and implement them with blocks
Declare Identifiers
D Declare variables, constants, cursors, and exceptions, and then use them in SQL and procedural statements
D Declare variables belonging to simple and composite datatypes
D Declare variables dynamically based upon the data structure of tables and
columns in the database
Program with Procedural Language Control Structures
D Execute a sequence of statements conditionally
D Execute a sequence of statements iteratively in a loop
D Process individually the rows returned by a multiple-row query with an explicit cursor
D Combine PL/SQL with Oracle tools, such as Developer/2000 Forms, to group associated commands together to control execution
Handle Errors
D Process Oracle7 Server errors with exception handling routines
D Declare user-defined error conditions and process them with exception handling routines
Continued
Trang 7Overview continued
Benefits of PL/SQLĊcontinued
Portability
D Since PL/SQL is native to Oracle, you can move programs to any host
environment that supports Oracle and PL/SQL
D You can also move code between the Oracle7 Server and your application
environment using Procedure Builder’s drag-and-drop capabilities
Integration
D PL/SQL plays a central role to both the Oracle7 Server (through stored
procedures, database triggers, and packages) and Oracle development tools
(through Developer/2000 component triggers)
D Variables and datatypes in PL/SQL and SQL are compatible Therefore, PL/SQL bridges the gap between convenient access to database technology and the need for procedural programming capabilities
Improve Performance
D PL/SQL can improve the performance of an application The benefits differ
depending upon the execution environment
D PL/SQL groups SQL statements together within a single block and sends the entire block to the server in a single call, therefore reducing network traffic
Trang 9PL/SQL Block Structure
A basic PL/SQL block of code can contain up to three parts The order in which the
sections of the blocks are written is shown below
Declarative Contains all variables, constants, cursors,
and user-defined exceptions that will be referenced within the Executable section
Optional
Executable Contains SQL statements to manipulate
data in the database and PL/SQL statements to manipulate data in the block
Mandatory
Exception Handling Specifies the actions to perform when
errors and abnormal conditions arise within the Executable section
Optional
Note: In PL/SQL, an error or warning is called an exception.
Section keywords DECLARE, BEGIN, and EXCEPTION are not followed by semicolons However, END and all other PL/SQL statements do require a semicolon
to terminate the statement You can string statements together on the same line However, this method is not recommended for clarity or editing
Trang 10Class Management Note:
In the classroom, the database uses PL/SQL version 2.3, while Procedure Builder uses PL/SQL version 1.5 Some apparent differences are datatypes, comparison operators, and composite datatypes
Trang 11PL/SQL Block Structure continued Every unit of PL/SQL comprises one or more blocks These blocks can be entirely separate or nested one within another Therefore, one block can represent a small part
of another block, which in turn can be part of the whole unit of code
PL/SQL Program Constructs
The following list outlines a variety of different PL/SQL program constructs using
the basic PL/SQL block They are available based on the environment where they are executed
Program
Anonymous Block Unnamed PL/SQL block that is
embedded within an application or
is issued interactively
All PL/SQL environments
Stored Procedure or
Function
Named PL/SQL block that can accept parameters and can be invoked repeatedly
Oracle7 Server with Procedural Extension
Application
Procedure or
Function
Named PL/SQL block that can accept parameters and can be invoked repeatedly
Components of Developer/2000, for example Forms
Package Named PL/SQL module that
groups together related procedures, functions, and identifiers
Oracle7 Server with Procedural Extension
Database Trigger PL/SQL block that is associated
with a database table and is fired automatically
Oracle7 Server with Procedural Extension
Application Trigger PL/SQL block that is associated
with an application event and is fired automatically
Components of Developer/2000, for example Forms
Note: A function is similar to a procedure, except that a function must return a
value
Trang 13PL/SQL Block Structure continued
The two types of constructs covered in this course are anonymous blocks and
subprograms.
Anonymous Blocks
Anonymous blocks are unnamed blocks They are declared at the point in an
application where they are to be executed and are passed to the PL/SQL engine for execution at runtime You can embed an anonymous block within a precompiler program and within SQL*Plus or Server Manager Triggers in Developer/2000
components consist of such blocks
Subprograms
Subprograms are named PL/SQL blocks You can declare them either as procedures
or as functions Procedures perform actions, and functions return values
Developer/2000 components allow you to declare procedures and functions as part of the application (a form or report), and call them from other procedures, functions, and triggers
Trang 15The PL/SQL Environment
PL/SQL is not an Oracle product in its own right; it is a technology employed by the Oracle7 Server and by certain Oracle tools Blocks of PL/SQL are passed to and processed by a PL/SQL engine, which may reside within the tool or within the
Oracle7 Server The engine used depends on where the PL/SQL is being invoked
PL/SQL Engine at the Oracle7 Server
When you submit blocks from a Pro* program, user-exit, SQL*Plus, or Server
Manager, the PL/SQL engine in the Oracle7 Server processes them It breaks the SQL within the block into separate statements and sends them to the SQL Statement
Executor This means that a single transfer is required to send the block from the application to the Oracle7 Server, thus improving performance especially in a
client-server network Stored subprograms can be referenced by any number of applications connected to the database
PL/SQL in Oracle Tools
Many Oracle tools, including Developer/2000, have their own PL/SQL engine, which
is independent of the engine present in the Oracle7 Server
The engine filters out SQL statements and sends them individually to the SQL
Statement Executor in the Oracle7 Server It processes the remaining procedural statements in the Procedural Statement Executor, which is within the PL/SQL engine The Procedural Statement Executor processes data that is local to the application (that
is already inside the client environment, rather than the database) This reduces work sent to the Oracle7 Server and the number of memory cursors required
Note: Procedures and functions declared as part of a Developer/2000 application are
distinct from those stored in the database, although their general structure is the same Stored subprograms are database objects and are stored in the Data Dictionary They can be accessed by any number of applications
Application subprograms pass blocks to that application’s local PL/SQL engine Work is done at the application site, not at the server site
Trang 17About Procedure Builder
Oracle Procedure Builder is a tool you can use to create, execute, and debug PL/SQL programs used in your application tools, such as a form or a report, or on the Oracle7 Server through its graphical interface
Integrated PL/SQL Development Environment
Procedure Builder’s development environment contains a built-in editor for you to
create or edit subprograms You can compile, test, and debug your code.
Unified ClientĆServer PL/SQL Development
Application partitioning through Procedure Builder is available to assist you with distribution of logic between client and server Users can drag and drop a PL/SQL program unit between the client and the server
Class Management Note:
Oracle Procedure Builder is sold with Developer/2000 or as a separate product