You are on page 1of 11

RDBMS Using Oracle

BIT-4

PL/SQL
Procedural Language/Structural
Query Language

Kamran.Munir@niit.edu.pk

PLSQL
?

In PLSQL we will work on PLSQL blocks.

There are two types of PLSQL blocks.


(A) Anonymous Block
? These are block without names.
(B) Named Block (Subprograms)
? These are the procedures &
Functions

Anonymous Block
Block Structure
DECLARE
<Define PL/SQL objects to be used within this block>
BEGIN
<Executable Statements>
EXCEPTION
< Used to trap Predefined error conditions>
END;

Anonymous Block
DECLARE

(Optional)

BEGIN

(Mandatory)

EXCEPTION

(Optional)

END;

(Mandatory)

Anonymous PL/SQL block - using


SQL*Plus defined variables
DECLARE
A VARCHAR2(20):= 10;
BEGIN
DBMS_OUTPUT.PUT_LINE('***********');
DBMS_OUTPUT.PUT_LINE(A);
DBMS_OUTPUT.PUT_LINE('***********');
END;
before running this program first set SERVER OUTOUT ON i.e.

SQL> SET SERVEROUTPUT ON;

Practice
?
?
?
?
?
?
?

Declare 2 number variables


Declare 2 date variables
Declare 2 Varchar2 variables
Assign all values inside BEGIN --- END
Show result by adding number variables
Show result by subtracting First date from
2nd date
Show CONCAT result of varchar variables

Block Structure for PLSQL Subprogram


(Named Block)
HEADER
IS
BEGIN

<Declaration Section>
<Executable Statements>

EXCEPTION
< Used to trap Predefined error conditions>
END;

PLSQL Subprogram
?

PLSQL subprograms can take Parameters


and be invoked from the database as well
as from txt file.
PLSQL has two types of subprograms
Procedures
Functions

PLSQL Subprogram
?

HEADER
Header is for named blocks only header
determines the PLSQL block type i.e.
Function or Procedure
Header Contains the
Name of the program
? Parameter List
? Return Type (only for Functions)
?

Benefits of Subprograms
Improved Maintenance
? Improved data security and integrity
? Improved Performance
?

Creating Procedure

Kamran.Munir@niit.edu.pk

Creating Procedure
?

A procedure can be stored in the database


as a database object for repeated
execution.
We can call a procedure any time from any
where, like from FrontFront-end program or from
database Server.

Creating Procedure
<SYNTAX>
CREATE [OR REPLACE] PROCEDURE <PROCEDURE NAME>
(argument 1
argument 2
..
.
IS [AS]
PLSQL BLOCK;

[MODE 1]
[MODE 2]

datatype-1,
datatypedatatype--2,
datatype

Creating Procedure contd..


?

Arguments
Name of PLSQL variables whose value or
variable name is passed.

Mode
There are three types of modes
IN (default)
? OUT
? IN OUT
?

Creating Procedure contd..


?

We can also create a procedure in any txt


editor.
Save it with *.sql
*. sql execution on any where
into your hard disk. e.g. c:
c:\\abc
abc\\a.sql
Run this procedure from SQL plus as
SQL> @ c:
c:\\abc
abc\\a.sql

A Simplest Procedure
CREATE or REPLACE Procedure p_test
IS
BEGIN
Delete from emp where sal = 800 ;
END;

A Simplest Procedure contd..


?

Procedure Execution
SQL> execute p_test
p_test;;

To see errors during compilation


SQL> SHOW ERRORS

Parameters MODES for formal


parameters
IN
DEFAULT

OUT
Must be
specified
Value is passed Returned to
Into subprogram calling
environment

IN OUT
Must be
specified
passed
Into subprogram
,Returned to
calling
environment

Creating Procedure
Using IN Parameter
EXAMPLE

Kamran.Munir@niit.edu.pk

CREATE or REPLACE procedure raise_salary

(v_id
IS
BEGIN

in

number)

Update emp
set sal = sal * 1.10
where empno = v_id
v_id;;
DBMS_OUTPUT.PUT_LINE(DONE);

End raise_salary
raise_salary;;

10

SQL> execute raise_salary(7369);

Thanks

Kamran.Munir@niit.edu.pk

11