Professional Documents
Culture Documents
BIT-4
PL/SQL
Procedural Language/Structural
Query Language
Kamran.Munir@niit.edu.pk
PLSQL
?
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)
Practice
?
?
?
?
?
?
?
<Declaration Section>
<Executable Statements>
EXCEPTION
< Used to trap Predefined error conditions>
END;
PLSQL Subprogram
?
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
?
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
Arguments
Name of PLSQL variables whose value or
variable name is passed.
Mode
There are three types of modes
IN (default)
? OUT
? IN OUT
?
A Simplest Procedure
CREATE or REPLACE Procedure p_test
IS
BEGIN
Delete from emp where sal = 800 ;
END;
Procedure Execution
SQL> execute p_test
p_test;;
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
(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
Thanks
Kamran.Munir@niit.edu.pk
11