You are on page 1of 43

Rakesh Chakrabarti emailtorakesh@gmail.

com

Oracle 10g (grid computing) PL/SQL

Introduction

 Procedural Language SQL


 Programming Language of Oracle
 Used for server side database processing
 Can be called from anywhere (any front-end) like: SQL*Plus forms reports menus
graphics
 Root in PASCAL programming
 4 GL (supports OOPS)
 Case insensitive
 Block level language
 Limitation: Screen i/p or o/p not allowed
 SQL commands allowed: Insert update delete Select Rollback commit savepoint
 DDL/DCL/SQL*Plus commands not allowed: Create drop alter Grant revoke unless use
dynamic SQL
 Execution is top to bottom left to right
 Entire program goes into buffer/ keeps getting overwritten/ to re execute enter /

Structure of PL/SQL block


[
Declare
-- Single Line comment
/* Multiple line
Comment */
Begin ….
… Processing …
{PL/SQL Block sub-block or nesting of block}
End;
/ (Terminator of program first column of last line @the end first character)
] = PL/SQL Block

Advantage of sub-block:
Global and local variables can be declared
Data hiding as encapsulation
Efficient error handling (exception)

Instructions:
Create an output table and put all programs output in Output Table
As the result can’t be shown into the screen send it to Output Table
Table Output: Create table output (File number (4), sec char (15));

Program No 1. p1.sql
SQL>Ed p1.sql
Begin
Insert into output values (1,’iflex’);
End;
/
SQL> @p1
-- compiles and execute

-1-
PL/SQL Procedure successfully completed
SQL>select * from output;

Program No 2. p2.sql
SQL>Ed p2.sql
Declare
X number(4);
-- store a null value take 1 bytes in RAM no garbage collection in PL/SQL
Begin
X:= 100; -- := assignment operator
Insert into output values (x,’iflex’);
End;
/

Program No 3. p3.sql
SQL>Ed p3.sql
Declare
X number(4) := 100;
-- declare and assign value at same time [recommended]
Begin
Insert into output values (x,’iflex’);
End;
/

Program No 4. p4.sql
SQL>Ed p3.sql
Declare
X number(4);
Begin
X:= &x; -- possible because common compiler for SQL SQL*Plus PL/SQL
Insert into output values (x,’iflex’);
End;
/
SQL>@p4
Enter value of x: 100

& works before compilation not at run time similar #define in C


Only for SQL*Plus Prompt/ Debugging | else where not recommended

Program No 5. p5.sql Integration of SQL*Plus + PL/SQL + SQL


SQL>Ed p5.sql
Clea scre
Accept x number prompt “Please enter the Salary”
Declare
X number(4);
Begin
X:= &x;
Insert into output values (x,’iflex’);
End;
/
Undefine x
Select * from output;
Delete output;

-2-
Program No 6. p6.sql
SQL>Ed p6.sql
Clea scre
Accept x number prompt “Please enter the Name”
Declare
X char(15);
Begin
X:= &x;|X:= ‘&x’; |X:= &x;
Insert into output values (1,x);
End;
/
Undefine x
Select * from output;
Delete output;

@p6
Please enter the Name: ’KING’|KING|1234
-- 1234 possible as implicit data type conversion is in Oracle only for number to char

Program No 7. p7.sql
SQL>Ed p7.sql
Declare
X constant number(4) := 1000;
-- constant must be define at declare section otherwise it be an error
Begin
Insert into output values (x,’iflex’);
End;
/

Program No 8. p8.sql
SQL>Ed p8.sql
Declare
X char(15);
Y number(7);
Z number(7);
Begin
X:= ‘&ename’;
Y:= &salary;
Z:= y*0.4;

Insert into output values (Y,X);


Insert into output values (Z,’HRA’);

End;
/

-3-
Program No 9. p9.sql
SQL>Ed p9.sql
Set verify off
Set serveroutput on
-- serveroutput on[off] SQL*plus command
Declare
X char(15);
Y number(7,2);
Z number(6,2);
Begin
X:= '&ename';
Y:= &salary;
Z:= Y*0.4;
DBMS_OUTPUT.PUT_LINE('HELLO');
DBMS_OUTPUT.PUT_LINE('ENAME = '||X);
DBMS_OUTPUT.PUT_LINE('SALARY ='||Y);
DBMS_OUTPUT.PUT_LINE('HRA ='||Z);

-- DBMS_OUTPUT is an Oracle supplied package not a part of standard PL/SQL command


-- Collection of procedures and functions
-- Helps to make PL/SQL interactive
-- PUT_LINE is a procedure of DBMS_OUTPUT package
-- DBMS_OUTPUT.PUT_LINE only for SQL*Plus Prompt
End;
/
undefine X,Y,Z

Program No 10. p10.sql


SQL>Ed p10.sql
Declare
Z number(4);
Begin
Select sal into Z from emp where ename = ‘KING’;
-- sal into Z get data from processing assuming for only 1 record
-- for multiple rows require an array
Insert into output values (Z,’KING’);
End;
/

Program No 11. p11.sql


SQL>Ed p11.sql
Declare
Z number(4);
Y CHAR(15);
Begin
Select sal, job into Z,Y from emp where ename = ‘KING’;
-- sal, job into Z,Y SEQUENCE is most important and data type (precession)
Insert into output values (Z,Y);
End;
/

-4-
Program No 12. p12.sql
SQL>Ed p12.sql
Declare
Z emp.sal%type;
Y emp.job%type;
-- Exact match if column precession changes then it helps
Begin
Select sal, job into Z,Y from emp where ename = ‘KING’;
Insert into output values (Z,Y);
End;
/

Program No 13. p13.sql


SQL>Ed p13.sql
Declare
Z emp%rowtype;
-- Z is a structure like emp entire record in one variable
-- if table structure changes no problem
-- Advantage of Structure: X emp%rowtype; X:=Z; contents of Z to X Helps to data transfer
-- X.sal = Z.sal individual content copy

Begin
Select * from Z where ename = ‘KING’;
Insert into output values (Z.sal,Z.job);
End;
/

Program No 14. p14.sql


SQL>Ed p14.sql
Declare
X emp%rowtype;
Begin
Select * into X from emp where ename = ‘KING’;
-- Select ename,sal,job into X.ename, X.sal, X.job from emp where ename = ‘KING’;
Insert into output values (X.sal,X.job);
End;
/

Program No 15. p15.sql


SQL>Ed p15.sql
Declare
Type pqr is record
( A emp.sal%type;
B emp.job%type; );
X pqr;
-- User defined structure/data type or abstract data type
-- Y pqr; Y: = X;
Begin
Select sal, job into X from emp where ename = ‘KING’;
Insert into output values (X.A,X.B);
End;
/

-5-
Program No 16. p16.sql
SQL>Ed p16.sql
Declare
Type abc is record
( A emp.sal%type;
B emp.job%type; );
Type pqr is record
( L number(4);
M abc; );
X pqr;
-- Structure within structure/ nested-structure
Begin
Select sal, job into X.M from emp where ename = ‘KING’;
-- Select deptno, sal, job into X from emp where ename = ‘KING’;
Insert into output values (X.M.A,X.M.B);
End;
/

Program No 17. p17.sql


SQL>Ed p17.sql
Declare
X number(4);
Begin
Select sal into X from emp where ename = ‘KING’;
If x>4000 then
Insert into output values (X, ‘High sal’);
End if;
-- If statement
End;
/

Program No 18. p18.sql


SQL>Ed p18.sql
Declare
X number(4);
Begin
Select sal into X from emp where ename = ‘KING’;
If x>4000 then
Insert into output values (X, ‘High sal’);
Else
Insert into output values (X, ‘Low sal’);
End if;
-- If-else statement
End;
/

Program No 19. p19.sql


SQL>Ed p19.sql
Declare
x number(4);
Begin
Select sal into X from emp where ename = ‘KING’;

-6-
If x>4000 then
Insert into output values (X, ‘High sal’);
Elsif x<4000 then
Insert into output values (X, ‘Low sal’);
Else
Insert into output values (X, ‘Med sal’);
End if;
-- elsif statement Else if is Nested if..
End;
/

If …………. Then
………………….
Elsif …………… Then
…………………
Elsif …………. Then
………………….
Else
If …………. Then
………………….
Else
…………………
End if;
End if;

Program No 20. p20.sql


SQL>Ed p20.sql
Declare
x Boolean;
-- Boolean variable logical data type [True, False, Null]
-- Only in PL/SQL cant store Boolean in Table
Begin
x:= true;
If x then
-- directly use Boolean variable
Insert into output values (1, ‘Delhi’);
End if;
End;
/

Program No 21. p21.sql


SQL>Ed p21.sql
Declare
x Boolean;
Begin
x:= false;
If not x then
Insert into output values (1, ‘Delhi’);
End if;
End;
/

-7-
Use Loops - For repetitive processing

Program No 22. p22.sql


SQL>Ed p22.sql
Declare
X number(4):= 1;
Begin
-- While loop no increment/decrement operator
While X<10
loop
Insert into output values (X, ‘in while loop’);
X;=X+1;
-- if no condition found while goes for infinite loop
-- to kill the process request the server administrator
End loop;
End;
/
X=1…9

Program No 23. p23.sql


SQL>Ed p23.sql
Declare
X number(4):= 1;
Y number(4):= 1;

Begin
-- While loop no increment/decrement operator
While X<10
Loop
While Y<10
Loop
Insert into output values (Y, ‘in Y loop’);
Y:=Y+1;
End loop;
Insert into output values (X, ‘in X loop’);
X:=X+1;
End loop;
End;
/

X=1
Y =1…9
X = 2….9

Program No 24. p24.sql


SQL>Ed p24.sql
Declare
X number(4):= 1;
Y number(4):= 1;
Begin
-- While loop no increment/decrement operator
While X<10

-8-
Loop
While Y<X
Loop
Insert into output values (Y, ‘in Y loop’);
Y:=Y+1;
End loop;
Insert into output values (X, ‘in X loop’);
X:=X+1;
End loop;
End;
/

X = 1 Y =1
X = 2 Y =2
……………..
X = 8 Y =8
X=9

Program No 25. p25.sql


SQL>Ed p25.sql
Declare
X number(4):= 1;
Y number(4);
Begin
While X<10
Loop
Y:=&SAL;
-- PROMPT FOR ONLY ONNCE &SAL
Insert into output values (Y, ‘in X loop’);
X:=X+1;
End loop;
End;
/

Program No 26. p26.sql


SQL>Ed p26.sql
Declare
X number(4):= 1;
Begin
While X<10
Loop
Insert into output values (X, ‘in X loop’);
If x>5 then
Exit;
End if;
X:=X+1;
End loop;
End;
/

X = 1...6

-9-
Program No 27. p27.sql
SQL>Ed p27.sql
Declare
X number(4):= 1;
Begin
While X<10
Loop
Insert into output values (X, ‘in X loop’);
Exit when x>5;
-- Exit when condition
X:=X+1;
End loop;
End;
/

X = 1...6

Program No 28. p28.sql


SQL>Ed p28.sql
Declare
X number(4):= 100;
Begin
Loop
Insert into output values (X, ‘in X loop’);
Exit when x>5;
-- If only loop … end loop; then go for infinite loop
-- loop … end loop and Exit when condition make a do-while loop

X:=X+1;
End loop;
End;
/

Program No 29. p29.sql


SQL>Ed p29.sql
Declare
X number(4):= 1;
-- optional declaration
-- for loop variable need not be declared scope within the for block
Begin
For X in 1..10
Loop
Insert into output values (X, ‘X for loop’);
-- For Loop Convenience of Programmer
-- Auto increment of variable only for integer (no float)
-- Always increment/decrement by 1 (step value not allowed)
-- .. Only two dots
-- X:=X+2;
-- Gives an error for loop variable is read only can’t be initialized within the loop
End loop;
End;
/

- 10 -
X = 1..10

Program No 30. p30.sql


SQL>Ed p30.sql
Declare
X number(4):= 100;
Begin
Insert into output values (X, ‘X before loop’); -- value of global X
For X in 1..10
Loop
Insert into output values (X, ‘X for loop’); -- value of local X
End loop;
Insert into output values (X, ‘X after loop’); -- value of global X
End;
/
X = 100
X = 1..10
X = 100

Program No 31. p31.sql


SQL>Ed p31.sql
Declare
X number(4):= 100; -- global X
Begin
For X in 1..&y -- local X
Loop
Insert into output values (X, ‘X for loop’); -- value of local X
End loop;
End;
/

Program No 32. p32.sql


SQL>Ed p32.sql
Declare
X number(4):= 100; -- global X
Begin
For X in &X ..&Y -- local X &X = substitution text
-- &X..&Y asks for “Enter value for X.:” it gives error as . is not a valid character
-- Solution = &X ..&Y or &X...&Y
Loop
Insert into output values (X, ‘X for loop’); -- value of local X
End loop;
End;
/

Program No 33. p33.sql


SQL>Ed p33.sql
Declare
X number(4):= 1;
Begin
For X in 10..1
-- o/p is nothing wouldn’t enter the loop
Loop

- 11 -
Insert into output values (X, ‘X for loop’);
End loop;
End;
/

Program No 34. p34.sql


SQL>Ed p34.sql
Declare
X number(4):= 100;
Begin
For X in reverse 1..10
-- use reverse order to decrement
Loop
Insert into output values (X, ‘X for loop’);
End loop;
End;
/
X= 10..1

Program No 35. p35.sql


SQL>Ed p35.sql
Declare
X number(4):= 100;
Begin
For X in 1..10
Loop
For Y in 1..x
Loop
Insert into output values (Y, ‘Y for loop’);
End loop;
End loop;
End;
/

Program No 36. p36.sql


SQL>Ed p36.sql
Declare
X number(4):= 1;
Begin
Insert into output values (X, ‘A’);
Goto abc; -- Goto statement Transfer of control [not recommended]
-- use in deeply nested loop, come out from IF construct
-- To emulate continue stmt. Of ‘C’
<<pqr>>
Insert into output values (X, ‘B’);
X: X+1;
<<abc>>
Insert into output values (X, ‘C’);
If x < 3 then
Goto pqr; -- large no. of GOTO stmt. Known as spagotte code.
End if;
End;
/

- 12 -
Rules for Goto:-
 Can’t use goto to enter loop use only to come out of loop
 Can’t use goto to enter IF construct use only to come out of IF construct
 End, End If, End loop are non executable stmt so there must some executable stmt
between GOTO control and end; end loop; or end if;
Goto abc;
<<abc>>
End; /End loop; End if; -- not allowed
 Solution: use null statement or dummy variable initialization x:= x;
Goto abc;
Null;
<<abc>>
End; /End loop; End if; -- allowed
 Using GOTO from sub-block to super-block is allowed, but super-block to sub-block is not
allowed

Sub-blocks:
 Main/super block can’t access sub-block variable
 Sub-block can access super-block variable and modify its value
 Two sub block within one super block always be independent
Program No 37. p37.sql
SQL>Ed p37.sql
Declare
X number (4):= 100;
Begin
Insert into output values (X, ‘before sub’);
Declare
Y number (4):= 200;
Begin
X; =X+Y;
Insert into output values (Y, ‘in sub’);
Insert into output values (X, ‘in sub’);
End;
Insert into output values (X, ‘after sub’);
-- Insert into output values (Y, ‘in sub’); not allowed
End;
/

Program No 38. p38.sql


SQL>Ed p38.sql
Declare
X number (4):= 100;
Begin
Insert into output values (X, ‘before sub’); -- global variable
Declare
X number (4):= 200;
Begin
Insert into output values (X, ‘in sub’); -- local variable
End;
Insert into output values (X, ‘after sub’); -- global variable
End;
/

- 13 -
Program No 39. p39.sql
SQL>Ed p39.sql
<<abc>> -- create a label
Declare
X number (4):= 100;
Begin
Insert into output values (X, ‘before sub’); -- global variable X
Declare
X number(4):= 200;
Begin
Insert into output values (abc.X, ‘in sub’); -- global variable X
End;
End;
/

<<abc>>
Declare 1
………x….;
Begin
………….;
Declare 2
………x….; call abc.x will call 1st block’s x
Begin
………….;
Declare 3
………x….; call abc.x will call 2nd block’s x
Begin
………….;
End;

End;

End;

<<abc>>
Declare 1
………x….;
Begin
………….;
<<pqr>>
Declare 2
………x….; call abc.x will call 1st block’s x
Begin
………….;
Declare 3
………x….; call abc.x will call 1st block’s x
Begin
………….;
End;

End;

- 14 -
End;
Declare 1
………x….;
Begin
………….;
<<abc>>
Declare 2
………x….; can’t call 3rd block’s pqr.x even with labels declared
Begin
………….;
End;
<<pqr>>
Declare 3
………x….; can’t call 2nd block’s abc.x with labels declared
Begin
………….;
End;
End;
http://in.briefcase.yahoo.com/emailtorakesh

--------------------------------------------------------------------------------------------------------------------

- 15 -
Cursors
 Cursor in all RDBMS (like Record set)
 Read only variable –data present in cursor can’t manipulated
 Can’t Add/Update/Delete in Cursor
 Similar 2D array
 Used for storing/handling/processing multiple rows
 Used for storing data temporarily (in RAM)
 Based on a select statement
 Open Single/one way linked list
Program No 40. p40.sql
SQL>Ed p40.sql
Declare
Cursor c1 is select * from EMP order by sal; -- Cursor declaration / definition [contains no data]
x emp%rowtype; -- only column wise 1 byte full of null value
Begin
Open c1; -- execute select statement and set cursor pointer points to first row/record
For y in 1..5
Fetch c1 to x; -- send 1st row’s data to x [actually return Boolean Value]
-- fetch data from cursor to intermediate variable
-- Cursor fetches data top to bottom / sequentially
-- [DB2 allows going cursor pointer backwards]
-- fetch only 1 row at a time
-- [multi row allowed in Informix sub-cursor concept]
Insert into output values (x.empno, x.ename); -- send data x to output table
End loop; -- 5 times in loop every time x is overwritten
Close c1; -- As cursor is a variable it automatically destroyed
-- CLOSE command to free up RAM
-- Closing the cursor is optional if it’s in last line of program
-- If want to OPEN cursor again for another purpose first close it then re-open it
-- Otherwise returns Error as cursor already opened
-- Cursor is open then Base table updated in cursor no modification done

End;
/
Program No 41. p41.sql “emp table only has 5 rows”
SQL>Ed p41.sql
Declare
Cursor c1 is select * from EMP order by sal;
x emp%rowtype;
Begin
Open c1;
For y in 1..10
Fetch c1 to x;
Insert into output values (x.empno, x.ename);
End loop; -- from 6th loop record of 5th row in EMP table inserted last 5 times.

Close c1;
End;
/

- 16 -
Program No 42. p42.sql -- not a recommended solution. Use Cursor Attributes/Functions:
SQL>Ed p42.sql
Declare
Cursor c1 is select * from EMP order by sal;
x emp%rowtype;
z number(4);
Begin
Select count (*) into z from emp;
Open c1;
For y in 1..z
Fetch c1 to x;
Insert into output values (x.empno, x.ename);
End loop;
Close c1;
End;
/
Cursor Attributes/Functions:
Program No 43. p43.sql
SQL>Ed p43.sql
Declare
Cursor c1 is select * from EMP order by sal;
x emp%rowtype;
Begin
Open c1;
Loop
Fetch c1 to x;
If c1%notfound then -- return a Boolean true value if last fetch is unsuccessful
-- cursor_name%notfound Works based on row id
Exit; -- exit when c1%notfound; (without using if construct)
Insert into output values (x.empno, x.ename);
End if;
End loop;
Close c1;
End;
/
Program No 44. p44.sql
SQL>Ed p44.sql
Declare
Cursor c1 is select * from EMP order by sal;
x emp%rowtype;
Begin
Open c1;
Loop
Fetch c1 to x;
If c1%found then -- return a Boolean true value if last fetch is 4successful
Insert into output values (x.empno, x.ename);
Else
Exit;
End if;
End loop;
Close c1;
End;

- 17 -
/
Program No 45. p45.sql
SQL>Ed p45.sql
Declare
Cursor c1 is select * from EMP order by sal;
x emp%rowtype;
y number(4);
Begin
Open c1;
Loop
Fetch c1 to x;
Exit when c1%notfound;
y: = c1%rowcount; -- how many rows fetched from the cursor so far
Insert into output values (y, x.ename);
End loop;
Close c1;
End;
/

Program No 46. p46.sql


SQL>Ed p46.sql
Declare
Cursor c1 is select * from EMP order by sal;
x emp%rowtype;
Begin
If c1%isopen then -- return a Boolean true value if cursor is already open
Close c1;
End if;
Open c1;
Loop
Fetch c1 to x;
Exit when c1%notfound;
Insert into output values (x.empno, x.ename);
End loop;
Close c1;
End;
/

Program No 47. p47.sql


SQL>Ed p47.sql
Declare
Cursor c1 is select ename,sal from EMP;
 x c1%rowtype; -- structure of x like ‘select ename, sal from EMP’
Begin
 Open c1;
Loop
 Fetch c1 to x;
 Exit when c1%notfound;
Insert into output values (x.sal, x.ename);
End loop;
 Close c1;
End;
/

- 18 -
Program No 48. p48.sql Integration of Cursor with loop *only in Oracle PL/SQL
SQL>Ed p48.sql
Declare
Cursor c1 is select ename,sal from EMP;
Begin
For x in c1 -- itself does the Open, fetch, Exit when c1%notfound and Close cursor.
Loop
Insert into output values (x.sal, x.ename);
End loop;
End;
/

Program No 49. p49.sql


SQL>Ed p49.sql
Declare
Cursor c1 is select * from EMP where deptno = 1; -- deptno = &deptno
Begin
For x in c1
Loop
Insert into output values (x.sal, x.ename);
End loop;
End;
/

Parameter to a Cursor
Program No 50. p50.sql
SQL>Ed p50.sql
Declare
Cursor c1 (dd number) is select * from EMP where deptno = dd;
Begin
For x in c1 (1) -- c1 (&d1) also taken from runtime
Loop
Insert into output values (x.sal, x.ename);
End loop;

For x in c1 (2)
loop
Insert into output values (x.sal, x.ename);
End loop;
End;
/

Program No 51. p51.sql


SQL>Ed p51.sql
Declare
Cursor c1 (dd number, ss number) is select * from EMP where deptno = dd and sal > ss;
Begin
For x in c1 (1, 5000) -- multiple parameter passing separated by comma
Loop
Insert into output values (x.sal, x.ename);
End loop;
End;

- 19 -
/
Program No 52. p52.sql
SQL>Ed p52.sql
Declare
Cursor c1 (dd number default 1) is select * from EMP where deptno = dd ;
Begin
For x in c1 -- specify default value for parameter
Loop
Insert into output values (x.sal, x.ename);
End loop;
End;
/
pfile Init.ora total parameter is 314 in Oracle 10g
OPEN_CURSOR = 300 – to manage cursor opened per session

Program No 53. p53.sql


SQL>Ed p53.sql
Declare
Cursor c1 is select * from EMP;
Cursor c2 is select * from DEPT;

Begin
For x in c1
Loop
For y in c2
Loop
If x.deptno = y.deptno then
Insert into output values (y.empno,x.dname);
End if;
End loop;
End loop;
End;
/

Program No 54. p54.sql --faster way [join works on this logic internally]
SQL>Ed p54.sql
Declare
Cursor c1 is select * from EMP;
Cursor c2 (dd number) is select * from DEPT where deptno = dd;

Begin
For x in c1
Loop
For y in c2 (x.deptno)
Loop
Insert into output values (y.empno,x.dname);
End loop;
End loop;
End;
/

- 20 -
Program No 55. p55.sql --fastest way [use joins] recommended
SQL>Ed p55.sql
Declare
Cursor c1 is select empno,deptno from EMP,dept where dept.deptno = emp.deptno;
Begin
For x in c1
Loop
Insert into output values (x.empno,x.dname);
End loop;
End;
/

Program No 56. p56.sql


SQL>Ed p56.sql
Declare
Cursor c1 is select ename, sal+1 SALARY from EMP;
-- For computed fields, expressions, functions need to provide alias for virtual column
Begin
For x in c1
Loop
Insert into output values (x.SALARY, x.ename);
End loop;
End;
/

Program No 56. p56.sql


SQL>Ed p56.sql
Declare
Cursor c1 is select * from EMP;
Begin
For x in c1
Loop
Update EMP set sal = sal+1;
-- For each time in loop all rows of sal column of EMP updated by 1;
End loop;
End;
/

Program No 57. p57.sql


SQL>Ed p57.sql
Declare
Cursor c1 is select * from EMP;
Begin
For x in c1
Loop
If x.sal > 7000 then
Update EMP set sal = sal+1;
-- For each time in loop if construct is true
-- Then all rows of sal column of EMP updated by 1;
End if;
End loop;
End;

- 21 -
/

Program No 58. p58.sql


SQL>Ed p58.sql
Declare
Cursor c1 is select * from EMP;
Begin
For x in c1
Loop
If x.sal > 7000 then
Update EMP set sal = sal+1 where empno = x.empno;
-- Possibility of duplicity
End if;
End loop;
End;
/

Program No 59. p59.sql


SQL>Ed p59.sql
Declare
Cursor c1 is select * from EMP for update; -- for update [wait 60]
-- If any other user modifies data then might be row-id changes
-- As cursor works upon row-id so using for update is mandatory
Begin
For x in c1
Loop
If x.sal > 7000 then
Update EMP set sal = sal+1 where current of c1;
-- Update the correspondence not all -- solve Possibility of duplicity
-- Also useful for Delete [Delete EMP where current of c1;]
-- Use where current of cursor_name compulsory is for update
End if;
End loop;
Commit;
End;
/

Cursor two types


Explicit – programmer created, declaring done explicit as CURSOR C1, Used for
storing/handling/processing multiple rows, Used for storing data temporarily (in RAM), Locking
Rows Manually
Declare
Cursor c1 is select * from EMP for update;
Begin
Open c1;
Close c1;
End;
/
Implicit – Oracle Created, System Cursor, check the status of last DML statements

- 22 -
(Whether successful or not), Can get a count of how many rows are effected during last DML
operation, maintain logs (audit trails) of DML operation, name of the implicit cursor accessible by
user is SQL.
Program No 60. p60.sql
SQL>Ed p60.sql
Declare
X number(4);
Begin
Update EMP set sal = sal+1 where deptno = 2;
-- Delete EMP where deptno = 2;
-- insert into SEMP select * from EMP;
If SQL%found then -- implicit cursor SQL
X:= SQL%rowcount;
Insert into output values (x, ‘updated’);
End if;
End;
/

- 23 -
Rony_CURSOR ex1

DECLARE
V_RAD NUMBER(5,2);
V_AREA NUMBER(5,2);
CURSOR mycur IS SELECT radius from RAD WHERE radius > 3 AND radius <7;
BEGIN
OPEN mycur;
LOOP
FETCH mycur INTO V_RAD;
EXIT WHEN mycur%notfound;
V_AREA := 3.14 * (V_RAD**2);
INSERT INTO CIRCLE VALUES(V_RAD,V_AREA);
END LOOP;
CLOSE mycur;
END;
/

Rony_CURSOR ex2
DECLARE
V_EMPNO NUMBER(4);
V_DATE DATE;
V_SAL NUMBER(7,2);
CURSOR EMP_AMT IS SELECT EMPNO,SAL FROM EMP WHERE DEPTNO =20;
BEGIN
OPEN EMP_AMT;
LOOP
FETCH EMP_AMT INTO V_EMPNO,V_SAL;
EXIT WHEN EMP_AMT%NOTFOUND;
V_SAL := V_SAL * 0.06;
V_DATE := SYSDATE;
INSERT INTO EMP_RAISE VALUES(V_EMPNO,V_DATE,V_SAL);
END LOOP;
CLOSE EMP_AMT;
END;
/

Rony_CURSOR ex3
DECLARE
V_EMPNO NUMBER(4);
V_TEMP NUMBER(2);
CURSOR EMP_CHK IS SELECT EMPNO FROM EMP2;
BEGIN
OPEN EMP_CHK;
V_TEMP := 0;
LOOP
FETCH EMP_CHK INTO V_EMPNO;
EXIT WHEN EMP_CHK%NOTFOUND;
V_TEMP := V_TEMP + 1;
END LOOP;
IF V_TEMP = 0 THEN
DBMS_OUTPUT.PUT_LINE('TRUE');
ELSE
DBMS_OUTPUT.PUT_LINE('FALSE');
END IF;
CLOSE EMP_CHK;
END;
/

- 24 -
Exceptions - error Handlers:
 Al exceptions group together at end of block
 Within the exception (error handling routine) all PL/SQL statements allowed
 Can’t transfer control to from the exception to the block [compilation error]
 Manage runtime error not Compile time error
 Pre defined exception total 21 exceptions available (frequently encountered errors)
 Error managed only between Begin and End Section not between Declare and Begin
section.
 Error managed only between Declare and Begin section by encapsulating the section
into Parent block
1. too_many_rows
2. no_data_found
3. value_error -- for variable/value/data
4. invalid_number -- for column type
5. zero_divide
6. others -- default Exception

Program No 61. p61.sql


SQL>Ed p61.sql
Declare
X number (4);
Begin
Select sal into x from emp where ename = ‘KING’;
Insert into output values (X, ‘KING’);
Exception when too_many_rows then -- must be at the end of the block
DBMS_OUTPUT.PUT_LINE ('Duplicate Entry for the Name KING');
End;
/

Program No 61. p61.sql


SQL>Ed p61.sql
Declare
X number (4);
Begin
Select sal into x from emp where ename = ‘KINGA’;
Insert into output values (X, ‘KINGA’);
Exception when no_data_found then
DBMS_OUTPUT.PUT_LINE ('No Entry for the Name KING');
End;
/

Program No 62. p62.sql


SQL>Ed p62.sql
Declare
X number (4);
Begin
X:= 10000; -- X: = ‘Pune’; for Datatype mismatch use value_error
Insert into output values (X, ‘KINGA’);

- 25 -
Exception when value_error then -- if value exceed the scale
-- X number (6, 2) X:= 100.786 Oracle automatically round off to 100.79 no error for precision
DBMS_OUTPUT.PUT_LINE (‘Value is to High’);
End;
/
Program No 63. p63.sql
SQL>Ed p63.sql
Declare
X number (4);
Begin
X: = Pune; -- assume Pune as variable
-- for variable not found no exception available in Oracle
End;
/

Program No 64. p64.sql


SQL>Ed p64.sql
Declare
X char (4);
Begin
X: = 100; -- no exception as character conversion done automatic in Oracle
End;
/

Program No 65. p65.sql


SQL>Ed p65.sql
Declare
X number (4);
Begin
Insert into output values (‘Pune’, ‘City’); -- try to insert char value in number column
Exception when invalid_number then
DBMS_OUTPUT.PUT_LINE (‘Pune is not Number value for Number Column’);
End;
/

Program No 66. p66.sql


SQL>Ed p66.sql
Declare
X number (4):= 30;
Y number (4):= 0; -- Y number(4); x:=x/y;divide by null returns null
Begin
X:= X/Y;
Exception when zero_divide then
DBMS_OUTPUT.PUT_LINE (‘Can’t Divide by 0);
End;
/

- 26 -
Declare
……………
X VARCHAR2 (10); Y VARCHAR2 (512);
Begin
……………
Exception When too_many_rows then
……………….
Exception When no_data_found then
………………..
Exception When value_error or invalid_number then
…………………
Exception When zero_divide then
……………….
Exception When others then
………………..
X:= SQLCODE; --SQL ERROR CODE for all types of error in Oracle
Y:= SQLERRM; --SQL ERROR MESSAGE for all types of error in Oracle
Insert into output values (X,Y,SYSDATE)
……………….
End;
/
-- To catch the line no. error as in multiple select stmt. Use a dummy variable & catch the error
using it.
Declare
X number (4);
Begin
x:= 1;
Select …………..;
x:= 2;
Select …………..;
Exception When too_many_rows then
If x=1 then
…………..;
Elsif x=2 then
……………;
Else
…………..;
End if;
End;
/
Declare
…………..
Begin
Declare
X number (4):= 10000;
Begin
Exception When value_error then
Insert into output values (1, ‘in sub error’); -- exception not encountered

- 27 -
End;
Exception When value_error then
Insert into output values (1, ‘in main error’); -- exception encountered here
End;
/
SQL>Ed p67.sql
Declare
X number (4):= 1;
Begin
Insert into output values (X, ‘before sub’);
Declare
X number (4):= 1000;
Begin
Insert into output values (X, ‘in sub’);
Exception When value_error then
Insert into output values (1, ‘in sub eror’);
End;
Insert into output values (X, ‘after sub’);
Exception When value_error then
Insert into output values (1, ‘in main error’); not allowed
End;
/

SQL>Ed p68.sql
Declare
X number (4):= 1;
Begin
Insert into output values (X, ‘before sub’); -- called
Declare
X number (4);
Begin
X:= 10000; -- error occurred
Insert into output values (X, ‘in sub’);
Exception When value_error then
Insert into output values (1, ‘in sub error’); -- called
End;
Insert into output values (X, ‘after sub’); -- called
Exception When value_error then
Insert into output values (1, ‘in main error’);
End;
/

SQL>Ed p69.sql
Declare
X number (4):= 1;
Begin
Insert into output values (X, ‘before sub’); -- called
Declare
X number (4);
Begin
X:= 10000; -- error occurred
Insert into output values (X, ‘in sub’);
End;

- 28 -
Insert into output values (X, ‘after sub’);
Exception When value_error then
Insert into output values (1, ‘in main error’); -- called
End;
/
Exceptions are 2 types
1. Pre-defined Exceptions
 Available with the system
 Automatically invoked by Oracle PL/SQL as required
 Named exceptions 21 others un-Named exceptions 35000 errors
www.ora-code.com
2. User-defined Exceptions
 Programmer created
 Functional rules or business rules
 Raised explicitly

SQL>Ed p70.sql
Declare
X number(4);
User_ex Exception; -- declare User-defined Exceptions
Begin
Select sal into x from emp where ename = ‘KING’;
If X> 5000 then
Raise User_ex; -- raise/define User-defined Exceptions
End if;
Insert into output values (x, ‘KING’);
Exception When User_ex then -- call User-defined Exceptions
Insert into output values (x, ‘High Sal’); -- exception encountered here
End;
/

Give a name to un-Named exceptions using User-defined Exceptions

SQL>Ed p71.sql
Declare
X number(4);
User_ex Exception; -- declare User-defined Exceptions
Pragma exception_init(User_ex,-01843); -- per-compiler directive
Begin
Select sal into x from emp where hiredate = ’10-MMM-94’;
-- confirm the error code [ora-01843 Not a Valid Month]
Exception When User_ex then -- call User-defined Exceptions
Insert into output values (X,‘ora-01843 Not a Valid Month’); -- exception encountered here
End;
/

- 29 -
PL/SQL Arrays
 To store multiple elements of same datatype
 Temporary PL/SQL table
 1-Dimension and 2- Dimension (as cursor) allowed
 For scalar or for composite datatype
 Array size is dynamic
 No need to Start from 0 a(1)…a(n) values/elements subscripts or index
 If subscript is a constant has to be base 10. a(4)
 If subscript is a variable has to be base 10 or base 2. a(x)
 Base 2 or binary integer always faster than integer or base 10
 Conversion of base 10 or base 2 is implicit or automatic
 Range of binary integer -2 billion to +2 billion
 Need not be sequential
 Stored in consecutive memory locations
 Oracle use Array in Encryption and decrypttion

SQL>Ed p72.sql
Declare
Type T is table of number (4) index by binary_integer; -- creation of datatype array
-- Index by binary_integer for using subscript in binary
Y T;
I binary_integer:=1; -- conversion is implicit
Begin
Y(1):= 1000;
Y(2:= 2000;
Y(3):= 3000;
While I < 4
Loop
Insert into output values (y(i),’iflex’);
I:= I+1;
End loop;
End;
/
SQL>Ed p73.sql
Declare
Type T is table of char (15) index by binary_integer;
y T;
Cursor c1 is select ename from emp;
i binary_integer:=1;
j binary_integer:=1;
Begin
For x in c1
Loop
Y(i):= x.ename;
i:=i+1;
End loop;
While j < i

- 30 -
Loop
Insert into output values (j, y (j));
j:=j+1;
End loop;
End;
/
Database Stored Objects
Stored Procedures
 Global procedures
 Stored in the database in the COMPILED Format make a EXE file of Procedure
 Hiding the code from user
 Execution be very fast
 Accessed from SQL*Plus, iSQL*Plus, PL/SQL Programs, forms, Reports, Menus any front-
end tool etc.
 Execution from server side [server RAM] suited for server side data processing
 Only a single copy brought into server RAM for processing (Program code shared by all
users)
 Parameter can be passed
 Put cursor, exception within stored procedure
 To modify first drop it then just recreate it.
 Without dropping just to overwrite use Create or replace
 Overloading of Stored procedure is not allowed
SQL>Ed p74.sql
Create or replace procedure SProc_1
As
X number (2):=10; -- no need to use Declare section
Begin
Insert into output values (X, ‘in procedure’);
End;
/
SQL>@p74.sql
Procedure created -- compiled and stored into database
SQL>execute SProc_1

SQL>Ed p75.sql
Create or replace procedure SProc_2(X number default 10)
As
Begin
Insert into output values (X, ‘in procedure’);
End;
/
SQL>@p75.sql
Procedure created
SQL>execute SProc_2(10)

SQL>Ed p76.sql
Create or replace procedure SProc_3(p number,t number,r number)
As
Intr number(6,2);
Amt number(7,2);
Begin
Intr:= p*t*r/100;

- 31 -
Amt:= p+ Intr
Insert into output values (Intr, ‘Interest’);
Insert into output values (Amt, ‘total Amount’);
Exception When value_error then
Insert into output values (1, ‘value too Large’);
End;
/
SQL>@p76.sql
Procedure created
SQL>execute SProc_3(10000,5,9)

Parameter of 3 types

In
Read only/ pass constant, variables, expressions/ call by value
In is the default value for stored procedure in Oracle

SQL>Ed p77.sql
Create or replace procedure SProc_4(X in number)
As
Begin
-- X:=100; -- not possible as X is IN variable
Insert into output values (X, ‘in procedure’);
End;
/
SQL>@p77.sql
Procedure created

Declare
X number(4):=10;
Begin
SProc_4(X); -- possible
SProc_4(5); -- possible
SProc_4(2*X+10); -- possible
End;
/

Out Write only/ variables only


SQL>Ed p78.sql
Create or replace procedure SProc_5(X OUT number) -- can read from other
As
Begin
X:=100; -- possible as X is OUT variable
Insert into output values (X, ‘in procedure’);
End;
/
SQL>@p78.sql
Procedure created
Declare
y number(4):=10;
Begin
Insert into output values (y, ‘before procedure’); -- y=10

- 32 -
SProc_5(y); -- refer to same address of X variable of SProc_5
-- return the value of X=100
-- it is the Call by Reference process
Insert into output values (y, ‘after procedure’); -- y=100
End;
/

In Out Read & Write

SQL>Ed p79.sql
Create or replace procedure SProc_6(X IN OUT number) -- can read from other
As
Begin
X:=X*X*X; -- possible as X is IN OUT variable
Insert into output values (X, ‘in procedure’);
End;
/
SQL>@p79.sql
Procedure created

Declare
y number(4):=10;
Begin
Insert into output values (y, ‘before procedure’); -- y=10
SProc_6(y); -- refer to same address of X variable of SProc_5
-- return the value of X=1000
-- It is the Call by Reference process
Insert into output values (y, ‘after procedure’); -- y=1000
End;
/

Select * from user_objects where object_type = upper(‘Procedure’);


Select * from user_source;
Select text from user_source where name = upper(‘SProc_6’);

Spool abc
Select text from user_source where name = upper(‘SProc_6’);
Spool off

select distinct type from user_source;


select distinct type from all_source;
select distinct type from dba_source;

- 33 -
Stored functions
 Routine that returns a value directly and compulsorily (void not accepted/null permitted)
 All properties same as Stored Procedures
 Can’t call at SQL Prompt
 Function can’t call by itself must equate with a variable
 Can be called in SQL commands

SQL>Ed p80.sql
Create or replace function SFunc_1(X number) return number
-- must return some value
As
Begin
X:=X*X*X;
End;
/
SQL>@p80.sql
Procedure created

Declare
Y number(4);
Begin
Y:= SFunc_1 (10);
Insert into output values (y, ‘after function’); -- result y=1000
End;
/

SQL>Ed p81.sql
Create or replace function SFunc_2(X number) return Boolean
As
Begin
If X>5000 then
Return true;
Else
Return false;
End if;
End;
/
SQL>@p81.sql
Procedure created

Declare
Y number(4):=&y;
Begin
If SFunc_2 (y) then
Insert into output values (y, ‘after function > 5000’);
else

- 34 -
Insert into output values (y, ‘after function <= 5000’);
End if;
End;
/

SQL>Ed p82.sql
Create or replace function SFunc_3(X number) return number
-- must return some value
As
Begin
X:=X*X*X;
End;
/
SQL>@p82.sql
Procedure created

SQL>select SFunc_3(4) from dual;


SFunc_3(4)
64

Packages (stored object)


 Collection of procedures and functions
 Package resides for the session from its first call in Server side RAM or till Oracle Applies
LRU Algorithm
 Group together the related procedures and functions into a package
 Make a call to any one procedure or function and the entire package loaded into the
server RAM
 Faster Execution
 All properties same as Stored Procedures
 Within the package multiple Procedure or Function can be overloaded if the datatype
parameter passing is different.
 No two procedures and Functions can share same name
 Privileges as User-Sys relation
 globally available if procedure in definition/ locally available if procedure in package body
 Variable are still exist in PGA by Oracle though LRU Algorithm applies.
 Put User defined Exception in Package
 Package Definition and Package body can created separately in separate file.

[The library cache is managed by a least recently used (LRU) algorithm. As the cache fills,
less recently used execution paths and parse trees are removed from the library cache to make
room for the new.
– Shared SQL area /1.1.2 Shared PL/SQL area
The shared PL/SQL area stores and shares the most recently executed PL/SQL statements.
Parsed and compiled program units and procedures (functions, packages, and triggers) are
stored in this area.]

- 35 -
SQL>Ed p83.sql
Create or replace package Pack_1
As
Procedure proc_1; -- globally available if procedure in definition
Procedure Proc_2; -- globally available if procedure in definition
End;
/ -- package Definition/ Specification

Create or replace package body Pack_1


As
Procedure Proc_1 -- can call Proc_3 but cannot call Proc_2
Is
Begin
Insert into output values (1, ‘1st procedure’);
Proc_3;
-- Proc_2; not possible Compilation error[Forward Declaration]
End;
Procedure Proc_2 -- can call Proc_3 and Proc_1
Is
Begin
Insert into output values (2, ‘2nd procedure’);
End;
Procedure Proc_3 -- locally available if procedure in package body
-- Only available in body call by another procedure in package
-- can’t call as SQL> Execute Pack_1.Proc_3
Is
Begin
Insert into output values (2, ‘2nd procedure’);
End;
End;
/
SQL> @p83
SQL> Execute Package_name.Procedure_name -- can’t execute package

Use package in other procedures


Declare
………..
Begin
Package_name.Procedure_name [(parameter)];
End;
/

- 36 -
SQL>Ed p84.sql
Create or replace package Pack_2
As
Procedure proc_1;
Procedure Proc_2;
End;
/
Create or replace package body Pack_2
As
Procedure Proc_1
Is
X number(4):=1;
Begin
Insert into output values (X, ‘1st procedure’);
X:=X+1;
End;
Procedure Proc_2
Is
X number(4):=1;
Begin
Insert into output values (X, ‘2nd procedure’);
X:=X+1;
End;
End;
/
SQL> @p84
SQL> Execute Pack_2.Proc_1 -- result X=1, X=2 X destroyed
SQL> Execute Pack_2.Proc_2 -- result X=1, X=2 X destroyed

Use package in other procedures


Declare
………..
Begin
Package_name.G_Variable_name:= value;
End;
/

SQL>Ed p85.sql
Create or replace package Pack_3
As
Procedure proc_1;
Procedure Proc_2;
Y number(4)=10; -- global variable in oracle
End;
/
Create or replace package body Pack_3

- 37 -
As
X number(4):=0; -- local
Procedure Proc_1
Is
Begin
Insert into output values (X, ‘1st procedure’);
X:=X+1;
End;
Procedure Proc_2
Is
Begin
Insert into output values (X, ‘2nd procedure’);
X:=X+1;
End;
End;
/
SQL> @p85
SQL> Execute Pack_3.Proc_1 -- result X=1, X=2 X remain in RAM
SQL> Execute Pack_3.Proc_2 -- result X=2, X=3 X remain in RAM

SQL>Ed p86.sql
Declare
Begin
Insert into output values (Pack_3.Y, ‘used procedure 1nc’);
Pack_3.Y:= 5;
Insert into output values (Pack_3.Y, ‘used procedure 2wc’);
End;
/
SQL> @p86
Output1 Output1
5 used procedure 1nc’
10 used procedure 2wc’

SQL>Ed p87.sql
Create or replace package Pack_3
As
Procedure proc_1;
Procedure Proc_2;
X number(4); -- don’t initialize
End;
/
Create or replace package body Pack_3
As
Procedure Proc_1
Is
Begin
Insert into output values (X, ‘1st procedure’);
X:=X+1;
End;
Procedure Proc_2
Is
Begin
Insert into output values (X, ‘2nd procedure’);

- 38 -
X:=X+1;
End;
Begin
Select count(*) into X from output
End;
/

Overloading using Package


SQL>Ed p88.sql
Create or replace package Pack_4
As
Function Func_1(s number) return number;
Function Func_1(s number, d number) return number;
End;
/
Create or replace package body Pack_4
As
Function Func_1(s number) return number
Is
Begin
Return 0.1*s;
End;
Function Func_1(s number, d number) return number;
Is
Begin
If d = 1 then
Return 0.2*s;
Else
Return 0.3*s;
End;
End;
/

Triggers (stored object)


 Routine or set of commands Execute automatically when some database event takes
place
 Written upon tables
 Events: BEFORE | AFTER <INSERT/UPDATE/DELETE> ON TABLE_NAME
 Maintain detail logs and summary logs (audit trails) for insertions/deletions/updates
 Stored in database in compiled format
 Triggers at server level [perform DML operation through any front end the trigger will
always fired]
 All PL/SQL statements, Exceptions are allowed in Trigger
 If trigger fails then DML operation also fails and vice versa for DML operation
 Oracle ensure data is consistent
 Trigger changes and DML operation changes automatically rolled back
 Commit and rollback not allowed within a trigger
 Commit and rollback have to be specified afterwards at the end of transaction then the
data will be consistent
 Maximum 12 trigger per table [(statement level + row level)*6 DML operation]
 Automatic data duplication parallel server concept in case of insert
 Shadow Table – in which table the data Inserted from parent table new insertion

- 39 -
 History Table – in which table the data Inserted from parent table deleted data
 Shadow Table/ History Table both can be maintained in Update trigger
 Automatic updating of related table
 :new/:old variables Only in Row level trigger
 To drop
 drop trigger trigger_name; -- table deletion trigger drops
 Alter Trigger trigger_name Disable|enable;
 System table USER_TRIGGERS
SQL>Ed p88.sql
Create or replace trigger Trg_1
BEFORE INSERT
ON EMP -- statement level trigger only once fired [default]
BEGIN
Insert into output values (1, ‘Trigger started’);
End;
/
SQL>@p88
Trigger created

SQL>Ed p89.sql
Create or replace trigger Trg_2
BEFORE INSERT
ON EMP
FOR EACH ROW -- row level trigger fired for every row
BEGIN
Insert into output values (1, ‘Trigger started’);
End;
/

SQL>Ed p90.sql
Create or replace trigger Trg_3
BEFORE INSERT
ON EMP
FOR EACH ROW
BEGIN
Insert into output values (:new.empno, new.ename);
-- Oracle created trigger Variable access the new value
-- Used: new.col_name Only in Row level trigger
End;
/

SQL>Ed p91.sql
CREATE OR REPLACE TRIGGER Trg_4
BEFORE INSERT ON EMP
FOR EACH ROW
BEGIN
: NEW.ENAME:= UPPER (:NEW.ENAME); -- data Cleansing (before insert)
END;
/

SQL>Ed p92.sql
Create or replace trigger Trg_5

- 40 -
BEFORE INSERT
ON EMP
FOR EACH ROW
BEGIN
Update dept set sal = sal +:new.sal where deptno =:new.deptno;
End;
/

SQL>Ed p93.sql
Create or replace trigger Trg_6
BEFORE delete
ON EMP
BEGIN
Insert into output values (1,’deleted’);
End;
/

SQL>Ed p94.sql
Create or replace trigger Trg_7
BEFORE delete
ON EMP for each row
BEGIN
Insert into output values (:old.empno, :old.ename);
-- oracle created trigger Variable access the old value
-- Used: old.col_name Only in Row level trigger
Update dept set sal = sal -:old.sal where deptno =:old.deptno;
End;
/

SQL>Ed p95.sql
Create or replace trigger Trg_8
BEFORE delete
ON EMP for each row
BEGIN
Insert into output values (:old.rownum, ’row deleted’);
End;
/

SQL>Ed p96.sql
Create or replace trigger Trg_9
BEFORE update
ON EMP
BEGIN
Insert into output values (1, ’row updated’);
End;
/

SQL>Ed p97.sql
Create or replace trigger Trg_10
BEFORE update
ON EMP for each row
BEGIN
Insert into output values (:old.empno,:old.ename);

- 41 -
Insert into output values (:new.empno,:new.ename);
End;
/
In oracle using update trigger fire insert trigger and delete trigger in continuity
– known as Cascading Triggers if one failed entire transaction will be rolled back
If the end trigger is an update trigger > Error: Table is undergoing mutation

SQL>Ed p98.sql
Create or replace trigger Trg_11
BEFORE update
ON EMP for each row
BEGIN
Update dept set sal = sal +:new.sal - :old.sal where deptno =:old.deptno;
End;
/

Procedure/function/package/trigger created with compilation error


SQL> Show error
In Oracle two type of Table
--- Relational table : using Standard Oracle datatype
--- Object table : using Abstract Oracle datatype [Object Relational DBMS]
Abstract Datatype (user-defined data type)
 Create your user-defined structure and store in the database as an object
 Saves time during development
 Leads to standardization of structures
 Improves reusability
 Can not use constraints in Abstract Datatype
 Nested Abstract Datatype possible
 Creating Abstract datatype Oracle automatically creates some CONSTRUCTOR METHODS
 To insert rows into Object table use Oracle created CONSTRUCTOR METHODS
 Method name as per the Abstract Datatype name
 Can’t use specific field, to use it use sub datatype

SQL>Create or replace type addr_type as object


(
Street varchar2(20),
City varchar2(15),
State char(2),
Zip number(10)
);
/
Type created
SQL>Desc addr_type

Create or replace type person_type as object


(
name varchar2(20),
address addr_type -- using Nested Abstract Datatype
);
/

- 42 -
SQL>Create table customers(
Custid number(4),
Person person_type);

person_type --- name varchar2(20)


--- addr_type --- Street varchar2(20),
--- City varchar2(15),
--- State char(2),
--- Zip number(10)

Insert into customers


Values(1001, person_type(‘i-flex’, addr_type(‘MG Road’,’Bangalore’,’KN’,500055)));
-- Oracle created CONSTRUCTOR METHODS

Select * from customers; -- can’t give 2D tabular format


Select custid from customers; -- give only custid
Select person from customers; -- person column in total
Select person.name from customers; -- wrong suppose name column from person table
Select customers.person.name from customers; -- it works
Select customers.person.address.city from customers; -- it works

Drop type type_name; -- can’t drop if any Table using it


Select * from user_types;
Select * from all_types; TYPE_NAME
Select * from user_types;

To Raise application error: Raise_ application_error (-20001,”User defined Error”);


Number range: 20001-20999

- 43 -

You might also like