PLSQL
PLSQL
PLSQL
RELATIONAL DATABASE
MANAGEMENT SYSTEM
(RDBMS)
TOPIC: PL/SQL
PL/SQL
PL/SQL stands for Procedural Language
/ Structured Query Language.
PL/SQL is a superset of SQL.
PL/SQL is a block-structured language
that enables developers to combine the
power of SQL with procedural
statements
2
Comparison between SQL and
PL/SQL
SQL PL/SQL
No Procedural Procedural
Capabilities Capabilities
Time consuming Reduced Network
Processing Traffic
No Error handling Error Handling
Procedures Procedures
No facilities Sharing Facilities Sharing
3
PL/SQL is a block-structured language,
meaning that programs can be divided
into logical blocks.
PL/SQL block has a definite structure.
PL/SQL block consists of up to three
sections
4
PL/SQL Block Structure
DECLARE (optional)
Declarations of memory variables, constants, cursors,
user defined exceptions etc in PL/SQL
BEGIN (Mandatory)
SQL Statements
PL/SQL Statements
EXCEPTION (optional)
SQL or PL/SQL code to handle errors that arise
during the execution of the code block
END; (Mandatory)
5
The PL/SQL Architecture
7
CREATING TABLE AREAS
CREATE TABLE AREAS
(
RADIUS NUMBER(6,2),
AREA NUMBER(16,2)
);
8
Program to find Area of a circle
declare
Pi Constant NUMBER (5, 3): = 3.142;
radius NUMBER (6,2);
area NUMBER (16,2);
begin
radius: = 6;
area : = Pi * power (radius, 2);
Insert into AREAS values (radius, area);
end;
/
9
The end; signals the end of the PL/SQL
block, and the / executes the PL/SQL
block. When the PL/SQL block is
executed, you will receive the following
response from Oracle.
PL/SQL procedure successfully
completed.
10
Vipan Arora
GPCG Jalandhar Punjab EDUSAT Society 11
Query and Results
Select *
From Areas;
The output is displayed as follows
12
Variables
Variables is PL/SQL blocks are named
variables. A variable name must begin
with a character and can be followed by
a maximum of 29 characters.
With PL/SQL you can declare variables
and then use them in SQL and
procedural statements anywhere an
expression can be used.
13
Temporary storage of data
Manipulation of stored values.
Reusability
Ease of maintenance
14
Type of Variables
Scalar datatypes hold a single value. The main
datatypes are those that correspond to column types
in Oracle Server tables : PL/SQL also supports
Boolean variables.
Composite datatypes such as records allow groups
of fields to be defined and manipulated in PL/SQL
blocks.
Reference datatypes hold values, called pointers,
that designate other program items.
LOB datatypes hold values, called locators, that
specify the location of large objects (graphic images
for example) that are stored out of line.
15
Declaring Variables
Variables are declared in the DECLARE
section of the PL/SQL block.
Examples
Declare
Name Char (25) ;
Age Number (3);
Deptno Number (4) NOT NULL : = 20;
Location VARCHAR2 (20) : = ‘Delhi’ ;
Comm CONSTANT NUMBER : = 2500;
16
Assigning value to variables
The assigning of a value to a variable can be done in
two ways :
(i) Using the assignment operator : = (i.e. a colon
followed by an equal to sign)
(ii) Selecting or fetching table data values into
variables
The syntax is
identifier : = expr;
In the syntax
identifier is the name of the scaler variable
expr can be a variable, literal or function call, but not a
database column.
17
DBMS_OUTPUT.PUT_LINE
An oracle-supplied packaged
procedure
An alternative for displaying data
from a PL/SQL block
Must be enabled in SQL*Plus with
SET SERVEROUTPUT ON
18
To compute the monthly salary and prints it to the screen, using
DBMS_OUTPUT.PUT_LINE
SET SERVEROUTPUT ON
ACCEPT p_annual_sal PROMPT‘Please enter the
annual salary :’
DECLARE
v_sal NUMBER (9,2) := &p_annual_sal;
BEGIN
v_sal := v_sal/12;
DBMS_OUTPUT.PUT_LINE (‘The monthly salary
is ’|| TO_CHAR(v_sal)) ;
END;
/
19
Writing Executable statements
20
Vipan Arora
GPCG Jalandhar Punjab EDUSAT Society 21