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.
SELECT COST FROM JD11.BOOK INTO TEMP_COST WHERE ISBN = 21;
IF TEMP_COST > 0 THEN
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.
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.
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
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).
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.
V_NUM1 NUMBER NOT NULL := 10109;
XYZ NUMBER(2,2) := 31.8;
ABC12 NUMBER(9,2) := XYZ * 131;
V_CHR2 VARCHAR2(12) := "JANUARY";
TODAY DATE := SYSDATE;
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 :-
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.
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
Now bringing you back...
Does that email address look wrong? Try again with a different email.
This action might not be possible to undo. Are you sure you want to continue?