0% found this document useful (0 votes)
1 views

Basic Structure and Syntax of P1

PL/SQL programs are structured into blocks, which can be named (subprograms) or unnamed (anonymous). Each block consists of a DECLARE section for defining variables and types, an EXECUTABLE section for the main code, and an EXCEPTION section for handling runtime errors. PL/SQL supports various data types and flow control statements, allowing for robust error handling and program structure.

Uploaded by

Hafsa Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Basic Structure and Syntax of P1

PL/SQL programs are structured into blocks, which can be named (subprograms) or unnamed (anonymous). Each block consists of a DECLARE section for defining variables and types, an EXECUTABLE section for the main code, and an EXCEPTION section for handling runtime errors. PL/SQL supports various data types and flow control statements, allowing for robust error handling and program structure.

Uploaded by

Hafsa Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Basic Structure and Syntax of PL/SQL : long as they are declared before they are

referenced in the program. Subprograms


PL/SQL, groups the syntax of the programs
declared last. A semicolon terminates each
into units called blocks. These blocks can
definition. Datatypes PL/SQL provides a
either named or unnamed. The named
number of predefined data types for
blocks are called subprograms and
variables and constants. It also enables you
unnamed blocks are called anonymous
to define your own types, which are
blocks. Subprograms can be referred as
subtypes of the predefined types
either functions or procedures. The
difference between functions and . The types fall into the following three
procedures is that a function can be used in categories:
an expression and it returns a value to that
 Scalar. These include all string, number,
expression. While a procedure is invoked as
and binary types.
a standalone statement and passes values
to the calling program only through  Composite. These are structured data
parameters. types. The PL/SQL composite types are
TABLE and RECORD.  Reference. There is
Subprograms can be nested within one
one kind of reference data type--REF
another and can be grouped in larger units
CURSOR--which is a pointer to a cursor...
called packages. A block has three parts:
In many cases, one can convert from one
o A DECLARE section. In this
data type to another, either explicitly or
section one can define local
automatically. One can also define a
variables, constants, types,
variable so that it inherits its data type from
exceptions, and nested
a database column or from another
subprograms.
variable or constant.
o An EXECUTABLE section. In this
is the actual code of a block 2.) The EXECUTABLE Section
gets executed. This part of the The executable section is the main body of
block must be always present code. It consists primarily of SQL
in the program. statements, flow control statements, and
o An EXCEPTION section. This assignments.
section is used for handling
runtime errors and warnings. Assignments
The assignment operator is :=. For example,
the following statement assigns the value ٤
1.) The DECLARE Section ٥ to the variable a: a := ٤٥;
The DECLARE section begins with the Character strings should be set off with
keyword DECLARE and ends when the single quotes (') as in all expressions. An
keyword BEGIN. The next section that example follows:
follows is the EXECUTABLE section. One can
declare types, constants, variables, FNAME := 'Clair';
exceptions, and cursors in any order, as Flow Control
PL/SQL supports the following kinds of predefined exceptions are provided by
flow-control statements: PL/SQL in a package called STANDARD.
They correspond to various runtime
 IF statements. These execute a group of
problems that are known to arise often--
one or more statements based on whether
for example, dividing by zero or running
a condition is TRUE.
out of memory.
 Basic loops. These repeatedly execute a
The Oracle Server can distinguish between
group of one or more statements until an
and track many more kinds of errors than
EXIT statement is reached.
the limited set that STANDARD predefines.
 FOR loops. These repeatedly execute a Each of Oracle's hundreds of messages are
group of one or more statements a given identified with a number, and STANDARD
number of times or until an EXIT statement has simply provided labels for a few of the
is reached. common ones.
 WHILE loops. These repeatedly execute a You can deal with the other messages in
group of one or more statements until a either or both of two ways:
particular condition is met or an EXIT
 You can define your own exception labels
statement is reached.
for specified Oracle messages using a
 GOTO statements. These pass execution pragma (a compiler directive). This
directly to another point in the code, procedure will be explained shortly.
exiting loops and enclosing blocks as
 You can define a handler for the default
necessary. Use these sparsely, as they make
exception OTHERS. Within that handler,
code difficult to read and debug.
you can identify the specific error by
3.) The EXCEPTION Section accessing the built-in functions SQLCODE
The EXCEPTION section follows the END and SQLERRM, which contain, respectively,
that matches the BEGIN of the the numeric code and a prose description
EXECUTABLE section and begins with the of the message.
keyword EXCEPTION. It contains code that You can also define your own exceptions as
responds to runtime errors. An exception is will be shown. It is usually better, however,
a specific kind of runtime error. to use Oracle exceptions where possible,
When that kind of error occurs, you say because then the conditions are tested
that the exception is raised. An exception automatically when each statement is
handler is a body of code designed to executed, and an exception is raised if the
handle a particular exception or group of error occurs.
exceptions. Exception handlers, like the
rest of the code, are operative only once
the code is compiled and therefore can do
nothing about compilation errors.
There are two basic kinds of exceptions:
predefined and user-defined. The

You might also like