You are on page 1of 36

Examples Learning

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 Input Variable to procedure


X number; 92 Continue…
Procedure abc (a in number, b out number)
is 95
Begin Declare
Insert into tempp values (a, 'in abc'); X number;
Procedure abc(a in out number) is
B:=a/2; Begin
End; Insert into tempp values(a,'in abc');
Begin A:=a/2;
Abc(10, x); End;
Insert into tempp values (x, 'in main'); Begin
End; X:=10;
/ Abc(x);
10 in abc
Insert into tempp values(x,'in main');
5 in main End;
Error : a cannot be used as assignment target. / 10 in abc
5 in main
A : = a/2; 93

If you remove the B := a/2; 94


10 in abc
in main
KhiZar M. DhaKam

Local Procedure Positional Parameter


Continue… Can
Can be
beassign
assignbyby
“=>”
“=>”
i.e. changing the position of parameter while passing
parameters

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

Local Procedure Calling


Calling Procedure
Procedure from
from procedure
procedure
Continue … Procedure can be call in procedure, trigger,
function, packages etc.

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

DECLARE 0 Calling Procedure from procedure


NOCOPY in Procedure
n NUMBER := 10; Procedure can be call in procedure, trigger,
PROCEDURE do_something ( n1 IN Number, function, packages etc.
n2 IN OUT NOCOPY Number,
n3 IN OUT Number) IS
BEGIN 0
dbms_output.put_line('n1 ' || n1); NOCOPY
dbms_output.put_line('n2 ' || n2);
dbms_output.put_line('n3 ' || n3);
dbms_output.put_line('n ' || n);
dbms_output.put_line('---- n2 := 20 --- ' );
n2 := 20;
ABC Exception;
dbms_output.put_line('n1 ' || n1);
dbms_output.put_line('n2 ' || n2);
dbms_output.put_line('n3 ' || n3); 0
dbms_output.put_line('n ' || n);
n3 := 30; Exception when abc then
dbms_output.put_line('--- n3 := 30 ---- ' );
dbms_output.put_line('n1 ' || n1);
dbms_output.put_line('n2 ' || n2); The NOCOPY hint tells the PL/SQL
dbms_output.put_line('n3 ' || n3);
compiler to pass OUT and IN OUT
dbms_output.put_line('n ' || n);
END; parameters by reference, rather than by
BEGIN value.
do_something(n, n, n);
dbms_output.put_line('-- out n value -- ' );
dbms_output.put_line(n); -- prints 20
END;
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.

declare 103 declare 104


procedure abc(a number) is x number;
begin
insert into tempp
procedure abs is
values(a,'in abc number'); begin
end; insert into tempp values(1,'in abc');
return 10;
procedure abc(a char) is end;
begin
insert into tempp values(1,a);
begin
end; x:=abc;
insert into tempp values(x,'in main');
begin end;
abc(10); /
abc('hello');
end;
/ In a procedure,
10 in abc number RETURN statement cannot contain an expression
1 hello identifier 'ABC' must be declared
KhiZar M. DhaKam

Functions Local Function


Return value of function should be matched.
Note : Function always return number or char
Function can take input variable of data types
number or char or date.
We have to catch the value return by function.
declare 105 Use of Calculation purpose.
x number :=9;
function abc return number is declare
x number; 106
begin
insert into tempp function abc return number is
values (1, 'In abc'); begin
return 'Hello'; insert into tempp values(1,'in abc');
end; return 10;
Begin end; 1 in abc
begin
10 in main
x:=abc; x:=abc;
insert into tempp values (x, 'In main'); insert into tempp values(x,'in main');
end; end;
/ /

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

Create <or Replace> Procedure <Name> Store Procedure & Function


(argument with in / out / inout parameters)
: PROCEDURE

When store at a database can be globally access.


is Over loading can be possible.
<DEFINE VARIABLES > Syntax for Creating
Store procedure, Can write a Business Logics.
Begin function,
Can be call from local procedure or database
package,
<Select Into clause package body procedure, or triggers or from java or any front-end.
<any DML> if require Function will return values.
SYNTAX

Can call Procedure


Can call Functions Create <or Replace> Package <Name> is
Procedure <name>(variable and parameter);

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

<DEFINE VARIABLES > Create <or Replace> Package body <Name>


Begin is Begin

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

Create or Replace Procedure Insert_Dept_V (dno number, name varchar2, lc varchar2) is


Begin
Insert into Dept_V values( dno, name, lc);
End;

Create or Replace Procedure Upd_Dept_V (dno number, name varchar2) is


Begin
Update dept_v set dname = name where deptno=dno;
End;

Create or Replace Procedure Del_Dept_V (dno number) is


Begin
Delete from Dept_v where deptno = dno;
End;
KhiZar M. DhaKam

Procedure Example1

A Simple Condition Statement


A Simple procedure which will find the
d represent which day of weak.
employee is executive or Normal
1 Sunday, 7 is saturday

create or replace function f_isSunday CREATE OR REPLACE PROCEDURE


0 0
(in_dt DATE) cant_go_there
return VARCHAR2 AS
is l_salary NUMBER := 10000;
v_out VARCHAR2(10); BEGIN
begin IF l_salary > 20000 then
if to_char(in_dt,'d') in (1,7) then dbms_output.put_line ('Executive');
v_out:='Y'; ELSE
DBMS_OUTPUT.put_line('IsSunday=Y'); dbms_output.put_line ('Drone');
end if; END IF;
return v_out; END cant_go_there;
end;
KhiZar M. DhaKam

Examples
for Function

Create or Replace Function Bonus_sal (eno number, bn number)


return number is
Sl number;
Jb varchar2(10);
Com emp.comm%type;
Bonus number;
Begin
select sal, job, comm into sl, jb, com from emp where empno = eno;
if jb = ‘SALESMAN’ THEN
:bonus := sal + bn + nvl(com, 0);
else
:bonus := sal + bn;
end if;
return bonus;
End;
KhiZar M. DhaKam

Create or Replace Package body Dept_pkg is

Examples Procedure inst_dept


(dno number, name varchar2, lc varchar2)

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

Grant can be given to user to execute AUTHID in Procedure & Function


procedure.

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

<any DML> if require FORMULA


Can call Procedure RETURN
Can call Functions Number/Char/Varchar/Boolean
etc. End;
End; SYNTAX / SYNTAX
/
KhiZar M. DhaKam
Authid Example

scott user AUTHID EXAMPLE HR user

Create table aut_dept as select * from dept where 1=2; Create table aut_dept as select * from dept where 1=2;

CREATE OR REPLACE PROCEDURE SQL> EXECUTE scott.aut_pro(10, ‘EDP’,


AUT_PRO (DNO Number, NAME VarChar2, ‘VASHI’);
LC VarChar2)
AUTHID CURRENT_USER AS
BEGIN SQL> Select * from aut_pro;
INSERT INTO AUT
VALUES(DNO, NAME, LC);
END; Check it will not commit as scott user while
/ creating the procedure not used commit in it.

GRANT Execute on AUT_PRO to HR; You can create function with AUTHID

SQL> EXECUTE scott.aut_pro(10, ‘EDP’,


‘VASHI’);

SQL> Select * from aut_pro; You can create function with AUTHID
KhiZar M. DhaKam

Grant can be given to user to execute Pragma


procedure.
AUTONOMUS_TRANSACTION in
Procedure & Function / Package /
Two things are important Triggers
1 initialize in declare
PRAGMA_AUTONOMOUST_TANSACTION
2 COMMIT / ROLLBACK in same
program.

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

<any DML> if require FORMULA


Can call Procedure RETURN
Can call Functions Number/Char/Varchar/Boolean
etc. End;
End; SYNTAX / SYNTAX
/
KhiZar M. DhaKam

Create or Replace package Khi_pkg is Pragma


Procedure Raise_sal ( eno number, rsal
AUTONOMUS_TRANSACTION in
number);
End; Procedure & Function / Package /
Triggers - Example
Create or Replace package body Khi_pkg is
Procedure Raise_sal ( eno number, rsal
number) is Create or Replace Procedure
Pragma Autonomous_transaction; Raise_sal ( eno number, rsal number) is
Begin Pragma Autonomous_transaction;
Package Example

Update emp set sal= sal + rsal Begin


where empno= eno; Update emp set sal= sal + rsal
Commit; where empno= eno;
End; Commit;
End; AUTONOMUS_TRA End;
NSACTION Example /
/

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

Dynamic SQL FOR DDL

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;

Declare Create or Replace add_loc (loc varchar2) is


Statement varhchar2(500); Begin
Name varchar2(20); Execute immediate
Dno number := &d; ‘Create table employee_’ || loc || ‘ (
Lc varchar2(20) := &L; empno number(4),
Begin Ename Varchar2(20),
Statement := ‘update dept set loc=:lc Sal Number(7,2)
where deptno=:dno )’;
returning dname into :n’; End;
Execute immediate statement using lc, dno, out name;
DBMS_OUTPUT.PUT_LINE(Name);
End;

Execute add_loc(‘MURUD’);
KhiZar M. DhaKam

Create or Replace Procedure Dynamic SQL FOR DDL Continue ..


insert_Rec (Dno number, name varchar2, lc
varchar2) is
Statement varchar2(100); Create function Cnt_Rec (Tname varchar2)
Begin return number is
Dynamic SQL FOR
Statement := ‘Insert into dept (deptno,
DDL Continue … Statement varchar2(100);
dname, loc) values(:Dno, :name, :lc); C number;
Execute immediate statement Begin
using dno, name, lc; Statement := ‘Select count(*) from ‘ ||
End; Tname;
Execute immediate statement
into C;
Return C;
Create function Cnt_Rec_JB (jb varchar2)
End;
return number is
Statement varchar2(100);
C number;
Select Cnt_Rec(‘emp’) from dual;
Begin
Statement := ‘Select count(*) from emp
Execute :x := Cnt_Rec(‘EMP’);
where rtrim(job) = :jb ‘; Declare
Execute immediate statement x number;
into C Using JB; Begin
X := Cnt_Rec(‘emp’);
Return C;
DBMS_OUTPUT.PUT_LINE(‘No of Employee’’s are ‘ ||
End; x);
/ End;
KhiZar M. DhaKam

Triggers

Triggers are attached with the Table or Views.

The events that fire a trigger include the following:


DML statements that modify data in a table
(insert, update, or delete)
DDL statements
Before drop/create/alter
System events such as
startup, shutdown, and error messages
User events such as
logon and logoff
KhiZar M. DhaKam

Triggers Referencing values :new, :old


Continue…

Referring :NEW and :OLD values

You can refer new and old values for various statement such as

For INSERT :new can used

For DELETE :old can be used

For UPDATE both :new and :old can be used because row will have
both the old and new values.
KhiZar M. DhaKam

Creating trigger to check the validity of Department code


(should be >=10 and <=100)
before row is inserted into a dept table.

create or replace trigger ins_dept 2 Create table dept_V ( 1


(Example) user should insert deptno in between 10 & 100

before insert on dept_V deptno number(2),


Validity Check using Trigger

For Each Row Dname varchar2(10),


declare Loc varchar2(10)
begin );
If (:new.deptno <10) or (:new.deptno>100)
then Insert into dept_V values 3
Raise_application_error (10, ‘EDP’, ‘MUMBAI’);
(-20110,' invalid department no') ;
end if; 4
end; Insert into dept_V values
/ (1, ‘EDP’, ‘MUMBAI’);

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

Creating trigger which does not allow any DML operation


over the dept table if the user is not Scott.

create or replace trigger UserCheck_trig NOTE:


user can perform DML on table even they have grant.
Validity Check using Trigger Example
Only user Scott can insert records. No Other user

before insert or update or delete on dept_V Even scott give permission to


declare Khizar to perform DML but
1 khizar cannot perform
uname varchar(30);
DML.
begin
select user into uname 2
from dual; Sql> Connect Khizar/Khizar
If uname!=‘SCOTT' --caps
then
Raise_application_error 3
(-20110,'Not a valid user for DML'); Insert into dept values
end if; (1, ‘EDP’, ‘MUMBAI’);
end;
ERROR at line 1:
ORA-20110: Not a valied user for DML
ORA-06512: at "BLACK.USERCHECK_TRIG", line 8
ORA-04088: error during execution of trigger 'BLACK.USERCHECK_TRIG'
KhiZar M. DhaKam

Creating trigger that do not allows DML operation on DEPT table


i.e on Sunday and Saturday DML not allowed

Take a hint from slide 47.

Assignment
Validity Check using Trigger
(Example)

Trigger Ensuring Data Entry During Business Hours


--------------------------------------------------------------------------
Insert, Update and delete are not allowed on Saturday or Sunday.
If DML perform on Saturday or Sunday should give you error.

Insert into dept_V values (1, ‘EDP’, ‘MUMBAI’);


KhiZar M. DhaKam

Altering Triggers / Enabling and Disabling Trigger

You can not alter the trigger, you have to use ‘replace’ or drop and recreate.

A trigger can be disable Temporarily when

Reference objects are not available.


Large (Volume) data is loaded in the table.

Disable : Alter trigger Dept_Sunday disable;


Enable : Alter trigger Dept_Sunday enable;

Disable : Alter table dept disable all triggers;


Enable : Alter table dept enable all triggers;
Information on Triggers is available from Views
• User_triggers
• All_triggers
• Dba_triggers
Rename : Alter trigger <old name> RENAME to <NEW NAME>;
KhiZar M. DhaKam

Writing to File Using UTL_FILE Package

Steps 1. You must have directory access. If not create it.


Step 2. Get grant from dba to create directory structure.
Step 3. Grant create any directory to scott;

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

Output to Excel files

Steps 1. You must have directory access. If not create it.


Step 2. Get grant from dba to create directory structure.
Step 3. Grant create any directory to scott;

Generating Excel files from the database:

The application requirement is as follows: for each manager in the EMP


table, generate an Excel file containing a list of direct reports with name, hire
date and salary. The output files should be written to c:\reports.

The solution is achieved through a PL/SQL procedure, which I'll first list and
then deconstruct. Here it is:
KhiZar M. DhaKam

Output to Excel files Using UTL_FILE

CREATE OR REPLACE procedure print_reports is


cursor c_mgr is select t1.ename, t1.empno from emp t1
where exists (select 'x' from emp t2 where t1.empno=t2.mgr);
cursor c_direct_reports (cv_mgr number) is select empno, ename, job, hiredate, sal
from emp where mgr=cv_mgr;
wfile_handle utl_file.file_type; v_wstring varchar2 (100);
v_header varchar2(100); v_file varchar2(100); v_date varchar2(20);
begin
select to_char(sysdate,'dd_mon_yyyy') into v_date from dual;
v_header := 'empno'||chr(9)||'ename'||chr(9)||'job'||chr(9)||'hiredate'||chr(9)||'sal';
for r_mgr in c_mgr loop
v_file := r_mgr.ename||'_direct_reports_'||v_date||'.xls';
wfile_handle := utl_file.fopen ('REPORTS',v_file, 'W');
utl_file.put_line(wfile_handle,v_header);
for r in c_direct_reports(r_mgr.empno)
loop
v_wstring := r.empno || chr(9) || r.ename || chr(9) || r.job || chr(9) ||
to_char(r.hiredate,'dd/mm/yyyy') || chr(9) || r.sal;
utl_file.put_line(wfile_handle,v_wstring);
end loop;
utl_file.fclose (wfile_handle);
end loop;
end print_reports; EXECUTE print_report;
Will produce the output to excel format at directory you mention.
KhiZar M. DhaKam

11g : Follow Clause in Trigger

Mutating Trigger Fix With Autonomous Transaction CREATE TABLE t1 (x int);


1
Inserting Record in table and selecting data from the

CREATE TABLE t2 (x int);

PRAGMA AUTONOMOUS_TRANSACTION; CREATE OR REPLACE


TRIGGER t_trigger
AFTER INSERT 2
Count on t1 is performed
Mutating Trigger

ON t1
as though a different
FOR EACH ROW
user logged on
DECLARE
same table.

and asked the question of t1.


i PLS_INTEGER;
BEGIN
Commit; SELECT COUNT(*)
INTO i FROM t1;
INSERT INTO t2
The insert into t1 firest the trigger VALUES(i);
which attempts to count the END;
number of records in t1 ...
which is ambiguous.
Insert into T1 values(5);
KhiZar M. DhaKam

!!g 11g : Sequence


Feature

Create Sequence RND_SEQ;


1
Use My_Sequence.Nextval in a PL/SQL expression

Create table Seq_example ( 2 CREATE OR REPLACE


Id Char(6), TRIGGER Seq_trigger
Before INSERT 3
Name Varchar2(10)
); ON Seq_Example
FOR EACH ROW
SEQUENCE

DECLARE

:New.Id := RND_SEQ.NEXTVAL; Id_Code Char(6);


Code Varchar2(6);
BEGIN
SELECT
RND_SEQ.NEXTVAL
INTO Code FROM Dual;

Insert into Seq_Example (Name) :New.ID := Id_Code;


Values(‘Khizar’); END;
KhiZar M. DhaKam

!!g 11g : Follow Clause in Trigger


Feature

CREATE OR REPLACE TRIGGER follows_a CREATE TABLE test (


1
with this one can specify after which order the trigger

3 testcol VARCHAR2(15));
New Follow Clause in Trigger : 11g

AFTER UPDATE
ON test
of the same type the trigger should fire.

FOR EACH ROW


BEGIN INSERT INTO test VALUES
dbms_output.put_line('A'); 2
('dummy');
END follows_a;
/

CREATE OR REPLACE TRIGGER follows_b UPDATE test SET


AFTER UPDATE 5
Testcol = ‘Dummy Cool';
ON test 4
FOR EACH ROW
FOLLOWS follows_a Follows_A Trriger will fire
BEGIN
First
dbms_output.put_line('B');
END follows_b; Follows_B Trigger will fire
/ Second
KhiZar M. DhaKam

!!g 11g : COMPOUND Trigger


Feature

CREATE TRIGGER <trigger_name> FOR <triggering_event>


STATEMENT and ROW LEVEL and BEFORE & AFTER
Its allows for writing a single trigger incorporating

ON <table_name>
New Compound Trigger Syntax

COMPOUND TRIGGER
BEFORE STATEMENT IS
BEGIN
...
END BEFORE STATEMENT;

BEFORE EACH ROW IS


BEGIN
...
END BEFORE EACH ROW;

AFTER STATEMENT IS
BEGIN
...
END AFTER STATEMENT;

AFTER EACH ROW IS


BEGIN
...
END AFTER EACH ROW;
END compound_trigger;
KhiZar M. DhaKam

!!g 11g : COMPOUND Trigger


Feature

CREATE OR REPLACE TRIGGER compound_trig


FOR INSERT ON test COMPOUND TRIGGER 2
CREATE TABLE test AS
New Compound Trigger Example

---------------------------
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

You might also like