Tài liệu developing a simple PL / SQL - Pdf 84

Developing a Simple PL/SQL
Block
21
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć2
Schedule: Timing Topic
60 minutes Lecture
30 minutes Practice
90 minutes Total
Developing a Simple PL/SQL Block 21Ć3
Objectives
In this lesson, you create a simple PL/SQL block after learning the various
elements that compose a block.
At the end of this lesson, you should be able to
D
Declare and use variables and constants in PL/SQL.
D
Assign new values to variables within the executable section.
D
Create and execute a named PL/SQL subprogram in Procedure Builder.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć4
Developing a Simple PL/SQL Block 21Ć5
Overview
A PL/SQL block is comprised of up to three sections: declarative (optional),
executable (required), and exception handling (optional). Only BEGIN and END
keywords are required. Each subprogram contains an additional section, the header
(required).
You can store and change values within a PL/SQL block by declaring and referencing
variables and other identifiers.
Handling Variables
D
Declare and initialize variables within the declaration section.

D
You can use naming conventions, for example v_name to represent a variable, and
c_name to represent a constant.
D
You have the option of assigning an initial value to variables, unless they are
NOT NULL.
D
Initialize the variable to an expression with the assignment operator (:=), or,
equivalently, with the DEFAULT reserved word; otherwise, variables are
initialized to NULL by default.
D
Declare at most one identifier per line.
For more information, see
PL/SQL User’s Guide and Reference, Release 2.3, “Datatypes.”
Technical Note:
Identifiers must not be longer than 30 characters. The first character must
be a letter, the remaining characters may be letters, numbers, or special
symbols.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć8
Class Management Note:
Mention that the NUMBER, CHAR, and VARCHAR2 datatypes have
subtypes. Additional base types are RAW, ROWID, and MLSLABEL
(Trusted Oracle).
Developing a Simple PL/SQL Block 21Ć9
Declaring Scalar Variables
PL/SQL supports three datatypes—scalar, composite, and reference—that you can
use for declaring variables, constants, and pointers.
Scalar Datatypes
A scalar datatype holds a single value and has no internal components. Scalar
datatypes can be classified into four categories: number, character, date and time, or

Class Management Note:
CMN for page 21-12.
The %TYPE attribute has some overhead involved in that a SELECT
statement is issued against the database to obtain the datatype. If the
PL/SQL code is in a client tool, the SELECT must be executed each time
the block is executed. If the PL/SQL code is a stored procedure, the column
or definition is stored as part of the P-code. However, if the table definition
changes, then a recompile is forced.
Developing a Simple PL/SQL Block 21Ć11
Declaring Scalar Variables
continued
Examples
Declare a variable to store the gender code (M or F).
v_gender CHAR(1);
Declare a variable to count the iterations of a loop and initialize the variable to 0.
v_count BINARY_INTEGER := 0;
Declare a variable to accumulate the total salary for a department and initialize the
variable to 0.
v_total_sal NUMBER(9,2) := 0;
Declare a variable to store the ship date of an order, and initialize the variable to one
week from today.
v_order_date DATE := SYSDATE + 7;
Declare a constant for the tax rate, which never changes throughout the PL/SQL
block.
c_tax_rate CONSTANT NUMBER(3,2) := 8.25;
Declare a flag to indicate whether a piece of data is valid or invalid, and initialize the
variable to TRUE.
v_valid BOOLEAN NOT NULL := TRUE;
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć12
Developing a Simple PL/SQL Block 21Ć13

v_minimum_balance v_balance%TYPE := 10;
...
A NOT NULL column constraint does not apply to variables declared using %TYPE.
Therefore, if you declare a variable using the %TYPE attribute using a database
column defined as NOT NULL, you can assign the NULL value to the variable.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć14
Developing a Simple PL/SQL Block 21Ć15
Declaring Composite Datatypes
A composite datatype contains internal components and is reusable. Two types of
composite datatypes are available in PL/SQL: TABLE and RECORD.
PL/SQL Table
D
A PL/SQL TABLE datatype is not the same as a database table.
D
A PL/SQL TABLE is similar to a one-dimensional array.
D
A PL/SQL TABLE must contain two components:
D
A primary key of datatype BINARY_INTEGER that indexes the PL/SQL
TABLE.
D
A column of a scalar datatype, which stores the PL/SQL TABLE elements.
D
A PL/SQL TABLE can increase dynamically because it is unconstrained.
Technical Note:
PL/SQL TABLES in PL/SQL version 2.3 can be created as a TABLE of
RECORDS. In the meantime, create a record where the fields of the
RECORD are PL/SQL TABLE types.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć16
Developing a Simple PL/SQL Block 21Ć17

continued
PL/SQL RECORD
D
A PL/SQL RECORD datatype is not the same as a row in a database table.
D
A PL/SQL RECORD is similar in structure to a record in a 3GL.
D
A PL/SQL RECORD must contain one or more components of any scalar,
RECORD, or PL/SQL TABLE datatype called fields. These uniquely named
fields can have different datatypes.
D
The PL/SQL RECORD allows you to treat this collection of fields as one logical
unit.
D
PL/SQL RECORDS are convenient for fetching a row of data from a table for
processing in a PL/SQL block.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć20
Developing a Simple PL/SQL Block 21Ć21
Declaring Composite Datatypes
continued
Declaring PL/SQL Records
1.
Declare a RECORD datatype.
2.
Declare a variable of that datatype.
Syntax
TYPE type_name IS RECORD
(field_name1 field_type
[NOT NULL {:=|DEFAULT} expr],
(field_name2 field_type

D
The number and datatypes of the underlying database columns may be unknown.
D
The number and datatypes of the underlying database columns may change at
runtime.
D
Useful when retrieving a row with the SELECT statement.
Example
Declare a variable to store the same information about a department as it is stored in
the S_DEPT table.
...
dept_record s_dept%ROWTYPE;
...
This declaration creates a record with the same field names and field datatypes as a
row in a table. DEPT_RECORD is a record. The fields are: DEPT_RECORD.ID,
DEPT_RECORD.NAME, and DEPT_RECORD.REGION_ID.
Class Management Note:
Draw on the board the structure of DEPT_RECORD.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć24
Developing a Simple PL/SQL Block 21Ć25
PL/SQL Block Syntax Rules
Because PL/SQL is an extension of SQL, the general syntax rules that apply to SQL
are also applicable to the PL/SQL language.
Guidelines
D
Identifiers can contain up to 30 characters, but they must start with an alphabetic
character.
D
Reserved words cannot be used as identifiers unless they are enclosed within
double quotation marks (for example, “SELECT”).


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status