Professional Documents
Culture Documents
Advance PL-SQL
Khizar M. Dhakam
Email : dhakam_khizar@yahoo.com
KhiZar M. DhaKam
Objectives
Local Procedure, Functions
Database Procedure, Functions, Packages (Example)
Authid Syntax
AUTONOMUS_TRANSACTION Syntax
Dynamic SQL FOR DDL
Triggers (Example)
KhiZar M. DhaKam
Local Procedure
Note : Procedure have to call if you want to execute it.
When not store at database (procedure created in
anonymous block) is not an database object.
Can have as many procedures. Local Procedure
Procedure can take input or output or both input/output
parameter
declare
84 85
declare
procedure abc is procedure abc is
begin begin
insert into tempp values(1,'in abc'); insert into tempp values(1,'in abc');
end; end;
Begin
abc; begin
insert into tempp values(2,'in main'); insert into tempp values(2,'in main');
end; end;
/ /
KhiZar M. DhaKam
declare 86 Input
Input variable
variable to
to procedure
procedure
procedure abc is IN or INOUT OR OUT parameter
begin By default it will be “IN” parameter
insert into tempp values(1,'in abc');
end; Declare
x number :=2; X number :=2; 89
begin Procedure abc(y number) is
insert into tempp values(x,'in main'); Begin
abc; Insert into tempp values(y,'in abc');
end; End;
/
Begin
Declare Insert into tempp values (x,'in main');
87 Abc(x);
x number :=2;
procedure abc is End;
y number := 999; X can change /
begin X := X + 1; PLS-00306: wrong number or types of
88 arguments in call to 'ABC'
insert into tempp values(y,'in abc');
insert into tempp values(x,'in abc'); ABC; 90
end;
begin
insert into tempp values(x,'in main'); procedure abc(y number default 3) is 91
abc;
end; 2 in main To avoid getting error can pass a default value
999 in abc
/
2 in abc
KhiZar M. DhaKam
Declare 96 Declare 97
x number :=10; x number :=10;
procedure abc (a in number, y number :=5;
b out number) is procedure abc(a number, b number)
begin is
b:=a/2; begin
end; insert into tempp values(a,'inserting a');
begin insert into tempp values(b,'inserting b');
Abc (x, x); end;
insert into tempp values (x, 'in main'); Begin
end;
/ 10 in main --position notation is before named notation
5 in main abc ( b=>x, a=>y);
end; 10 in abc
/
TIP : Use case statement in procedure call 5 in main
to use the proper parameter value
KhiZar M. DhaKam
declare 98 declare 99
x number :=2; x number :=2;
procedure abc is procedure abc is
begin begin
Insert into tempp values(1,'in abc'); insert into tempp
end; values(1,‘from pqr in abc');
end;
procedure pqr is
procedure pqr is
begin
begin
insert into tempp values(2,'in pqr');
insert into tempp values(2,'in pqr');
end;
abc;
2 in main end;
begin
1 in abc
insert into tempp values(x,'in main'); begin
abc; 2 in pqr insert into tempp values(x,'in main');
pqr; pqr;
end; end; 2 in main
/ / 2 in pqr
1 from pqr in abc
KhiZar M. DhaKam
OverLoading
OverLoading in
in Procedure
Procedure Note : In Procedures
Two or more procedure with same name in Only function return number or char.
database can be possible but with different You can’t assign values like this.
parameter or datatypes.
ERROR at line 1: If you don’t pass Return clause it will produce error 107
ORA-06502: PL/SQL: numeric or value error: ORA-06503: PL/SQL:
character to number conversion error Function returned without value
KhiZar M. DhaKam
PACKAGE
etc.
Procedure <name>(variable and parameter);
End;
/ SYNTAX Function <name> (variable and parameter)
return number ;
End; SYNTAX
Create <or Replace> function <Name>
(variables with parameter) return number is
: FUNCTION
Package body
<Select Into clause Procedure <name>(variable and parameter)
FORMULA --- statements
SYNTAX
RETURN End;
Number/Char/Varchar/Boolean Function <name> (variable and parameter)
End; return number is Begin --- statements
/ end;
SYNTAX End; SYNTAX
KhiZar M. DhaKam
Examples
for Procedure
Procedure Example1
Examples
for Function
for Package is
Begin
Statements
End;
Create or Replace Package Dept_pkg is
Procedure inst_dept(dno number, name varchar2, lc Procedure upd_dept
varchar2); (dno number, lc varchar2, name varchar2)
Procedure upd_dept(dno number, lc varchar2, name is
varchar2); Begin
Function bonus_sl (eno number, sl number) Statements
return number ;
End;
End;
Function bonus_sl
(eno number, sl number)
return number is
Begin
Statements
End;
End;
KhiZar M. DhaKam
DEFINER CURRENT_USER
If procedure is made using authid If procedure created with
DEFINER then the procedure will execute CURRENT_USER then the procedure will
in where the procedure is made. execute in who is using it.
Create <or Replace> Procedure <Name> Create <or Replace> function <Name>
(argument with in / out / inout parameters) (variables with parameter) return number
: PROCEDURE
: FUNCTION
AUTHID DEFINER | CURRENT_USER AUTHID DEFINER | CURRENT_USER
is is
<DEFINE VARIABLES > Authid Syntax
<DEFINE VARIABLES >
Begin Begin
SYNTAX
<Select Into clause <Select Into clause
SYNTAX
Create table aut_dept as select * from dept where 1=2; Create table aut_dept as select * from dept where 1=2;
GRANT Execute on AUT_PRO to HR; You can create function with AUTHID
SQL> Select * from aut_pro; You can create function with AUTHID
KhiZar M. DhaKam
Create <or Replace> Procedure <Name> Create <or Replace> function <Name>
(argument with in / out / inout parameters) (variables with parameter) return number
: PROCEDURE
: FUNCTION
AUTHID DEFINER | CURRENT_USER AUTHID DEFINER | CURRENT_USER
is is
AUTONOMUS_TRA
<DEFINE VARIABLES > NSACTION Syntax <DEFINE VARIABLES >
Begin Begin
SYNTAX
<Select Into clause <Select Into clause
SYNTAX
Begin Begin
Insert into emp values( -- -); Insert into emp values( -- -);
Khi_pkg.Raise_sal(7369, 5000); Raise_sal(7839, 5000);
Delete from emp where ename=‘KING’; Delete from emp where ename=‘KING’;
Rollback; Rollback;
End; End;
/ /
KhiZar M. DhaKam
Create or Replace Procedure -SQL & PLSQL Does not support DDL Statement
-To create table Dynamically you should have create any
Drop_table (tname varchar2) is
Dynamic SQL FOR table Previlage.
Begin DDL -It was possible before 8 ver also but using dbms_sql.
Execute immediate -From 8i ver ounwards its very simple using EXECUTE
‘Drop table ‘ || Tname; IMMEDEATE
End;
Execute add_loc(‘MURUD’);
KhiZar M. DhaKam
Triggers
You can refer new and old values for various statement such as
For UPDATE both :new and :old can be used because row will have
both the old and new values.
KhiZar M. DhaKam
ERROR at line 1:
ORA-20110: invalid department no
ORA-06512: at "BLACK.INS_DEPT", line 4
ORA-04088: error during execution of trigger 'BLACK.INS_DEPT'
KhiZar M. DhaKam
Assignment
Validity Check using Trigger
(Example)
You can not alter the trigger, you have to use ‘replace’ or drop and recreate.
DECLARE
FileHandler UTL_FILE.FILE_TYPE;
BEGIN
FileHandler := UTL_FILE.FOPEN(‘MYDIR', 'test_file.txt', 'W');
UTL_FILE.PUTF(fileHandler, 'Look ma, I''m writing to a file!!!\n');
UTL_FILE.FCLOSE(fileHandler);
EXCEPTION
WHEN utl_file.invalid_path THEN
raise_application_error(-20000, 'ERROR: Invalid path for file.');
END;
/
KhiZar M. DhaKam
The solution is achieved through a PL/SQL procedure, which I'll first list and
then deconstruct. Here it is:
KhiZar M. DhaKam
ON t1
as though a different
FOR EACH ROW
user logged on
DECLARE
same table.
DECLARE
3 testcol VARCHAR2(15));
New Follow Clause in Trigger : 11g
AFTER UPDATE
ON test
of the same type the trigger should fire.
ON <table_name>
New Compound Trigger Syntax
COMPOUND TRIGGER
BEFORE STATEMENT IS
BEGIN
...
END BEFORE STATEMENT;
AFTER STATEMENT IS
BEGIN
...
END AFTER STATEMENT;
---------------------------
1
SELECT table_name, BEFORE STATEMENT IS
tablespace_name BEGIN
FROM user_tables; dbms_output.put_line('Before Statement Level ');
END BEFORE STATEMENT;
-------------------------------
BEFORE EACH ROW IS
SELECT trigger_name, 3 BEGIN
trigger_type dbms_output.put_line('BEFORE ROW LEVEL');
FROM user_triggers; END BEFORE EACH ROW;
-------------------------------
AFTER STATEMENT IS
BEGIN
INSERT INTO test VALUES 4 dbms_output.put_line('After Statement Level');
(‘DEPT‘, ‘USERS’); END AFTER STATEMENT;
-------------------------------
AFTER EACH ROW IS
BEGIN
dbms_output.put_line('AFTER ROW LEVEL');
INSERT INTO test VALUES END AFTER EACH ROW;
(‘EMP‘, ‘USERS’); END compound_trig;
/
END of Examples
Learning PL-SQL
Any suggestion or comments to improve
please email.
Khizar M. Dhakam
Email : dhakam_khizar@yahoo.com