Вы находитесь на странице: 1из 195

PL/SQL

Version 1.0

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
PL/SQL

 Day 1
– Module 1: Introduction to PL/SQL
– Module 2: PL/SQL Programming Constructs
– Module 3: PL/SQL Language Features
– Module 4: SQL in PL/SQL
– Module 5: Control Structures
 Day 2
– Module 6: Stored sub programs (Stored Procedures &
Functions)
– Module 7: Cursors and Data Types as in 3GL

Version 1.0 2
PL/SQL (Contd.).

 Day 3
– Module 8: Cursors
– Module 9: Packages
 Day 4
– Module 10: Exceptions
– Module 11: Triggers
– Module 12: Dependencies among PL/SQL Constructs

Version 1.0 3
Module 1:
Introduction to PL/SQL

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
PL/SQL - INTRODUCTION

 Integrating Programming Features with SQL


Programming Language +SQL=PL/SQL

 PL/SQL submits the request as a Block

Version 1.0 5
What is PL/SQL Block

 PL/SQL Block comprises of


– SQL Statements
– PL features like
• Conditional Statements
• Looping Constructs
– Additional features like
• Stored Procedures
• Exceptions
• Triggers

Version 1.0 6
Why PL/SQL?

 Connectivity exists between consecutive requests


in PL/SQL
 Logical implementation of user’s requirement is
made possible with PL/SQL

Version 1.0 7
PL/SQL Engine

 It is a separator, which splits the SQL and


PL/SQL statements.
 PL/SQL is available
– Client Side
– Server Side

Version 1.0 8
Where PL/SQL stands?

Front end
application Program


…. Query
Invoking
PL/SQL SQL DATABASE
PL/SQL
PROCEDURE Data
….
….

………………..SERVER…………..……..

Version 1.0 9
PL/SQL Block

[Declare

Variable Declarations (if any) ]

Begin

SQL / PL-SQL Statements

[ Exception]
….
End;
Version 1.0 10
Advantages of PL/SQL
 Block structured
– SQL statements are grouped and send as one
consolidated request to the server.
 Improved Performance
– Network traffic can be reduced
 Security
– Access to the data through PL/SQL
– Limited actions allowed over the table through
PL/SQL
 Portable
– Work wherever Oracle Works
Version 1.0 11
Advantages-Contd.

 Compatibility
– Supports all the data types available in
Oracle/SQL.
 Stored Procedures
– Additional Features like Stored procedures
and Triggers are included
– Stored Procedures are available in the server
as database objects and is ready to execute

Version 1.0 12
Nesting of Blocks

 Blocks within the other Block


 Sub Blocks are allowed to enhance the modularity
 Inner blocks follow the similar syntax
 The variables declared in the inner block is valid
till the end of that block
 No limitations on number of nested blocks

Version 1.0 13
Module 2:
PL/SQL Program Constructs

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
PL/SQL Program Constructs

Anonymous
Block Packages

PL/SQL
BLOCK
Stored
Procedure Triggers
/Programs

Procedures Functions

Version 1.0 15
Anonymous Block

 PL/SQL block without a name for identification


 It has to be brought to the server’s memory every
time it is required.
 It can’t be accessed/referred neither by oracle
nor by user for execution
 Has to be compiled every time we need that
block of statements
 Both compilation and execution are done at the
server end.

Version 1.0 16
Stored Procedures

 Named PL/SQL Block


 Stored as Database Objects like Tables, Views,
Indexes, Sequences, Synonyms
 Stored in the ready to executable form
 Can be referred by other PL/SQL block
 One copy is brought to the buffer for sharing by
multiple users

Version 1.0 17
Packages

 Collection of related objects


 The components can be variables, Procedures or
Functions
 Collectively identified as one Database object

Version 1.0 18
Triggers

 The PL/SQL Block which gets fired


automatically whenever some specified event
happens
 The chain of actions to be performed can be
written in the trigger body for continual
execution
 Will be invoked by oracle automatically

Version 1.0 19
Module 3:
PL/SQL Language Features

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Glimpse of PL/SQL Block

Declare
v_designation varchar2(15); Variable declaration

Begin
select job
into v_designation Data Retrieval from Table
into Variable
from emp
where ename='SCOTT';
if (v_designation = 'MANAGER') then
update emp set sal=sal*1.5 where ename='SCOTT';
else
update emp set sal=sal*1.05 where ename='SCOTT';
End if;
End;

Version 1.0 21
Lexical Unit

 Character Set
– Alphabets (both upper and lower)
– Numerals
– Special Characters like #,%,$,etc.,
 Commenting
– Two Consecutive hyphens -- for single line
comment and /*…..*/ for multi line comments
 Delimiter
– Every statement in PL/SQL Block is separated
using semicolon(;)

Version 1.0 22
Variables

 Identifier is the name given for variables


 Identifiers used to hold values that is to be used
in PL/SQL block
 All the variables must be declared before the
begin and end block
 Scope of this variable is within the block

Version 1.0 23
Rules-Identifier

 It is user defined name and should not be oracle


defined names (reserved words)
 It should start with an alphabet
 Can have zero or more additional characters
– 0 to 9 or Alphabets(A-Z or a-z)
– Symbols like # or $ or _ are also allowed
 Follow Standards – Naming Conventions

Version 1.0 24
Data Types

 Primitive Data Type


– Numeric – Number, Integer
– Character –Char,Varchar2,Varchar,Long
– Date
– Boolean
 Composite Data Type
– Record, Tables
– Cursors

Version 1.0 25
Declaration - Example

Syntax is
Variable Name Data Type [ DEFAULT ]
[ NOT NULL ] [ NULL ];
Example
Salary Number(7,2);
HRA Number(7,2) default 3000;
DA Number(7,2):=2000;

Version 1.0 26
Declaration

 Identifiers are to be declared before their usage


 Map to relevant data types
 Memory and initialization will be done every
time it is executed
 Multiple Declaration is not allowed

Version 1.0 27
Scope & Visibility of Variables
(In nested Blocks)

Declare
A number: =200;
BEGIN
…….
Declare --Inner Block Starts
A number:=100; What would be
BEGIN the value of A?

DBMS_OUTPUT.PUT_LINE(A); -- Print A
…..
END; -- Inner Block ends
A is 100 or
DBMS_OUTPUT.PUT_LINE(A); 200?
END;

Version 1.0 28
LABEL << … >>

<<outer>>
Declare
A number: =200;
BEGIN
…….
Declare --Inner Block Starts
A number:=100; Print 100
BEGIN and then 200
DBMS_OUTPUT.PUT_LINE(A || outer.A);
…..
END; -- Inner Block ends
DBMS_OUTPUT.PUT_LINE(A);
END;

Version 1.0 29
Operators-Assignment

 := is the assignment operator in PL/SQL


 Example A:=10;
– Assign 10 to the variable A;
 Data :=null;
– Assign null to the variable ‘Data’
– By default un-initialized variables are assigned
to null irrespective of the data type of the
variable

Version 1.0 30
Operators

 Arithmetic Operators
– * , / , + , - ,mod
 Comparison Operators
– Relational Operators
• <, <=, >, >=, = and != or <> or ~=
– IS NULL, LIKE,BETWEEN,IN
 Logical Operators
– NOT, AND, OR

Version 1.0 31
Built-in-Functions

 Number
 Character
 Conversion
 Date
 Error Reporting
 Others

Version 1.0 32
Number Functions

 ROUND
 TRUNC
 MOD
 POWER,SQRT
 COS,SIN,TAN,LOG,

Version 1.0 33
Character Function

 Lower, Upper, Initcap


 Lpad, Rpad
 Ltrim, Rtrim
 Substr, Concat
 Length
 Replace, Translate

Version 1.0 34
Date -Function

 Sysdate
 Last_day
 Months_between
 Round
 Trunc
 Add_months
 New_time
 Next_day

Version 1.0 35
Conversion Function

 To_char
 To_date
 To_number
 Chartorowid
 Rowidtochar
 Hextoraw
 rawtohex

Version 1.0 36
Error Reporting Functions

 SQLERRM
 SQLCODE

Version 1.0 37
Others - Functions

 DECODE
 GREATEST
 LEAST
 NVL
 USER

Version 1.0 38
Module 4:
SQL In PL/SQL

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Data Retrieval From PL/SQL

 SQL Select statement is the only source for data


retrieval.
 From PL/SQL the data can be retrieved with the
help of Select Statement with added INTO
clause.
 PL/SQL Block can include all DML,TCL
commands and not DDL & DCL commands
directly

Version 1.0 40
INTO Clause
(Data From SQL into PL/SQL)

 Syntax of SELECT statement in PL/SQL is


SELECT column [list]
INTO variable [list]
FROM table [list]
[WHERE condition(s) ]
 SQL data retrieval – SELECT list
 PL/SQL data mapping - INTO list

Version 1.0 41
INTO Clause - Example

DECLARE
v_name varchar2(10);
v_dno number(2);
BEGIN
SELECT ename, deptno

INTO v_name, v_dno


FROM emp
WHERE empno=7788;
dbms_output.put_line(v_name||’is working for
‘||v_dno);
END;

Version 1.0 42
Example – 2

DECLARE
v_name varchar2(10);
Begin
SELECT ename
INTO v_name
FROM emp
WHERE deptno=10;
end;
 What is wrong with this query?
 Will this query work if there are more that 1 employee
working for deptno 10 ?
 Will this query work if there are no employees working
for deptno 10?

Version 1.0 43
Restrictions

 Select statement in PL/SQL can retrieve only one


record at a time
 SELECT & INTO clause should contain
– Equal number of columns list and variable list
– Similar (compatible) data types
– Order of specification of select statement
corresponding to its appropriate variable list

Version 1.0 44
DML in PL/SQL

Begin
v_eno:=7782;
v_new_mgr:=7788
update emp set mgr=v_new_mgr where mgr=v_eno;
insert into dept values(60,’FINANCE’,’TEXAS’);
delete emp where empno=v_eno;
end;
 DML commands can be executed through PL/SQL without
any modification in the syntax.
 DML Can use compatible data items

Version 1.0 45
TCL in PL/SQL

DECLARE
v_sal number(7,2);
BEGIN
update emp set sal=sal*1.50;
select sum(sal)
into v_sal
from emp;
if v_sal > 1000000 then
rollback;
else
commit;
end if;
END;

Version 1.0 46
& Substitution Variable

 Prompt the user for data


 Example
– data:=&value1;
– Select * from emp where deptno=&dno;
– Insert into dept values (&dno, &dname, &loc);
 SQL * PLUS specific command
 Prompts at compile time with the predefined
message ‘enter the value for dno:’

Version 1.0 47
Host variables

 Referred as bind variables


 Usage
– SQL:>VAR X NUMBER --declare variable
– SQL:>EXECUTE :X:=10 --define it
– SQL:>PRINT X --refer it in SQL
 Scope - Session specific

Version 1.0 48
Bind variables - Example

PL/SQL Block 1
Begin
a:=:x; --acts as global variable
:x:=:x + a; --assign 20 to variable x
end;

PL/SQL Block 2
begin
dbms_output.put_line (:x); --prints 20
end;

Version 1.0 49
Module 5:
Control Structures

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Control Structures

 By default control flows in sequential manner -


Executing the statements one after the other
 Control can be transferred as required based on
the logic using any one of the control structures
– Conditional Statements
– Looping Constructs

Version 1.0 51
Conditional Statements

 Branches the execution by checking the


condition
 Can be done using IF statements
 Types of IF statements available are
– IF….THEN….END IF
– IF….THEN….ELSE….END IF
– IF….THEN….ELSIF….END IF

Version 1.0 52
IF Statement -Example

Example 1:
IF (V_JOB=‘CLERK’) THEN
V_BONUS:=2000;
END IF;
Example 2:
IF (V_JOB=‘SALESMAN’) THEN
V_BONUS:=2000;
ELSE
V_BONUS:=1000;
END IF;
Version 1.0 53
IF –THEN-ELSE
Example 3:
IF (V_JOB=‘SALESMAN’) THEN
V_BONUS:=2000;
ELSE
IF (V_JOB=‘CLERK’) THEN
V_BONUS:=3000;
ELSE
V_BONUS:=1000;
END IF;
END IF;
Version 1.0 54
ELSIF -Example

Example 4:
IF (V_JOB=‘SALESMAN’) THEN
V_BONUS:=4000;
ELSIF (V_JOB=‘CLERK’) THEN
V_BONUS:=3000;
ELSIF (V_JOB=‘ANALYST’) THEN
V_BONUS:=2000;
ELSE
V_BONUS:=1000;
END IF;
Version 1.0 55
CASE

 To group multiple conditions


 Syntax is
CASE
WHEN condition THEN
statements;
WHEN condition THEN
statements;
ELSE
statements;
END CASE;

Version 1.0 56
Looping Constructs

 Executes set of statements repeatedly


 Looping Constructs
– LOOP….END LOOP
– WHILE …LOOP…END LOOP
– FOR…IN…LOOP…END LOOP

Version 1.0 57
LOOP…END LOOP

LOOP
…..
…..
END LOOP
 Statements between LOOP and END LOOP will
be executed repeated and also infinitely
 Use EXIT to terminate the endless looping

Version 1.0 58
EXIT

LOOP
…..
EXIT;
….
END LOOP
…..

Version 1.0 59
EXIT WHEN - Example

Loop

EXIT WHEN CNT>10;

END LOOP;

IF (CNT>10) THEN
DBMS_OUTPUT.PUT_LINE (‘Exceeded limit 10’);
EXIT;
END IF;

Version 1.0 60
WHILE LOOP

WHILE (condition)
LOOP

END LOOP;
 Statements between LOOP and END LOOP will
be executed while the condition is true
 Toggle the condition in loop body - to avoid
infinite looping

Version 1.0 61
FOR LOOP

FOR counter IN [REVERSE]


start_value .. final_value
LOOP

END LOOP;
 Implicit declaration – FOR loop variable
 Scope is within the loop

Version 1.0 62
FOR LOOP - Example

FOR cnt IN 1..10 LOOP


dbms_output.put_line (cnt);
END LOOP;

FOR cnt IN REVERSE 1..10 LOOP


dbms_output.put_line (cnt);
END LOOP;

Version 1.0 63
NULL - Statement

 PL/SQL statement used to do nothing


 Blindly transfers the control to the next line
without executing anything
Example
if (a=10) then
NULL;
else
dbms_output.put_line (‘a is not equal to 10’);
end if;

Version 1.0 64
Module 6:
Stored Sub Programs

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Advantages of Subprograms

 Modular approach
 Improved Maintenance
 Stored in an executable format
 Stored as Database objects in the server
 Security
 Parameterized

Version 1.0 66
Types of Parameters

 Formal Parameters
 Actual Parameters

Version 1.0 67
Module 6.1:
Stored Procedures

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Different Modes of Parameters
(IN, OUT, IN OUT)

 IN Parameter
– To send input values to the sub program
 OUT Parameter
– Carry output values to the calling environment
 IN OUT Parameter
– Used in both the directions to send and carry
from the sub block to the calling block

Version 1.0 69
IN Parameter

 Takes input to the sub program


 Formal Parameter
– If mode is not specified treated as IN
parameter
– Is used as constant (IN parameter can’t be
altered inside the sub program)
 The actual Parameter
– Can be variable, literal or an expression
– Should be an initialized variable

Version 1.0 70
OUT Parameter

 Carries the output to the calling environment


 Formal Parameter
– Mode OUT is specified explicitly
– OUT parameter is filled with the output data
 The actual Parameter
– Must be a variable
– Should be un-initialized

Version 1.0 71
IN OUT Parameter

 Send and receive data to and from the sub block


 Formal Parameter
– Mode IN OUT is specified explicitly
– Acts as input as well as output parameter
 The actual Parameter
– Must be a variable
– Should be initialized

Version 1.0 72
Creating the Procedure

CREATE [OR REPLACE] PROCEDURE Procedure name


[(Formal parameter1 [MODE] Data type,
Formal parameter2 [MODE] Data type,
….)]
IS/AS
[Local_variable Data type]
PL/SQL Block

Version 1.0 73
Procedure - Example

Create or replace procedure retrieve(


v_eno IN number,
v_name OUT varchar2,
v_job OUT varchar2)
is
begin
select ename,job
into v_name,v_job
from emp
where empno=v_eno;
end;

Version 1.0 75
Invoking the Procedure

To call the procedure “retrieve” with 1 IN parameter


and 2 OUT parameters
SQL:>variable name varchar2(15)
SQL:>variable job varchar2(15)
SQL:> execute retrieve(7788,:name,:job);

Use PRINT command to know the ouptut


SQL:>PRINT NAME
SQL:>PRINT JOB

Version 1.0 76
Invoking the Procedure
 To call the procedure “retrieve” with 1 IN
parameter and 2 OUT parameters from an
anonymous block
Declare
v_name varchar2(15);
v_job varchar2(15);
Begin
retrieve(7788,v_name,v_job);
End;

Version 1.0 77
Removing the Procedure

 Syntax is
DROP PROCEDURE procedure_name;

 Example
drop procedure retrieve;

Version 1.0 78
Data Dictionary Views

 DESC procedure_name
 DESC USER_SOURCE
 DESC USER_OBJECTS
 DESC USER_DEPENDENCIES

Version 1.0 79
Module 6.2:
Stored Functions

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Stored Function

 Named and stored PL/SQL Block


 Should return at least one value
 Used like Built-in functions
– It can be single row function and not group
function
 Invoked as part of expression
– No separate statement is needed to invoke
functions

Version 1.0 81
Creating the Functions
Syntax is
CREATE [OR REPLACE] FUNCTION function_name
[(Formal parameter1 [MODE] Data_type,
Formal parameter2 [MODE] Data_type,
….)]
RETURN Data_type
IS|AS
PL/SQL Block;

Version 1.0 82
Return Statement

 RETURN data_type
– Only type has to be specified and size
specification is not allowed
– Can be Scalar data type and composite data
types.
– Can return only one value
– Should have at least one return statement in
the PL/SQL block
• RETURN variable;

Version 1.0 83
Example

Create or replace function ret_maxsal (dno IN number)


Return number
IS
v_sal number;
Begin
Select max(sal)
Into v_sal
From emp
Where deptno = dno;
Return v_sal;
End;

Version 1.0 84
Invoking the Function

 SQL:>execute :salary:=ret_maxsal(10)

 Insert into emp (empno, sal, deptno)


values(1234,ret_maxsal(10),50);

 select ename
from emp
where sal>ret_maxsal(20);

Version 1.0 85
Removing the Function

 DROP FUNCTION function_name

Example
– DROP FUNCTION ret_maxsal
Will remove the stored function ret_maxsal.
Also remove the related data dictionary
entries and contents.

Version 1.0 86
Restrictions on using functions

 Single row function and not group function


 Should not have OUT and IN OUT parameters
 In case the functions are invoked from SQL
– Return data type should be a primitive data
type like NUMBER, CHAR, DATE
– Return data type should not be composite
data type which is supported only in PL/SQL

Version 1.0 87
Procedures Vs Functions

PROCEDURE FUNCTION

Invoked as separate Can be invoked as part of


statement expression

Return zero or more Return one value using


values using OUT Return statement
parameter

Version 1.0 88
Data Dictionary Views

 DESC function_name
 DESC USER_SOURCE
 DESC USER_OBJECTS
 DESC USER_DEPENDENCIES
 DESC USER_ERRORS

Version 1.0 89
Module 7:
Cursors and Data types
(as in 3GL)

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
%TYPE

DATA DICTIONARY
Declare
DESC EMP
V_SAL EMP.SAL%TYPE;
…..
SAL NUMBER(7,2)
….

NUMBER(7,2)

Version 1.0 91
%Type

 Declare
v_sal emp.sal%type;

– Implicit data type (structure) specification.
– Refer the data dictionary for EMP table.
– Fetch the current data type of SAL column.
– Map it for the variable v_sal.

Version 1.0 92
%TYPE - Example

Example 1
DECLARE
var1 NUMBER(7,2);
var2 var1 %TYPE;
Example 2
DECLARE
sal emp.sal%TYPE;
eno NUMBER(4);

Version 1.0 93
Example - 1
 Retrieve all the columns of particular employee
Declare
V_eno number(4);
V_name varchar2(15);
V_job varchar2(10);
V_mgr number(4);
V_hiredate date;
V_sal number(7,2);
V_comm number(7,2);
V_dno number(2);
Begin
Select *
Into v_eno,v_ename,v_job,v_mgr,v_hiredate,v_sal,
v_comm,v_dno
……

Version 1.0 94
%ROWTYPE
Declare
myemp emp%rowtype;
Begin
select *
into myemp
….
if (myemp.comm is not null) then …
 Structure of emp table is referred and copied to
variable myemp.
 To access the variables use rowtype variable and
name of the column. i.e.,myemp.comm
Version 1.0 95
Record Type

 User defined, customized data type


 Like structures in C
 Collection of various members
 Create the Data type and declare record variable
which refers the user defined record type

Version 1.0 96
Record Type - Example

DECLARE
TYPE my_data is RECORD
(ename emp.ename%TYPE,
dname dept.dname%TYPE);
my_emp my_data;
BEGIN
SELECT ename,dname
INTO my_emp
FROM emp,dept
WHERE emp.deptno=dept.deptno AND empno=&ENO;
dbms_output.put_line(my_emp.ename ||
my_emp.dname);
END;
Version 1.0 97
Record Type - Example

TYPE WBP IS RECORD


(LTA NUMBER(7,2),
HRA NUMBER(7,2),
fon NUMBER(7,2));
my_allowance WBP;
-- declare variable of record type WBP
BEGIN
my_allowance.LTA:=3000;
my_allowance.fon:=2000;

Version 1.0 98
PL/SQL Table

 Collection of records and not rows


 Store records like arrays
 Single dimensional
 No limits for upper bound
– Can store as many records as needed.

Version 1.0 99
PL/SQL Tables-Example

Version 1.0 100


Built-in-Functions

 Built in functions used to access PL/SQL tables


are
– COUNT
– EXISTS(N)
– FIRST , LAST
– NEXT , PRIOR
– DELETE(N)

Version 1.0 101


Advantages-Composite Data types

 When the structure of base table changes no


need to modify the PL/SQL block
 Refers the current structure and variables are
declared accordingly
 Provides Logical Independence
 %ROWTYPE helps user to avoid individual
member specification
 Helpful when row is retrieved from SQL

Version 1.0 102


Module 8:
Cursors

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
When and Why Cursors
 Fetching more than one record is not possible
with simple variables
 Can be done through cursors
 Cursor is the memory area with pointers
 Temporary memory area used to store data
getting referred during
– PL/SQL fetch operation
– And DML statements

Version 1.0 104


Implicit & Explicit Cursors

 Implicit Cursors
– Handled and controlled by Oracle
– System defined cursors
– Memory area used temporarily for fetching
and DML operation
 Explicit Cursors
– Defined ,Named and used by programmer
– Controlled by user

Version 1.0 105


Cursor Attributes-Implicit

 SQL%ROWCOUNT
– Number of rows affected by the most recent SQL
statement
 SQL%FOUND
– TRUE if the most recent SQL found any row
– Set to TRUE if SQL%ROWCOUNT is <>0
 SQL%NOTFOUND
– TRUE if the most recent SQL didn’t find any row
– Set to true if SQL%ROWCOUNT is =0 or SQL
%Found is false
 SQL%ISOPEN
– Always FALSE because control will be transferred
to the user after closing implicit cursor

Version 1.0 106


Example

Declare
dno number(4);
Begin
dno:=&depno;
delete from emp where deptno=dno;
if (sql%found) then
dbms_output.put_line (sql%rowcount);
end if;
if (sql%notfound) then
dbms_output.put_line ('no employees working for
deptno ‘||dno);
end if;
end;

Version 1.0 107


Stages in Cursors

START

No into in
DECLARE CURSOR select
Declare
Cursor my_cur is select ename,sal from emp;

OPEN CURSOR
open my_cur; Brings the active set
into the cursor area

NP

Version 1.0 108


Stages in Cursors

NP

FETCH CURSOR INTO VARIABLE


Fetch my_cur into cur_rec;

IS
NO
EMPT
Y

YES
CLOSE CURSOR
close my_cur;

STOP

Version 1.0 109


Cursor to Record type

 Fetch cursor into variable


– Fetches the current record from active set to
the PL/SQL variable.
– Data type of the variable depends on structure
of the cursor
– Variable can be declared using %rowtype of
cursor or individual component specification
is also allowed

Version 1.0 110


Cursor to Record type

Example
Declare
Cursor my_cur is
select ename, sal from emp;
cur_rec my_cur%rowtype;
Begin
….
Fetch my_cur into cur_rec;
Example
Declare
….
v_ename emp.ename%type;
v_sal emp.sal%type;
Begin
….
Fetch my_cur into v_ename,v_sal;

Version 1.0 111


Explicit Cursor Attributes

 %ROWCOUNT
– Number of rows fetched so far by the cursor
 %FOUND
– TRUE if the most recent fetch operation found
any row
 %NOTFOUND
– TRUE if the most recent fetch didn’t find any new
record in cursor’s active set
– Set to TRUE if %Found is FALSE
 %ISOPEN
– Shows the status of the cursor
– TRUE if it is open else it is FALSE.

Version 1.0 112


Cursor Attributes-Explicit

Version 1.0 113


Cursor with FOR loop

 For record_variable IN cursor_name


LOOP
….
END LOOP;
 No need
– To declare the record_variable
– To Open
– To Fetch into record_variable
– To Check for new data found or not
– To Close
Version 1.0 114
Example

declare
cursor dep_cur is select * from emp where deptno=10 order by sal;
begin
dbms_output.put_line('-------------------------------');
dbms_output.put_line ('Sl.No. Salary details');
dbms_output.put_line('-------------------------------');
FOR dep_rec IN dep_cur
LOOP
dbms_output.put_line (dep_cur%rowcount||' '||dep_rec.ename
||'''s salary is '||dep_rec.sal);

END LOOP;
dbms_output.put_line('-------------------------------');
end;

Version 1.0 116


Sub queries in FOR loop

 FOR record_variable in (Sub query)


LOOP

END LOOP;
 No need to
– declare the cursor and record variable
– Implicit open, fetch, check for empty, close

Version 1.0 117


Example

Begin
FOR dep_rec IN (select * from emp where deptno=10 order by sal)
LOOP
…..
END LOOP;
End;

 Implicit declaration of cursor is done in the for


loop
 No Access to the Explicit cursor attributes

Version 1.0 118


Cursor with Parameters

Declare
cursor dep_cur (v_did number) is
select count(*) cnt from emp where deptno=v_did ;
dep_rec dep_cur%rowtype;
Begin
open dep_cur(10); --Retrieve dept 10 employees
fetch dep_cur into dep_rec;
dbms_output.put_line (dep_rec. cnt);
close dep_cur;
open dep_cur(20); --Retrieve dept 20 employees
fetch dep_cur into dep_rec;
dbms_output.put_line (dep_rec. cnt);
close dep_cur;
end;

Version 1.0 119


Locking

 Locks the data retrieved for updating


 No separate request is needed during update
operation
 Use “FOR UPDATE” clause in SELECT
statement
 To refer recently fetched record use “WHERE
CURRENT OF cursor name”

Version 1.0 120


FOR UPDATE

 Syntax is
Select …..
FOR UPDATE [OF COLUMN] [NOWAIT]
 Locks the ROWS being read for updating
 Other user’s request for the rows locked will be
in the wait state
 Locks can be released using rollback/commit
 Example
– SELECT * FROM EMP WHERE DEPTNO=10
FOR UPDATE;

Version 1.0 121


WHERE CURRENT OF

 Syntax
WHERE CURRENT OF cursor name;
 Used in Update & Delete commands
 Refers the rows recently fetched by the cursor.
 No additional specifier is needed to refer the
locked rows.
 Example
– DELETE emp WHERE CURRENT OF
my_cur;

Version 1.0 122


Example

Declare
Cursor my_cur IS
Select * from emp where deptno=10
FOR UPDATE;
current_rec my_cur%rowtype;
Begin
….
FETCH my_cur INTO current_rec;
update emp set comm=1000
WHERE CURRENT OF my_cur;
……

Version 1.0 123


Module 10:
Packages

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Introduction to Packages

 Groups related components together and stored


as single object - Package
 Components includes
– Variables
– Sub Programs –Packaged Subprograms
 Functions

 Procedures

– Cursors
– And all PL/SQL allowed Constructs

Version 1.0 125


Advantages of Packages

 Modularity
– Related modules are grouped and kept as one
collection
 Interface
– Provides single and simple interface to access its
components
 Sharable object
– Can be shared by many users at a time
 Minimal disk I/O
– Loads the code into the memory when it is referred
for the first time

Version 1.0 126


Advantages

 Extended Functionality
– Additional facility provided along with stored sub
programs
 Security
– Decide upon the list of operations allowed over
the database objects
 Global Access
– Declared variable are accessible from anywhere
– Data remains throughout the session
 Overloading
– Is allowed with packaged sub programs and not
with stored sub programs

Version 1.0 127


Packages -Restrictions

 Nesting is not allowed


– Creating package within another package is
not allowed
 Parameter
– Parameter can’t be passed in packages
 Calling the Package directly is not possible
– Package can be referred by invoking any of the
components and not the package directly.

Version 1.0 128


Packages

 Components of the package is referred


– Using the package name as primary identifier
– Component name as second identifier
– Separated using a dot operator ‘.’
 Creation of Packages involves two steps
– Package Specification-Header
• Includes details about WHAT it consists of?
– Package Body
• Implements/defines the specification
• Can change the contents without disturbing the
header

Version 1.0 129


Package Specification

 Syntax
CREATE [OR REPLACE] PACKAGE package_name
IS
Variable Declaration;
Sub Program header specification;
END [package_name];

Version 1.0 130


Package Specification

Syntax
CREATE [OR REPLACE] PACKAGE package_name
IS
Variable Declaration;
Sub Program header specification;
END [package_name];

Example
CREATE OR REPLACE PACKAGE bonus_pack
IS
v_bonus NUMBER:=0;
PROCEDURE retrieve_bonus(p_eno NUMBER);
END bonus_pack;

Version 1.0 131


Package Body

CREATE [OR REPLACE] PACKAGE BODY package_name


IS
local variable declaration;
local sub program declaration;
packaged sub program definition;
END [package_name];
 Local variables and sub programs are declared
and used inside the body
 They are local to the Package body -Private

Version 1.0 132


Package Body

1 CREATE OR REPLACE PACKAGE body bonus_pack


2 IS
3 PROCEDURE retrieve_bonus (p_eno NUMBER)
4 IS
5 v_sal NUMBER;
6 BEGIN
7 SELECT sal, comm
8 INTO v_sal, v_bonus
9 FROM emp
10 WHERE empno = p_eno;
11 v_bonus:=v_sal*0.2+v_bonus;
12 END retrieve_bonus;
13* END bonus_pack;
SQL> /
Package body created.

Version 1.0 133


Referencing Package objects

SQL:>EXECUTE bonus_pack.v_bonus:=1000;
SQL:>EXECUTE bonus_pack.retrieve_bonus(7788);

BEGIN
bonus_pack.v_bonus:=200;
dbms_output.put_line(bonus_pack.v_bonus);
….
bonus_pack.retrieve_bonus(7369);
END;

Version 1.0 134


Public and Private Members

 Declarations in package specification are public


members
– Allowed for public reference
 In package body all the declarations made like
local variable, subprograms are private
– Accessible only within the Body and not
allowed for public
– Private member

Version 1.0 135


Private Member -Example
1 CREATE OR REPLACE PACKAGE body bonus_pack
2 IS
3 v_sal NUMBER;
4 FUNCTION retrieve_sal(p_eno NUMBER) Private function
5 RETURN NUMBER
6 IS
7 BEGIN
8 SELECT sal
9 INTO v_sal
10 FROM emp
11 WHERE empno = p_eno;
12 RETURN v_sal;
13 END;
14 PROCEDURE RETRIEVE_BONUS(p_eno NUMBER)
15 IS
16 BEGIN
17 v_bonus:=retrieve_sal(p_eno)*0.2;
18 END retrieve_bonus;
19* END bonus_pack;
SQL> /

Package body created.

Version 1.0 136


Session state -Package variables

 Used as global variable


– Accessible from anywhere
 Initialization is done once per session
– Data are maintained throughout the session
 Scope of the value remains for the current
session.
 Value accessed by one user can’t be
shared/disturbed by other user

Version 1.0 137


Removing the Packages

 To drop the package body


– DROP PACKAGE BODY package_name
 To drop the package header
– DROP PACKAGE package_name
 After removing the package body for the existing
header new body can be created
 But after deleting header the body will be no
longer valid

Version 1.0 138


Overloading

 Overloading is the concept of using the same


name
 Overloading stored sub programs are not
allowed whereas possible with packaged sub
programs
•FUNCTION compute_salary(p_eno NUMBER)
RETURN NUMBER;
•FUNCTION compute_salary(p_ename VARCHAR2,p_doj
DATE)
RETURN NUMBER;
•PROCEDURE print_data(p-job VARCHAR2)
•PROCEDURE print_data(p_ename VARCHAR2,p-job
VARCHAR2)

Version 1.0 139


One time only Procedures

Example
CREATE OR REPLACE PACKAGE dept_pack
IS
v_dept_cnt NUMBER:=0;
PROCEDURE print_details;
END dept_pack;

Version 1.0 140


One time only Procedures

CREATE OR REPLACE PACKAGE BODY dept_pack


IS
PROCEDURE print_details
IS
BEGIN
DBMS_OUTPUT.PUT_LINE (‘No. of Records '|| v_dept_cnt);
END;
BEGIN Gets executed once when loaded for the
SELECT COUNT(*) first time

INTO v_dept_cnt
FROM DEPT;
END;

Version 1.0 141


Example

SQL> EXECUTE dept_pack.print_details


No. of Records4

SQL> INSERT INTO DEPT VALUES(50,'Operations','NY');

SQL> SELECT COUNT(*) FROM DEPT;

COUNT(*)
----------
5

SQL> EXECUTE dept_pack.print_details


No. of Records4

Version 1.0 142


Compiler directives on functions

 Pragma is the compiler directive


 Helps to insist purity level for the packaged
functions
 PRAGMA RESTRICT_REFERENCES
(function_name, WNDS , WNPS , RNDS, RNPS)
– WNDS,WNPS to restrict function updating
database and package variables
– RNDS,RNPS to restrict function reading
database and package variables

Version 1.0 143


Built in packages

 DBMS_OUTPUT
 UTL_FILE
 DBMS_ALERT
 DBMS_LOCK
 DBMS_JOB
 DBMS_SESSION

Version 1.0 144


Module 10:
Exceptions

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Introduction - Exception

 Any abnormal condition to the normal execution


of PL/SQL Block is an Exception
 Way of handling Run time errors

Version 1.0 146


Exception Block

 Syntax
….
EXCEPTION
WHEN name_of_exception1 THEN
handler – PL/SQL statements
WHEN name_of_exception2 THEN
handler
END;

Version 1.0 147


Exception

 Any PL/SQL block can have its own exception


block
 Exception block contains
– Name of the exception thrown
– HANDLER explains the actions to be
performed when error occur
 Exception is not going to solve the current error
condition.

Version 1.0 148


Need for Exception

 When Error occurs


– Abruptly stop executing the current block
– ROLLBACK
 If Exception is Handled
– Stops the current process
– Saves the process done so far
– Transfer the control to exception handler
– Smooth Transition from error condition to
end of program

Version 1.0 149


Demonstrate
 Observe and execute the following code
DECLARE
v_empno EMP.EMPNO%TYPE;
BEGIN
INSERT INTO DEPT VALUES(55,’Operations’,’NY’);
SELECT empno
INTO v_empno
FROM emp
WHERE ename=‘ABC’;
DBMS_OUTPUT.PUT_LINE (v_empno);
END;

 Will the new dept 55 get inserted?


Version 1.0 150
Types of Exception

Exception

Non-
Pre-defined User-
Predefined
Exception defined
Exception

Version 1.0 151


Pre-defined Exceptions

 PL/SQL defined exceptions


 Sensed and mapped to the corresponding
exception handler automatically
 User has to DEFINE it - handler
 No need to DECLARE and INVOKE
•No_data_found •Invalid_number •Cursor_already_open
•Too_many_rows •Zero_divide •Dup_val_on_index

Version 1.0 152


Example
DECLARE
v_empno EMP.EMPNO%TYPE;
Will this get inserted
BEGIN in database?
INSERT INTO DEPT VALUES(55,’Operations’,’NY’);
SELECT empno
INTO v_empno
FROM emp
Will this get printed?
WHERE ename=‘ABC’;
DBMS_OUTPUT.PUT_LINE (v_empno);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE (‘no employee found);
END;
Version 1.0 153
WHEN OTHERS
 Unhandled exception can be trapped in the
OTHERS category
Example
…..
EXCEPTION
…..
WHEN OTHERS THEN
DBMS_OUTPUT,PUT_LINE (SQLCODE);
DBMS_OUTPUT,PUT_LINE (SQLERRM);
END;

 When PL/SQL senses exception, handler is


searched. If specific handler is not found OTHERS
handler is executed
 Use SQLCODE & SQLERRM to print the error code
and message
Version 1.0 154
User defined Exception

 Designed by user as error


 Kind of Logical error
– Manager getting commission > 500 is invalid
 User has to
– DECLARE
– DEFINE &
– INVOKE

Version 1.0 155


User defined Exception

 In Declaration Block - DECLARE


– Exception_name EXCEPTION;
 In Exception Block – DEFINE
– WHEN Exception_name THEN
Handler
 In Execution Block – INVOKE
– RAISE Exception_name

Version 1.0 156


Example
DECLARE
v_comm EMP.comm%TYPE;
v_job EMP.job%TYPE;
invalid_commisn EXCEPTION;
BEGIN
……
IF (v_job=‘MANAGER’ AND v_comm>500) THEN
RAISE invalid_commisn;
END IF;
…..
EXCEPTION
WHEN invalid_commisn THEN
….
END;

Version 1.0 157


Non-Predefined Exception

 Name is not defined by PL/SQL


 PL/SQL identifies such exceptions as errors with
error code
 User has to assign name for it, map a exception
name for the error.
 User need to
– DECLARE and DEFINE it
– Implicit INVOKING is done after user assigns
name
– Use pragma EXEPTION_INIT to associate
user defined exception name with the error
code
Version 1.0 158
Example

DECLARE
unique_constraint EXCEPTION;
PRAGMA EXCEPTION_INIT(unique_constraint,-0001);
BEGIN
…..
INSERT INTO DEPT VALUES(10,'Finance','CA');
…..
EXCEPTION
WHEN unique_constraint THEN
DBMS_OUTPUT.PUT_LINE ('caught exception');
……
END;

 PRAGMA EXCEPTION_INIT
– Associates name “unique constraint” with the error
code -0001
 Exception got declared, handled and not invoked
Version 1.0 159
RAISE APPLICATION ERROR

 Used to print user defined error message

….
RAISE_APPLICATION_ERROR(-20010,'Check Constraint
Violation');

ERROR at line 1:
ORA-20010: Check Constraint Violation

Version 1.0 160


Propagation -Exception

DECLARE
v_empno EMP.EMPNO%TYPE;
BEGIN
INSERT INTO DEPT VALUES(55,’Operations’,’NY’);
SELECT empno
INTO v_empno
FROM emp
WHERE ename=‘ABC’;
DBMS_OUTPUT.PUT_LINE (v_empno);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE (‘no employee found);
END;

Version 1.0 161


Propagation-Nested Blocks

DECLARE

When no_data_found
BEGIN
Exception is raised
….
BEGIN --Inner block begins
…..
EXCEPTION --Exception handler for inner block
WHEN NO_DATA_FOUND THEN
…..
END --Inner block ends
…..
…..
EXCEPTION --Exception handler for outer block
…….
……
END;

Version 1.0 162


Propagation-Nested Blocks
DECLARE

When too_many_rows
BEGIN
Exception is raised
….
BEGIN --Inner block begins
…..
EXCEPTION --Exception handler for inner block
WHEN NO_DATA_FOUND THEN
…..
END --Inner block ends
…..
…..
EXCEPTION --Exception handler for outer block
…….
……
END;

Version 1.0 163


Propagation-Nested Blocks

DECLARE
… When no_data_found Exception
is raised in inner block and
BEGIN
handled in both blocks
….
BEGIN --Inner block begins
…..
EXCEPTION --Exception handler for inner block
WHEN NO_DATA_FOUND THEN
…..
END --Inner block ends
…..
…..
EXCEPTION --Exception handler for outer block
WHEN NO_DATA_FOUND THEN
……
END;

Version 1.0 164


RE-RAISE

DECLARE
… When no_data_found Exception
is raised in inner block and
BEGIN
handled in both blocks
….
BEGIN --Inner block begins
…..
EXCEPTION --Exception handler for inner block
WHEN NO_DATA_FOUND THEN
RAISE;
END --Inner block ends
…..
…..
EXCEPTION --Exception handler for outer block
WHEN NO_DATA_FOUND THEN
……
END;

Version 1.0 165


Module 11:
Triggers

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
What a trigger is?

 PL/SQL block consists of actions to be continued


implicitly for the current event
 Chain of action is to be performed whenever
insertion happens - trigger is the solution for it
 Gets fired automatically/implicitly.
 Treats user’s request and trigger action as one
transaction

Version 1.0 167


Need for a Trigger

 Owner of the database objects can decide upon


the chain of action to happen
 Along with the specified request trigger will also
get executed

Version 1.0 168


Types of Triggers

TRIGGERS

Database Triggers System Triggers Instead of Triggers

Version 1.0 169


Components of Triggers

 Trigger timing
– When to fire trigger after the request or before
the current request
 Triggering event
– For what event this trigger is to be fired
– Is it insert/update/delete
 Table name
– Name of the table for which trigger is

Version 1.0 170


Components of Triggers

 Trigger type
– Two categories of trigger exist –decides upon
how trigger gets fired
– Statement level – once per request
– Row level – for each and every row got
affected trigger is executed
 When clause
– Prepares selected rows for trigger action
– Allowed only for Row level trigger
 Trigger body
– Explains what to be done in the trigger end
Version 1.0 171
Database Triggers

CREATE [OR REPLACE] TRIGGER trigger_name


BEFORE/AFTER
INSERT [OR DELETE OR
UPDATE [OF COLUMNS] ]
ON table_name
PL/SQL BLOCK;

Version 1.0 172


Database Triggers-Example

CREATE TRIGGER salhike_emp


AFTER
UPDATE OF SAL
ON EMP
BEGIN
INSERT INTO
LOG_HIKE(HIKEDATE,USER_NAME)
VALUES(SYSDATE,USER);
END;

Version 1.0 173


Database Triggers-Example

 How many times the previous trigger get


executed if all the 14 employees are eligible for
salary hike?
 Will this trigger help us to store details of the
salary hike, like
– name of the person eligible for hike,
– old salary and
– the revised salary?

Version 1.0 174


Statement level & Row level Trigger

 Decides upon how trigger gets fired


– Statement level
–once per request
– Row level
–for each and every row got affected trigger
body is executed

Version 1.0 175


Row level Triggers

CREATE [OR REPLACE] TRIGGER


BEFORE/AFTER
INSERT [OR UPDATE OR DELETE]
ON table_name
[REFERENCING OLD AS old | NEW AS new]
FOR EACH ROW
[WHEN condition]
PL/SQL Block;

Version 1.0 176


Row level Triggers

CREATE TRIGGER log_hike_employee


AFTER
UPDATE
ON EMP
FOR EACH ROW
BEGIN
INSERT INTO
LOG_HIKE_EMP(EMPNO,PREVSAL,CURSAL)
VALUES(:OLD.EMPNO,:OLD.SAL,:NEW.SAL);
END;

Version 1.0 177


Row level Triggers

CREATE OR REPLACE TRIGGER TR1


AFTER
UPDATE
ON EMP
REFERENCING OLD AS PREVSAL NEW AS CURSAL
FOR EACH ROW
WHEN (PREVSAL.JOB='CLERK')
BEGIN
INSERT INTO
HIK_EMP_HISTRY(EMPNO,PREVSAL,CURSAL)
VALUES(:PREVSAL.EMPNO,:PREVSAL.SAL,
:CURSAL.SAL);
END;

Version 1.0 178


Requirement - Demo

 Whenever salary hike is announced comm is


revised by 10%.
 Will this trigger solve the problem?

create or replace trigger sal_hike_comm


before update of sal on emp
for each row
begin
update emp set comm=comm*1.1 where empno=:old.empno;
end;

Version 1.0 179


Mutation Errors in Triggers

 When the data referred in the user query is used


in its trigger mutation error occurs
 Example
In the trigger written for update event if we try to
delete the rows locked by update command
mutation occurs

Version 1.0 180


Row level Triggers

CREATE TRIGGER sal_hike_rowlevel


BEFORE Will this work for After trigger?
UPDATE
ON EMP
FOR EACH ROW
BEGIN
INSERT INTO
LOG_HIKE_EMP(EMPNO,PREVSAL,CURSAL)
VALUES(:OLD.EMPNO,:OLD.SAL,:NEW.SAL);
:NEW.COMM:=500;
END;

Version 1.0 181


Instead of Triggers

 Triggers are written for Tables and not for views


 Instead of triggers are applicable only for views
and not for tables
 Get fired for any DML operations requested over
the view
– Instead of updating view, modification can be
done somewhere else
– Can be defined in the trigger body

Version 1.0 182


Syntax

CREATE OR REPLACE TRIGGER trigger_name


INSTEAD OF event ON view_name
PL/SQL Block;
 Where event is the DML event which is
requested towards this view_name

Version 1.0 183


Instead of trigger

CREATE OR REPLACE TRIGGER TRG1


INSTEAD OF INSERT ON CLER_VIEW
BEGIN
INSERT INTO LOG(USER_NAME,DTE)
VALUES(USER,SYSDATE);
END;
/

Version 1.0 184


System Triggers

 Considered during the following event


– DDL
• CREATE, ALTER and DROP
– Database events
• server startup/shutdown
• user logon/logoff

Version 1.0 185


Syntax

CREATE OR REPLACE TRIGGER Trigger_name


[BEFORE |AFTER]
[ddl event list| database_event list]
[ON| DATABASE| [schema.][SCHEMA]
[WHEN CLUASE]
[trigger_body]

Version 1.0 186


Example

CREATE TRIGGER scott_login_trigg


AFTER
LOGON
ON SCOTT. schema
WHEN (to_char(SYSDATE,'dy') not in ('sun','sat'))
Begin
insert into log values(user,sysdate);
end;

Version 1.0 187


Example-DDL triggers

CREATE TRIGGER scott_ddl_trigg


AFTER
CREATE ON schema
Begin
insert into log values (user,sysdate);
end;

Version 1.0 188


Restrictions in Triggers

 TCL commands are not allowed


– Treats user’s request and trigger action as one
transaction
– Part of the transactions can be neither
committed nor rollback
 Avoid data/function Mutation

Version 1.0 189


Removing Triggers

 DROP TRIGGER trigger_name


 DROP TRIGGER emp_insert_trigg;
 ALTER TRIGGER trigger_name
ENABLE|DISABLE
 ALTER TABLE table_name ENABLE/DISABLE
ALL TRIGGERS

Version 1.0 190


Data Dictionaries -Triggers

 DESC USER_OBJECTS
 DESC USER_SOURCE
 DESC USER_ERRORS
 DESC USER_TRIGGERS

Version 1.0 191


Module 12:
Dependencies among PL/SQL
Constructs

Copyright © 2007, Wipro Limited, Bangalore


Version 1.0
Dependant and Referenced Objects

 When a Procedure body refers a table,


procedure is dependant on table and table is
referenced by procedure
– Procedure is dependant object –which
depends on table
– Table is referenced object – getting referred
by other objects
 If referenced object is modified dependant
object is affected

Version 1.0 193


Dependencies
 Assume
– stored function FUN1 invokes PROC1.
– PROC1 is getting changed NOW
– Makes FUN1 outdated and change its status to INVALID
– ALTER FUNCTION FUN1 COMPILE
 Similarly
– ALTER FUNCTION function_name COMPILE
– ALTER PROCEDURE procedure_name COMPILE
– ALTER PACKAGE package_name COMPILE PACKAGE
– ALTER PACKAGE package_name COMPILE BODY
– ALTER TRIGGER trigger_name [ENABLE|DISABLE|
COMPILE[DEBUG]

Version 1.0 194


Dependencies - package

 Package body is dependant on header


 Changes in body doesn’t affect specification
 But changes in header affects the body needs to
be reconstructed.

Version 1.0 195


Direct and Remote Dependencies

 Direct dependencies exist if dependant objects


are in the same server
 In case of dependant objects scattered to
different servers remote dependencies exists

Version 1.0 196


Data Dictionary views

 DESC USER_OBJECTS
 DESC USER_SOURCE
 DESC USER_ERRORS
 DESC USER_TRIGGERS
 DESC USER_DEPENDENCIES

Version 1.0 197

Вам также может понравиться