Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
Basic PL

Basic PL

Ratings: (0)|Views: 30 |Likes:
Published by api-3759101

More info:

Published by: api-3759101 on Oct 15, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Basic PL/SQL Block Structure

PL/SQL is structured into blocks and can use conditional statements, loops and branches to control program flow. Variables can be scoped so that they are only visible within the block where they are defined.

PL/SQL blocks come in three types, these are anonymous procedure, named procedure and
named function. All of these block types share most PL/SQL features so during this tutorial the
features that apply to all block types will be grouped into single subjects.

An anonymous procedure, is an unnamed procedure, it can't be called. It is placed where it is to
be run, normally attached to a database trigger or application event.
A named procedure may be called, it may accept inbound parameters but won't explicitly return
any value.
A named function may also be called, it may accept inbound parameters and will always return a
value.
DECLARE
Definition of any variables ,or objects that are used within the declared block.
BEGIN
Statements that make up the block.
EXCEPTION
All exception handlers.
END;
End of block marker.
Example :-
DECLARE
TEMP_COST NUMBER(10,2);

BEGIN
SELECT COST FROM JD11.BOOK INTO TEMP_COST WHERE ISBN = 21;
IF TEMP_COST > 0 THEN

UPDATE JD11.BOOK SET COST = (TEMP_COST*1.175) WHERE ISBN = 21;
ELSE
UPDATE JD11.BOOK SET COST = 21.32 WHERE ISBN = 21;
END IF;
COMMIT;
EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO JD11.ERRORS (CODE, MESSAGE) VALUES(99, 'ISBN 21 NOT FOUND');
END;
As you can see there are several elements in the example that haven't been covered in the SQL
tutorial, these elements are the PL/SQL extensions. They include :-
Variables and These objects are used to store and manipulate block level data. They can be
1
Constants
CHAR, VARCHAR2, NUMBER, DATE or BOOLEAN data types.
SQL support
All SQL statements are supported within PL/SQL blocks including transaction
control statements.
Composite
Datatypes
Records allow groups of fields to be defined and manipulated in PL/SQL blocks.
Flow ControlIfs, Loops, GOTOs and labels provide conditional actions, tests, branching and
iterative program control.
Built In
functions
Most SQL data functions are supported within PL/SQL blocks.
Cursor
handling

Cursors (a memory area holding a result set) can be explicitly defined and
manipulated allowing the processing of multiple rows. A group of PL/SQL system
attributes provide the ability to test a cursor's internal state.

Exception
handling

Blocks have the ability to trap and handle local error conditions (implicit
exceptions). You may also self generate explicit exceptions that deal with logic
and data errors.

Code storage
Blocks may be stored within an Oracle database as procedures, functions,
packages (a group of blocks) and triggers.
The rules of block structure are :-

Every unit of PL/SQL must constitute a block. As a minimum there must be the delimiting words
BEGIN and END around the executable statements.
SELECT statements within PL/SQL blocks are embedded SQL (an ANSI category). As such they
must return one row only. SELECT statements that return no rows or more than one row will
generate an error. If you want to deal with groups of rows you must place the returned data into a
cursor. The INTO clause is mandatory for SELECT statements within PL/SQL blocks (which are
not within a cursor definition), you must store the returned values from a SELECT.
If PL/SQL variables or objects are defined for use in a block then you must also have a
DECLARE section.
If you include an EXCEPTION section the statements within it are only processed if the
condition to which they refer occurs. Block execution is terminated after an exception handling
routine is executed.
PL/SQL blocks may be nested, nesting can occur wherever an executable statement could be
placed (including the EXCEPTION section).

Declaring Variables And Constants

PL/SQL supports a variety of data types that you can use for declaring variables and constants.
You can assign values to variables as you declare them and you can change the value of a variable
through further assignments.

You must assign the value of a constant to it when you declare it, this value is fixed and cannot be
changed at run time.
Example variable declarations are given below :-
2
DECLARE

V_NUM1 NUMBER NOT NULL := 10109;
NUM8 NUMBER(3,1);
XYZ NUMBER(2,2) := 31.8;
ABC12 NUMBER(9,2) := XYZ * 131;
V_CHR1 CHAR(89);
V_CHR2 VARCHAR2(12) := "JANUARY";
TODAY DATE := SYSDATE;
TRUEFALSE BOOLEAN;

Example constant declarations are given below :-
DECLARE
PI CONSTANT NUMBER(9,3) := 3.142;
VAT CONSTANT NUMBER(4,2) := 17.5;

When you declare PL/SQL variables to hold column values you must ensure that the variable type
is the same as the column type else you will get a run time error at execution. You can use the
%TYPE attribute to base a variable upon the column definition as defined within the Oracle data
dictionary. The attribute is prefixed with the schema, table and column name and used where the
datatype is required in the DECLARE section. PL/SQL determines the datatype and size of the
variable when the block is complied and so is always compatible with the column used to
populate it. An example follows :-

DECLARE
V_NUM1 JD11.BOOK.COST%TYPE;
PL/SQL Records

A PL/SQL record is a variable that contains a collection of separate fields. Each field is
individually addressable. You can reference the field names in both assignments and expressions.
The fields within a record may have different datatypes and sizes, like the columns of a database
table. Records are a convenient way of storing a complete fetched row from a database table.

Use the %ROWTYPE attribute to declare a record based upon a collection of database columns
from a table or view. The fields within the record take their names and datatypes from the
columns of the table or view.

Declare the record in the DECLARE section along with any other required variables and
constants. An example follows :-
DECLARE
REC1 JD11.BOOK%ROWTYPE;
REC4 JD11.BOOK%ROWTYPE;

The above declaration sets the object REC1 to be a record object holding fields that match the
columns in the BOOK table. It doesn't hold any values until it is populated.
Assign values into a PL/SQL record ,by naming the record after the INTO keyword of a SELECT
statement. The INTO keyword defines the name specification for the storage area(s) of queried
value(s).

BEGIN
SELECT * FROM JD11.BOOK INTO REC1 WHERE ISBN = 21;
END;
You can assign all the record values from one record to another provided that the record
definitions are the same.
3

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->