PL/SQL Stands For Procedural Language/SQL. It Extends SQL by Adding
PL/SQL Stands For Procedural Language/SQL. It Extends SQL by Adding
PL/SQL stands for Procedural Language/SQL. It extends SQL by adding constructs found in
procedural languages, resulting in a structural language that is more powerful than SQL and
combines the data manipulating power of SQL with the data processing power of procedural
languages.
Using PL/SQL You can control program flow with statements like IF and LOOP. As with other
procedural programming languages, you can declare variables,define procedures and functions,
and trap runtime errors. PL/SQL lets you break complex problems down into easily
understandable procedural code, and reuse this code across multiple applications. SQL's column
types, making it easy to interchange PL/SQL variables with data inside
a table.
PL/SQL Architecture
The PL/SQL compilation and run-time system is a technology, not an independent product.
PL /SQL Engine compiles and executes PL/SQL blocks and subprograms.
The engine can be installed in an Oracle server or in an Oracle development tool such as Oracle
Forms or Oracle Reports.
PL/SQL Blocks
Anonymous procedure:
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.
Named procedure:
A named procedure may be called, it may accept inbound parameters but won't explicitly return
any value.
Named function:
A named function may also be called, it may accept inbound parameters and will always return a
value
The basic units (named procedures, namedx functions, and anonymous blocks) that make up a
PL/SQL program are logical blocks, which can be nested inside one another. A blockgroups
related declarations and statements. You can place declarations close to where they are used,
such as inside a large subprogram. The declarations are local to the block and cease to exist when
the block completes, helping to avoid cluttered namespaces for variables and procedures.
Section Description
Declarations Defines and initializes the variables and
cursors
used in the block.
Example
DECLARE
x NUMBER;
BEGIN
X := 15000;
DBMS_OUTPUT.PUT_LINE (‘The values for x is ’);
DBMS_OUTPUT.PUT_LINE (X);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE (‘Error Occurred’);
END;
declare
pi constant NUMBER(9,7) := 3.1415927;
radius INTEGER(5);
area NUMBER(14,2);
begin
radius := 3;
area := pi*power(radius,2);
insert into AREAS values (radius, area);
end;
Execution of Blocks
There are three basic ways to execute PL/SQL Code
Declarations Section
The Declarations section begins a PL/SQL block. The Declarations section starts with the
declare keyword, followed by a list of variable and cursor definitions. You can define variables
to have constant values, and variables can inherit data types from existing columns and query
results as shown in the above examples.
In the above listing, the area of a circle is calculated. The result is stored in a table named
AREAS. The AREAS table has two columns, to store radius and area values. The area of the
circle is calculated by squaring the value for the circle’s radius and multiplying that value times
the constant pi:
The end; signals the end of the PL/SQL block; depending on the tool you use, you may need to
add a / to execute the PL/SQL block. When the PL/SQL block is executed, you will receive the
following response from Oracle:
It allows the data manipulation and query statements of SQL to be included in block-
structured and procedural units of code, making PL/SQL a powerful transaction processing
language. With PL/SQL, you can use SQL statements to finesse Oracle data, and PL/SQL
control statements to process the data.
PL/SQL Environment
PL/SQL is not an Oracle product in its own right; it is a technology used by the
Oracle 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 Oracle
server. The engine that is used depends on where the PL/SQL block is being invoked.
When you submit PL/SQL blocks from a Oracle precompiler such as Pro*C or
Pro*Cobol program, user exit, iSQL*Plus, or Server Manager, the PL/SQL engine in the
Oracle Server processes them. It separates the SQL statements and sends them
individually to the SQL statements executor.
A single transfer is required to send the block from the application to the Oracle Server,
thus improving performance, especially in a client-server network. PL/SQL code can
also be stored in the Oracle Server as subprograms that can be referenced by any
number of applications connected to the database.
Benefits of PL/SQL
i) Integration
PL/SQL plays a central role in both the Oracle server (through stored procedures,
stored functions,database triggers, and packages) and Oracle development tools
(through Oracle Developer component triggers).
Oracle Forms Developer, Oracle Reports Developer, and Oracle Graphics Developer
applications make use of shared libraries that hold code (procedures and functions) and
can be accessed locally or remotely.
SQL data types can also be used in PL/SQL. Combined with the direct access that SQL
provides, these shared data types integrate PL/SQL with the Oracle server data
dictionary. PL/SQL bridges the gap between convenient access to database technology
and the need for procedural programming capabilities.
The procedural statement executor processes data that is local to the application (that
is, data already inside the client environment, rather than in the database). This reduces
the work that is sent to the Oracle server and the number of memory cursors that are
required.
Each SQL statement results in another call to the Oracle server and higher performance
overhead. In a networked environment, the overhead can become significant. As the
slide illustrates, if the application is SQL intensive you can use PL/SQL blocks and
subprograms to group SQL statements before sending them to the
Oracle server for execution.
Modularization Of Program
• Break down a complex problem into a set of manageable, well defined, logical
modules and implement the modules with blocks.
• Place reusable PL/SQL code in libraries to be shared between Oracle Forms and
Oracle Reports applications or store it in an Oracle server to make it accessible to
any application that can interact with an Oracle database.
Portability:
• PL/SQL is native to the Oracle server, so that easily move programs to any host
environment (operating system or platform) that supports the Oracle server and
PL/SQL.
• Also move code between the Oracle server and your application. You can write
portable program packages and create libraries that can be reused in different
environments.
Identifiers:
In PL/SQL you can use identifiers to do the following:
• Declare variables, cursors, constants, and exceptions and then use them in SQL and
procedural statements.
• Declare variables belonging to scalar, reference, composite, and large object (LOB)
data types.
• Declare variables dynamically based on the data structure of tables and columns in
the database.
• Process individually the rows returned by a multiple-row query with an explicit cursor
Errors:
The Error handling functionality in PL/SQL allows you to do the following:
Benefits of Subprograms
• Easy maintenance
• Improved data security and integrity
• Improved performance
• Improved code clarity
DECLARE
v_variable VARCHAR2(5);
BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
WHEN exception_name THEN
...
END;
• Place a semicolon (;) at the end of a SQL statement or PL/SQL control statement.
• When the block is executed successfully, without unhandled errors or compile errors,
the message output should be as follows:
• END and all other PL/SQL statements require a semicolon to terminate the
statement.
Declaration Section
The declaration section is the first section of the PL/SQL block. It contains definitions of
PL/SQL identifiers such as variables, constants, cursors, and so on.
FOR EXAMPLE
DECLARE
v_first_name VARCHAR2(35);
v_last_name VARCHAR2(35);
v_counter NUMBER := 0;
Executable Section
The executable section is the next section of the PL/SQL block. This section contains
executable statements that allow you to manipulate the variables that have been
declared in the declaration section.
FOR EXAMPLE
BEGIN
SELECT firstname, lastname
INTO v_first_name, v_last_name
FROM customer
WHERE customer# = 1013;
DBMS_OUTPUT.PUT_LINE ('Customer Name: '||v_first_name|| ' '||v_last_name);
END;
The example given shows the executable section of the PL/SQL block. It
begins with the keyword BEGIN and contains a SELECT INTO statement from
the customer table. The first and last names for customer# 1013 are
selected into two variables: v_first_name and v_last_name. Then the values of
the variables, v_first_name and v_last_name, are displayed on the screen with
the help of DBMS_OUTPUT.PUT_LINE statement. The end of the executable
section of this block is marked by the keyword END. The executable section
of any PL/SQL block always begins with the keyword BEGIN and ends with
the keyword END.
Exception-Handling Section
The exception-handling section is the last section of the PL/SQL block. This
section contains statements that are executed when a runtime error occurs
within the block. Runtime errors occur while the program is running and
cannot be detected by the PL/SQL compiler. Once a runtime error occurs,
control is passed to the exception-handling section of the block. The error is
then evaluated, and a specific exception is raised or executed. This is best
illustrated by the following example.
FOR EXAMPLE
BEGIN
SELECT firstname, lastname
INTO v_first_name, v_last_name
FROM customer
WHERE customer# = 1013;
DBMS_OUTPUT.PUT_LINE ('Customer Name: '||v_first_name|| ' '||v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('There is no customer with '|| 'Customer Number 1013');
END;
This shows the exception-handling section of the PL/SQL block. It begins with
the keyword EXCEPTION. The WHEN clause evaluates which exception must
be raised. In this example, there is only one exception, called
NO_DATA_FOUND, and it is raised when the SELECT statement does not
return any rows. If there is no record for cusomter# 1013 in the CUSTOMER
table, control is passed to the exception-handling section and the
DBMS_OUTPUT.PUT_LINE statement is executed.
You have seen examples of the declaration section, executable section, and
exception-handling section. Consider combining these examples into a single
PL/SQL block.
FOR EXAMPLE
DECLARE
v_first_name VARCHAR2(35);
v_last_name VARCHAR2(35);
BEGIN
SELECT firstname, lastname
INTO v_first_name, v_last_name
FROM customer
WHERE customer# = 1013;
DBMS_OUTPUT.PUT_LINE ('Customer Name: '||v_first_name|| ' '||v_last_name);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('There is no customer with '|| 'Customer Number
1013');
END;
Block Types
A PL/SQL program comprises one or more blocks. These blocks can be entirely
separate or nested one within another. The basic units (procedures and functions, also
known as subprograms, and anonymous blocks) that make up a PL/SQL program are
logical blocks, which can contain any number of nested subblocks. Therefore, one block
can represent a small part of another block, which in turncan be part of the whole unit of
code.
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 run time.
Example
Used Inside trigger and programs
Subprograms:
Subprograms are named PL/SQL blocks that can accept parameters and can be
invoked.
Example
Declare them either as procedures or as functions. Generally use a procedure to
perform an action and a function to compute a value.
Variables:
With PL/SQL you can declare variables and then use them in SQL and procedural
statements anywhere that an expression can be used.
• Temporary storage of data: Data can be temporarily stored in one or more variables
for use when validating data input and for processing later in the data flow process.
• Manipulation of stored values: Variables can be used for calculations and other data
manipulations without accessing the database.
• Ease of maintenance: When using %TYPE and %ROWTYPE you declare variables,
basing the declarations on the definitions of database columns. If an underlying
definition changes, the variable declaration changes accordingly at run time. This
provides data independence, reduces maintenance costs, and allows programs to
adapt as the database changes to meet new business needs.
Types of Variables:
All PL/SQL variables have a data type, which specifies a storage format, constraints,
and valid range of values. PL/SQL supports four data type categories—scalar,
composite, reference, and LOB (large object)—that you can use for declaring variables,
constants, and pointers.
PL/SQL variables:
Scalar:
Scalar data types hold a single value. The main data types are those that
correspond to column types in Oracle server tables; PL/SQL also supports Boolean
variables.
Composite:
Composite data types, such as records, allow groups of fields to be defined and
manipulated in PL/SQL blocks.
Reference:
Reference data types hold values, called pointers, that designate other program
items.
• Non-PL/SQL variables:
Bind and host variables
Syntax:
In the syntax:
CONSTANT constrains the variable so that its value cannot change; constants must
be initialized.
NOT NULL constrains the variable so that it must contain a value. (NOT NULL
variables must be initialized.)
expr
It is any PL/SQL expression that can be a literal expression, another
variable, or an expression involving operators and functions.
Examples:
DECLARE
v_hiredate DATE;
v_deptno NUMBER(2) NOT NULL := 10;
v_location VARCHAR2(13) := 'Atlanta';
c_comm CONSTANT NUMBER := 1400;
• If you use the NOT NULL constraint, you must assign a value.
• Declaring only one identifier per line makes code easier to read and maintain.
• In constant declarations, the keyword CONSTANT must precede the type specifier.
The following declaration names a constant of NUMBER subtype REAL and assigns
the value of 50000 to the constant.
Example:
Naming Rules
• Two variables can have the same name, provided they
are in different blocks.
Example
DECLARE
empno NUMBER(6);
BEGIN
SELECT empno
INTO empno
FROM employees
WHERE last_name = 'Kochhar';
END;
/
The above block both employee_id declared in PL/SQL block and select from the emp
Table is same this is not allowed or error in PL/SQL.
DECLARE
V_employee_id NUMBER(6);
BEGIN
SELECT empno
INTO v_employee_id
FROM emp
WHERE ename = 'SMITH';
DBMS_OUTPUT.PUT_LINE(‘ The employee id is ‘|| v_employee_id);
END;
/
• CHAR [(maximum_length)]
• VARCHAR2 (maximum_length)
• LONG
• LONG RAW
• NUMBER [(precision, scale)]
• BINARY_INTEGER
• PLS_INTEGER
• BOOLEAN
The examples of variable declaration shown on the slide are defined as follows:
• v_job: variable to store an employee job title
• v_count: variable to count the iterations of a loop and initialized to 0
• v_total_sal: variable to accumulate the total salary for a department and initialized to 0
• v_orderdate: variable to store the ship date of an order and initialize to one week
from today
• c_tax_rate: a constant variable for the tax rate, which never changes throughout the
PL/SQL block
• v_valid: flag to indicate whether a piece of data is valid or invalid and initialized to
TRUE
When declare PL/SQL variables to hold column values, must ensure that the variable is
of the correct data type and precision. If it is not, a PL/SQL error will occur during
execution.
use the %TYPE attribute to declare a variable according to another previously declared
variable or database column. The %TYPE attribute is most often used when the value
stored in the variable will be derived from a table in the database. To use the attribute in
place of the data type that is required in the variable declaration, prefix it with the
database table and column name. If referring to a previously declared variable, prefix
the variable name to the attribute.
Advantage of %Type
There is no need to be concerned with column data type changes made at the database
level. You can also declare a variable according to another previously declared variable
by prefixing the variable name to the attribute.
Examples:
v_name emp.ename%TYPE;
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 10;
Declare variables to store the last name of an employee. The variable v_name is
defined to be of the same data type as the LAST_NAME column in the EMPLOYEES
table. %TYPE provides the data type of a database column:
...
v_name emp.ename%TYPE;
The variable v_min_balance is defined to be of the same data type as the variable
v_balance. %TYPE provides the data type of a variable:
v_balance NUMBER(7,2);
v_min_balance v_balance%TYPE := 10;
With PL/SQL you can compare variables in both SQL and procedural statements.
These comparisons, called Boolean expressions, consist of simple or complex
expressions separated by relational operators. In a SQL statement, you can use
Boolean expressions to specify the rows in a table that are affected by the statement. In
a procedural statement,
Boolean expressions are the basis for conditional control. NULL stands for a missing,
inapplicable, or unknown value.
Examples
v_sal1 := 50000;
v_sal2 := 60000;
A scalar type has no internal components. A composite type has internal components that can be
manipulated individually. Composite data types (also known as collections) are of TABLE,
RECORD, NESTED TABLE, and VARRAY types.
Bind Variables
A bind variable is a variable that declare in a host environment. Bind variables can be
used to pass run-time values, either number or character, into or out of one or more
PL/SQL programs. The PL/SQL programs use bind variables as they would use any
other variable. It can reference variables declared in the host or calling environment in
PL/SQL statements, unless the statement is in a procedure, function, or package. This
includes host language variables declared in precompiler programs, screen fields in
Oracle Developer Forms applications, and iSQL*Plus bind variables.
To declare a bind variable in the SQL*Plus environment, use the command VARIABLE.
For example,
Declare a variable of type NUMBER and VARCHAR2 as follows:
SQL Plus can reference the bind variable, and SQL*Plus can display its value
through the SQL*Plus PRINT command.
BEGIN
SELECT sal
INTO :g_salary
FROM emp
WHERE empno = 7902;
END;
/
PRINT g_salary
To reference host variables, you must prefix the references with a colon (:) to
distinguish them from declared PL/SQL variables.
Example
This example computes the monthly salary, based upon the annual salary supplied by
the user. This script contains both SQL*Plus commands as well as a complete PL/SQL
block.
PRINT g_monthly_sal
Scope of a Variable
The scope of a variable is the portion of the program in which the variable can be
accessed, or where the variable is visible. It usually extends from the moment of
declaration until the end of the block in which the variable was declared. The visibility of
a variable is the part of the program where the variable can be accessed.
Labels can be added to a block in order to improve readability and to qualify the
names of elements that exist under the same name in nested blocks. The name of the
block must precede the first line of executable code (either the BEGIN or DECLARE) as
follows:
set serveroutput on
<<find_stu_num>>
BEGIN
DBMS_OUTPUT.PUT_LINE('The procedure find_stu_num has been executed.');
END find_stu_num;
The label optionally appears after END. In SQL*Plus, the first line of a PL/SQL block cannot
be a label. For commenting purposes, you may alternatively use "- -" or /*, nding with */.
SET SERVEROUTPUT ON
<< outer_block >>
DECLARE
v_test NUMBER := 123;
BEGIN
DBMS_OUTPUT.PUT_LINE
('Outer Block, v_test: '||v_test);
<< inner_block >>
DECLARE
v_test NUMBER := 456;
BEGIN
DBMS_OUTPUT.PUT_LINE('Inner Block, v_test: '||v_test);
DBMS_OUTPUT.PUT_LINE('Inner Block, outer_block.v_test: '||
outer_block.v_test);
END inner_block;
END outer_block;
A variable that has been declared in the declaration section of the PL/SQL block can
later be given a value with a SELECT statement.
DECLARE
v_zip zipcode.zip%TYPE;
v_user zipcode.created_by%TYPE;
v_date zipcode.created_date%TYPE;
BEGIN
SELECT 43438, USER, SYSDATE INTO v_zip, v_user, v_date FROM dual;
INSERT INTO zipcode (ZIP, CREATED_BY ,CREATED_DATE, MODIFIED_BY,
MODIFIED_DATE ) VALUES(v_zip, v_user, v_date, v_user, v_date);
END;