Sie sind auf Seite 1von 98

PL/SQL

Oracle Day 3

O()ecti*es
o e+plain PL/SQL s,(progra-s ,nits . . . . Proce!,res /,nctions Pac0ages Data(ase riggers

Copyright 2005, Infosys echnologies Lt!

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

O*er*ie1 of S,(progra-s
2 s,(progra-3 . Is a na-e! PL/SQL (loc0 that can accept para-eters an! (e in*o0e! fro- a calling en*iron-ent . Is of t1o types3 4 2 proce!,re that perfor-s an action 4 2 f,nction that co-p,tes a *al,e . Is (ase! on stan!ar! PL/SQL (loc0 str,ct,re . Pro*i!es -o!,larity, re,sa(ility, e+tensi(ility, an! -aintaina(ility . Pro*i!es easy -aintenance, i-pro*e! !ata sec,rity an! integrity, i-pro*e! perfor-ance, an! i-pro*e! co!e clarity

Copyright 2005, Infosys echnologies Lt!

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

$loc0 Str,ct,re for 2nony-o,s PL/SQL $loc0s


D"CL2#" 6optional7 Declare PL/SQL o()ects to (e ,se! 1ithin this (loc0 $"8I& 6-an!atory7 Define the e+ec,ta(le state-ents "9C"P IO& 6optional7 Define the actions that ta0e place if an error or e+ception arises "&D: 6-an!atory7

Copyright 2005, Infosys echnologies Lt!

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

$loc0 Str,ct,re for PL/SQL S,(progra-s


<header> IS ; 2S Declaration section $"8I& "+ec,ta(le section "9C"P IO& "+ception section "&D: Subprogram Specification

Subprogram Body

Copyright 2005, Infosys echnologies Lt!

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

PL/SQL S,(progra-s
===== === === === ===== === === === +++ +++ +++ +++ +++ ===== === === +++ === ===== === === === ===== === === === ===== === === +++ === +++ +++ ===== === === +++ === +++ +++ ===== === === === ===== ===+++ === +++=== +++ ===== ===+++ === +++=== +++ +++ +++ +++ +++ +++ +++ Subprogram A, which contains the repeated code

===== === === === ===== === === === A === === ===== === ===== === === === ===== === === === A === === ===== === ===== === === === A === === ===== ===

Code repeated more than once in a PL/SQL program

PL/SQL program invoking the subprogram at mu tip e ocations

Copyright 2005, Infosys echnologies Lt!

<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

$enefits of S,(progra-s
. . . . "asy -aintenance I-pro*e! !ata sec,rity an! integrity I-pro*e! perfor-ance I-pro*e! co!e clarity

Copyright 2005, Infosys echnologies Lt!

>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

@hat Is a Proce!,reA
. . 2 proce!,re is a type of s,(progra- that perfor-s an action' 2 proce!,re can (e store! in the !ata(ase, as a sche-a o()ect, for repeate! e+ec,tion'

Copyright 2005, Infosys echnologies Lt!

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Synta+ for Creating Proce!,res


C!"A#" $%! !"PLAC"& P!%C"'(!" procedurename )parameter1 $mode& datatype1, parameter2 $mode& datatype2, . . .* +S,AS PL/SQL B ock-

. .

#he !"PLAC" option indicates that if the procedure e.ists, it wi be dropped and rep aced with the new version created by the statement/ PL/SQL b ock starts with either B"0+1 or the dec aration of oca variab es and ends with either "1' or "1' procedurename.

Copyright 2005, Infosys echnologies Lt!

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

De*eloping Proce!,res
"ditor Code to create procedure SQL3P us 4 Load and e.ecute file.sql 2 filename.sql

Source code %rac e Compi e P code ".ecute

(se S5%6 "!!%!S to view compi ation errors Procedure created 7


C0 "#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Copyright 2005, Infosys echnologies Lt!

/or-al %ers,s 2ct,al Para-eters


./or-al para-eters3 *aria(les !eclare! in the para-eter list of a s,(progra- specification "+a-ple3 C#"2 " P#OC"DD#" inEsalary6pEi! &DF$"#, pEa-o,nt &DF$"#7 ''' "&D incEsalary: .2ct,al para-eters3 *aria(les or e+pressions reference! in the para-eter list of a s,(progra- call "+a-ple3 inEsalary6*Ei!, 20007

Copyright 2005, Infosys echnologies Lt!

CC

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Proce!,ral Para-eter Fo!es


Procedure Ca ing environment +1 parameter %(# parameter +1 %(# parameter )'"CLA!"* B"0+1 "8C"P#+%1 "1'-

Copyright 2005, Infosys echnologies Lt!

C2

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating Proce!,res 1ith Para-eters


+1 'efau t mode ;a ue is passed into subprogram %(# <ust be specified !eturned to ca ing environment +1 %(# <ust be specified Passed into subprogramreturned to ca ing environment +nitia i9ed variab e <ust be a variab e

:orma parameter acts as (ninitia i9ed a constant variab e Actua parameter can be a itera , e.pression, constant, or initia i9ed variab e Can be assigned a defau t va ue <ust be a variab e

Cannot be assigned a defau t va ue

Cannot be assigned a defau t va ue

Copyright 2005, Infosys echnologies Lt!

C3

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

I& Para-eters3 "+a-ple


2>? p=id

C!"A#" %! !"PLAC" P!%C"'(!" inc=sa ary )p=id +1 emp /emp=id@#AP"* +S B"0+1 (P'A#" emp S"# sa ary B sa ary 3 2/2C 65"!" emp=id B p=id"1' inc=sa ary/

Copyright 2005, Infosys echnologies Lt!

C5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

OD Para-eters3 "+a-ple

Ca ing environment 2DE

QUERY_EMP procedure
p_id

!am DDCC C/7C

p_name p_salary p_incentives

Copyright 2005, Infosys echnologies Lt!

C5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

OD Para-eters3 "+a-ple

C!"A#" %! !"PLAC" P!%C"'(!" Fuery2 )p=id +1 emp /emp=id@#AP", p=name %(# emp /name@#AP", p=sa ary %(# emp /sa ary@#AP", p=comm %(# emp /commission@#AP"* +S B"0+1 S"L"C# name, sa ary, commission +1#% p=name, p=sa ary, p=comm :!%< emp 65"!" emp=id B p=id"1' Fuery2/

Copyright 2005, Infosys echnologies Lt!

C<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

%ie1ing OD Para-eters
. .
Load and run the emp=Fuery/sF script fi e to create the Q("!A2 procedure/ 'ec are host variab es, e.ecute the Q("!A2 procedure, and print the va ue of the g oba 0=1A<" variab e/

;A!+ABL" g=name ;A!+ABL" g=sa ary ;A!+ABL" g=comm

;A!C5A!4)4G* 1(<B"! 1(<B"!

"8"C(#" Fuery2)2DE, Hg=name, Hg=sa , Hg=comm* P!+1# g=name

Copyright 2005, Infosys echnologies Lt!

C>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

%ie1ing I& OD Para-eters


;A!+ABL" g=phone ;A!C5A!4)2G* B"0+1 Hg=phone HB IE2EDDG4?C7GDJ"1'/ P!+1# g=phone "8"C(#" Phone=proc )Hg=phone* P!+1# g=phone

Copyright 2005, Infosys echnologies Lt!

C?

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Fetho!s for Passing Para-eters


. . . Positional3 List act,al para-eters in the sa-e or!er as for-al para-eters' &a-e!3 List act,al para-eters in ar(itrary or!er (y associating each 1ith its correspon!ing for-al para-eter' Co-(ination3 List so-e of the act,al para-eters as positional an! so-e as na-e!'

Copyright 2005, Infosys echnologies Lt!

CB

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

D"/2DL Option for Para-eters

C!"A#" %! !"PLAC" P!%C"'(!" add=department )p=name +1 dep/dep=name@#AP" '":A(L# IabcJ, p= oc +1 dep/ oc=id@#AP" '":A(L# 2ECC* +S B"0+1 +1S"!# +1#% dep)dep=id, dep=name, oc=id* ;AL("S )seF2/1"8#;AL, p=name, p= oc*"1' add=department/

Copyright 2005, Infosys echnologies Lt!

20

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

"+a-ples of Passing Para-eters


B"0+1 add=departmentadd=department )J#!10J, 7GCC*add=department ) p= oc BK 7LCC, p=name BKJ"'(J*add=department ) p= oc BK 74CC* "1'/ S"L"C# dep=id, dep=name, oc=id :!%< dep-

Copyright 2005, Infosys echnologies Lt!

2C

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

'ec aring Subprograms


C!"A#" %! !"PLAC" P!%C"'(!" emp = eave )p=id +1 emp /emp=id@#AP"* +S P!%C"'(!" og=e.ec2 +S B"0+1 +1S"!# +1#% og=tab e2 )user=id, og=date* ;AL("S )(S"!, SAS'A#"*"1' og=e.ec2B"0+1 '"L"#" :!%< emp 65"!" emp=id B p=idog=e.ec2"1' emp= eave/

Copyright 2005, Infosys echnologies Lt!

22

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

In*o0ing a Proce!,re fro- an 2nony-o,s PL/SQL $loc0


'"CLA!" v=id 1(<B"! HB 247B"0+1 inc=sa ary)v=id*- MMinvoke procedure C%<<+#/// "1'-

Copyright 2005, Infosys echnologies Lt!

23

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

In*o0ing a Proce!,re fro- 2nother Proce!,re

C!"A#" %! !"PLAC" P!%C"'(!" emp=process +S C(!S%! c2 +S S"L"C# emp=id :!%< emp B"0+1 :%! i +1 c2 L%%P inc=sa ary)emp=rec/emp oyee=id*"1' L%%PC%<<+#"1' emp=process/

Copyright 2005, Infosys echnologies Lt!

25

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

#e-o*ing Proce!,res
Drop a proce!,re store! in the !ata(ase'

Synta+3 '!%P P!%C"'(!" procedurename "+a-ple3 '!%P P!%C"'(!" inc=sa ary-

Copyright 2005, Infosys echnologies Lt!

25

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Store! /,nctions

. . .

2 f,nction is a na-e! PL/SQL (loc0 that ret,rns a *al,e' 2 f,nction can (e store! in the !ata(ase as a sche-a o()ect for repeate! e+ec,tion' 2 f,nction is calle! as part of an e+pression'

Copyright 2005, Infosys echnologies Lt!

2<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Synta+ for Creating /,nctions

C!"A#" $%! !"PLAC"& :(1C#+%1 functionname )parameter1 $mode& datatype1, parameter2 $mode& datatype2, . . .* !"#(!1 datatype +S,AS PL/SQL B ock-

#he PL/SQL b ock must have at east one !"#(!1 statement/

Copyright 2005, Infosys echnologies Lt!

2>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating a /,nction
"ditor Code to create function iSQL3P us 4 Load and e.ecute filename.sql 2

Source code %rac e Compi e P code +nvoke


Copyright 2005, Infosys echnologies Lt!

:unction created 7
2? "#/CO#P/C#S/D$25/003 %ersion &o' 2'0

"+ec,ting /,nctions
. . . In*o0e a f,nction as part of a PL/SQL e+pression' Create a *aria(le to hol! the ret,rne! *al,e' "+ec,te the f,nction' he *aria(le 1ill (e pop,late! (y the *al,e ret,rne! thro,gh a RETURN state-ent'

Copyright 2005, Infosys echnologies Lt!

2B

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

2!*antages of Dser=Define! /,nctions in SQL "+pressions


. . . "+ten! SQL 1here acti*ities are too co-ple+, too a101ar!, or ,na*aila(le 1ith SQL Can increase efficiency 1hen ,se! in the WHERE cla,se to filter !ata, as oppose! to filtering the !ata in the application Can -anip,late character strings

Copyright 2005, Infosys echnologies Lt!

30

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

+nvoking :unctions in SQL ".pressionsH ".amp e


C!"A#" %! !"PLAC" :(1C#+%1 ta.ca c)p=va +1 1(<B"!* !"#(!1 1(<B"! +S B"0+1 !"#(!1 )p=va 3 C/2G*"1' ta./ S"L"C# emp=id, name, sa ary, ta.ca c)sa ary* :!%< emp 65"!" dep=id B 222-

Copyright 2005, Infosys echnologies Lt!

3C

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Locations to Call Dser=Define! /,nctions


. Select list of a SELECT co--an! . Con!ition of the WHERE an! HAVING cla,ses CONNECT BY, START WITH, ORDER BY, an! GROUP BY cla,ses VALUES cla,se of the INSERT co--an! SET cla,se of the UPDATE co--an!

Copyright 2005, Infosys echnologies Lt!

32

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

#estrictions on Calling /,nctions fro- SQL "+pressions GG''


o (e calla(le fro- SQL e+pressions, a ,ser=!efine! f,nction -,st3 . $e a store! f,nction . 2ccept only IN para-eters . 2ccept only *ali! SQL !ata types, not PL/SQL specific types, as para-eters . #et,rn !ata types that are *ali! SQL !ata types, not PL/SQL specific types

Copyright 2005, Infosys echnologies Lt!

33

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

#estrictions on Calling /,nctions fro- SQL "+pressions


. . . . . /,nctions calle! fro- SQL e+pressions cannot contain DFL state-ents' /,nctions calle! fro- UPDATE/DELETE state-ents on a ta(le cannot contain DFL on the sa-e ta(le ' /,nctions calle! fro- an UPDATE or a DELETE state-ent on a ta(le cannot H,ery the sa-e ta(le' /,nctions calle! fro- SQL state-ents cannot contain state-ents that en! the transactions' Calls to s,(progra-s that (rea0 the pre*io,s restriction are not allo1e! in the f,nction'

Copyright 2005, Infosys echnologies Lt!

35

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

#estrictions on Calling fro- SQL


C!"A#" %! !"PLAC" :(1C#+%1 f2 )p=sa ary 1(<B"!* !"#(!1 1(<B"! +S B"0+1 +1S"!# +1#% emp oyees)emp=id, name, mai , Noin=date, Nob=id, sa ary* ;AL("S)2, ILisaJ, ILisaO+1:A/comJ, SAS'A#", I"'(CA#%!J, ECCC*!"#(!1 )p=sa ary P 22CC*"1'/ (P'A#" emp S"# sa ary B f2)4CCC* 65"!" emp=id B 242-

Copyright 2005, Infosys echnologies Lt!

35

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

!emoving :unctions
Drop a store! f,nction'

Synta.H
DROP FUNCTION FUNCTION ffunction_name DROP !

".amp eH .A the privi eges granted on a function are revoked


when the function is dropped/

.#he C!"A#" %! !"PLAC" synta. is eFuiva ent to

dropping a function and recreating it/ Privi eges granted on the function remain the same when this synta. is used/

Copyright 2005, Infosys echnologies Lt!

3<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Io1 Proce!,res an! /,nctions Differ

Procedure

:unction

Ca ing environment

+1 parameter %(# parameter +1 %(# parameter


)'"CLA!"* B"0+1 "8C"P#+%1 "1'-

Ca ing environment

+1 parameter

)'"CLA!"* B"0+1 "8C"P#+%1 "1'-

Copyright 2005, Infosys echnologies Lt!

3>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Comparing Procedures and :unctions


Procedures ".ecute as a PL/SQL statement 'o not contain !"#(!1 c ause in the header Can return none, one, or many va ues Can contain a !"#(!1 statement :unctions +nvoke as part of an e.pression <ust contain a !"#(!1 c ause in the header <ust return a sing e va ue <ust contain at east one !"#(!1 statement

Copyright 2005, Infosys echnologies Lt!

3?

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

$enefits of Store! Proce!,res an! /,nctions


. . . . I-pro*e! perfor-ance "asy -aintenance I-pro*e! !ata sec,rity an! integrity I-pro*e! co!e clarity

Copyright 2005, Infosys echnologies Lt!

3B

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

List 2ll Proce!,res an! /,nctions

S"L"C# obNect=name, obNect=type :!%< user=obNects 65"!" obNect=type in )JP!%C"'(!"J,J:(1C#+%1J* %!'"! BA obNect=name-

Copyright 2005, Infosys echnologies Lt!

50

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

DS"#ESOD#C" Data Dictionary %ie1

Co umn 1A<" #AP"

Co umn 'escription 1ame of the obNect #ype of obNect, for e.amp e, P!%C"'(!", :(1C#+%1, PACQA0", PACQA0" B%'A Line number of the source code #e.t of the source code ine

L+1" #"8#

Copyright 2005, Infosys echnologies Lt!

5C

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

List the Co!e of Proce!,res an! /,nctions

S"L"C# te.t :!%< user=source 65"!" name B IP2J %!'"! BA ine-

Copyright 2005, Infosys echnologies Lt!

52

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

List Co-pilation "rrors (y Dsing SIO@ "##O#S

S5%6 "!!%!S P!%C"'(!" p2

Copyright 2005, Infosys echnologies Lt!

53

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

De(,gging PL/SQL Progra- Dnits

he DBMS_OUTPUT pac0age3
4 2cc,-,lates infor-ation into a (,ffer 4 2llo1s retrie*al of the infor-ation fro- the (,ffer

. .

2,tono-o,s proce!,re calls 6for e+a-ple, 1riting the o,tp,t to a log ta(le7 Soft1are that ,ses DBMS_DEBUG
4 4 Proce!,re $,il!er hir!=party !e(,gging soft1are

Copyright 2005, Infosys echnologies Lt!

55

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

O*er*ie1 of Pac0ages
Pac0ages3 . 8ro,p logically relate! PL/SQL types, ite-s, an! s,(progra-s . Consist of t1o parts3 4 Specification 4 $o!y . Cannot (e in*o0e!, para-eteriJe!, or neste! . 2llo1 the Oracle ser*er to rea! -,ltiple o()ects into -e-ory at once

Copyright 2005, Infosys echnologies Lt!

55

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Co-ponents of a Pac0age
Pub ic variab e Procedure A dec aration Pub ic procedure

Package specification

Private variab e Package body Procedure B definition Procedure A definition Private procedure

Pub ic procedure Loca variab e

Copyright 2005, Infosys echnologies Lt!

5<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

De*eloping a Pac0age

Sa*ing the te+t of the CREATE PACKAGE state-ent in t1o !ifferent SQL files facilitates later -o!ifications to the pac0age' 2 pac0age specification can e+ist 1itho,t a pac0age (o!y, (,t a pac0age (o!y cannot e+ist 1itho,t a pac0age specification'

Copyright 2005, Infosys echnologies Lt!

5>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating the Pac0age Specification


Synta.H

C!"A#" $%! !"PLAC"& PACQA0" package_name +S,AS public type and item declarations subprogram specifications "1' package_name; . . . he #"PL2C" option !rops an! recreates the pac0age specification' %aria(les !eclare! in the pac0age specification are initialiJe! to &DLL (y !efa,lt' 2ll the constr,cts !eclare! in a pac0age specification are *isi(le to ,sers 1ho are grante! pri*ileges on the pac0age'

Copyright 2005, Infosys echnologies Lt!

5?

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Declaring P,(lic Constr,cts

COMM_P"C#"$E package
$_COM Package specification

RE%ET_COM procedure dec aration

Copyright 2005, Infosys echnologies Lt!

5B

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating a Pac0age Specification3 "+a-ple

C!"A#" %! !"PLAC" PACQA0" com=pack +S g=com 1(<B"! HB C/2GP!%C"'(!" reset=com )p=com +1 1(<B"!*"1' com=pack/

. .

0=C%< is a g oba variab e and is initia i9ed to C/2G !"S"#=C%< is a pub ic procedure that is imp emented in the package body/

Copyright 2005, Infosys echnologies Lt!

50

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating the Pac0age $o!y


Synta+3 C!"A#" $%! !"PLAC"& PACQA0" B%'A package_name +S,AS private type and item declarations subprogram bodies "1' package_name;

. . .

#he !"PLAC" option drops and recreates the package body/ +dentifiers defined on y in the package body are private constructs/ #hese are not visib e outside the package body/ A private constructs must be dec ared before they are used in the pub ic constructs/

Copyright 2005, Infosys echnologies Lt!

5C

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

P,(lic an! Pri*ate Constr,cts


COMM_P"C#"$E package
$_COM RE%ET_COM procedure dec aration

Package specification

2 4

Package body

&"'ID"TE_COM function definition

RE%ET_COM procedure definition

Copyright 2005, Infosys echnologies Lt!

52

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating a Pac0age $o!y3 "+a-ple

C!"A#" %! !"PLAC" PACQA0" B%'A com=pack +S :(1C#+%1 va idate=comm )p=com +1 1(<B"!* !"#(!1 B%%L"A1 +S v=ma.=com 1(<B"!B"0+1 S"L"C# <A8)commission* +1#% v=ma.=com :!%< emp +: p=com K v=ma.=com #5"1 !"#(!1):ALS"*"LS" !"#(!1)#!("*"1' +:"1' va idate=com///

Copyright 2005, Infosys echnologies Lt!

53

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating a Pac0age $o!y3 "+a-ple

P!%C"'(!" reset=com )p=com +1 1(<B"!* +S B"0+1 +: va idate=com)p=com* #5"1 g=comHBp=com- MMreset g oba variab e "LS" !A+S"=APPL+CA#+%1="!!%!)M4C444,ICommission +nva idJ*"1' +:"1' reset=com"1' com=pack/

Copyright 2005, Infosys echnologies Lt!

55

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

In*o0ing Pac0age Constr,cts


"+a-ple C3 In*o0e a f,nction fro- a proce!,re 1ithin the sa-e pac0age' CRE"TE OR REP'"CE P"C#"$E (ODY c)m_pac* I% + + + PROCEDURE reset_c)m ,p_c)m IN NUM(ERI% (E$IN IF validate_c)m,p_c)mT.EN /_c)m 01 p_c)m! E'%E R"I%E_"PP'IC"TION_ERROR ,2343335 6C)mmissi)n Invalid 7-! END IF! END reset_c)m! END c)m_pac*!
Copyright 2005, Infosys echnologies Lt! 55 "#/CO#P/C#S/D$25/003 %ersion &o' 2'0

#e-o*ing Pac0ages

#o remove the package specification and the body, use the fo owing synta.H '!%P PACQA0" package_name;

#o remove the package body, use the fo owing synta.H '!%P PACQA0" B%'A package_name;

Copyright 2005, Infosys echnologies Lt!

5<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

8,i!elines for De*eloping Pac0ages


. . . . Constr,ct pac0ages for general ,se' Define the pac0age specification (efore the (o!y' he pac0age specification sho,l! contain only those constr,cts that yo, 1ant to (e p,(lic' Place ite-s in the !eclaration part of the pac0age (o!y 1hen yo, -,st -aintain the- thro,gho,t a session or across transactions' Changes to the pac0age specification reH,ire reco-pilation of each referencing s,(progra-' he pac0age specification sho,l! contain as fe1 constr,cts as possi(le'

. .

Copyright 2005, Infosys echnologies Lt!

5>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

2!*antages of Pac0ages
. . . Fo!,larity3 "ncaps,late relate! constr,cts' "asier application !esign3 Co!e an! co-pile specification an! (o!y separately' Ii!ing infor-ation3 4 Only the !eclarations in the pac0age specification are *isi(le an! accessi(le to applications' 4 Pri*ate constr,cts in the pac0age (o!y are hi!!en an! inaccessi(le' 4 2ll co!ing is hi!!en in the pac0age (o!y'

Copyright 2005, Infosys echnologies Lt!

5?

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

2!*antages of Pac0ages
. . 2!!e! f,nctionality3 Persistency of *aria(les an! c,rsors $etter perfor-ance3 4 he entire pac0age is loa!e! into -e-ory 1hen the pac0age is first reference!' 4 here is only one copy in -e-ory for all ,sers' 4 he !epen!ency hierarchy is si-plifie!' O*erloa!ing3 F,ltiple s,(progra-s of the sa-e na-e

Copyright 2005, Infosys echnologies Lt!

5B

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

O*erloa!ing
. . . "na(les yo, to ,se the sa-e na-e for !ifferent s,(progra-s insi!e a PL/SQL (loc0, a s,(progra-, or a pac0age #eH,ires the for-al para-eters of the s,(progra-s to !iffer in n,-(er, or!er, or !ata type fa-ily "na(les yo, to (,il! -ore fle+i(ility (eca,se a ,ser or application is not restricte! (y the specific !ata type or n,-(er of for-al para-eters &ote3 Only local or pac0age! s,(progra-s can (e o*erloa!e!' Ko, cannot o*erloa! stan!=alone s,(progra-s'

Copyright 2005, Infosys echnologies Lt!

<0

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

%ver oadingH ".amp e

C!"A#" %! !"PLAC" PACQA0" over=package +S P!%C"'(!" add=department )p=deptno +1 dep/dep=id@#AP", p=name +1 dep/dep=name@#AP" '":A(L# IABCJ, p= oc +1 dep/ oc=id@#AP" '":A(L# 222*P!%C"'(!" add=department )p=name +1 dep/dep=name@#AP" '":A(L# IABCJ, p= oc +1 dep/ oc=id@#AP" '":A(L# 222*"1' over=package/

Copyright 2005, Infosys echnologies Lt!

<C

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Dsing /or1ar! Declarations

Ko, -,st !eclare i!entifiers (efore referencing the-' C!"A#" %! !"PLAC" PACQA0" B%'A forward=package +S P!%C"'(!" bonus)/ / /* +S B"0+1 ca c=rating)/ / /*MMi ega reference "1'P!%C"'(!" rating)/ / /* +S B"0+1 /// "1'"1' forward=package/
Copyright 2005, Infosys echnologies Lt! <2 "#/CO#P/C#S/D$25/003 %ersion &o' 2'0

(sing :orward 'ec arations


C!"A#" %! !"PLAC" PACQA0" B%'A forward=package +S P!%C"'(!" rating)/ / /*P!%C"'(!" bonus)/ / /* +S defined B"0+1 rating)/ / /*/// "1'P!%C"'(!" rating)/ / /* +S B"0+1 /// "1'"1' forward=package/
Copyright 2005, Infosys echnologies Lt! <3 "#/CO#P/C#S/D$25/003 %ersion &o' 2'0

MM forward dec aration MM subprograms MM in a phabetica order

#estrictions on Pac0age /,nctions Dse! in SQL


2 f,nction calle! fro-3 . . . 2 H,ery or DFL state-ent can not en! the c,rrent transaction, create or roll (ac0 to a sa*epoint, or 2L "# the syste- or session' 2 H,ery state-ent or a paralleliJe! DFL state-ent can not e+ec,te a DFL state-ent or -o!ify the !ata(ase' 2 DFL state-ent can not rea! or -o!ify the partic,lar ta(le (eing -o!ifie! (y that DFL state-ent'

&ote3 Calls to s,(progra-s that (rea0 the a(o*e restrictions are not allo1e!'

Copyright 2005, Infosys echnologies Lt!

<5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

In*o0ing a Dser=Define! Pac0age /,nction fro- a SQL State-ent

S"L"C# ta.es=package/ta.)sa ary*, sa ary, name :!%< emp -

Copyright 2005, Infosys echnologies Lt!

<5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

(sing Supp ied Packages


Oracle=s,pplie! pac0ages3 .2re pro*i!e! 1ith the Oracle ser*er ."+ten! the f,nctionality of the !ata(ase ."na(le access to certain SQL feat,res nor-ally restricte! for PL/SQL

Copyright 2005, Infosys echnologies Lt!

<<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Dsing &ati*e Dyna-ic SQL


Dyna-ic SQL3 . Is a SQL state-ent that contains *aria(les that can change !,ring r,nti-e . Is a SQL state-ent 1ith placehol!ers an! is store! as a character string . "na(les general=p,rpose co!e to (e 1ritten . "na(les !ata=!efinition, !ata=control, or session=control state-ents to (e 1ritten an! e+ec,te! fro- PL/SQL . Is 1ritten ,sing either DBMS_SQL or nati*e !yna-ic SQL

Copyright 2005, Infosys echnologies Lt!

<>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

"+ec,tion /lo1
SQL state-ents go thro,gh *ario,s stages3 . Parse . $in! . "+ec,te . /etch &ote3 So-e stages -ay (e s0ippe!'

Copyright 2005, Infosys echnologies Lt!

<?

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Dsing the D$FSESQL Pac0age


he DBMS_SQL pac0age is ,se! to 1rite !yna-ic SQL in store! proce!,res an! to parse DDL state-ents' So-e of the proce!,res an! f,nctions of the pac0age incl,!e3
OPEN_CURSOR PARSE BIND_VARIABLE EXECUTE FETCH_ROWS CLOSE_CURSOR

Copyright 2005, Infosys echnologies Lt!

<B

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Dsing the D$FSEDDL Pac0age

he D$FSEDDL Pac0age3 . Pro*i!es access to so-e SQL DDL state-ents fro- store! proce!,res . Incl,!es so-e proce!,res3 4 2L "#ECOFPIL" 6o()ectEtype, o1ner, o()ectEna-e7 4 2&2LKL"EO$M"C 6o()ectEtype, o1ner, na-e, -etho!7 'B<S=''L/AL#"!=C%<P+L")JP!%C"'(!"J,JA=(S"!J,JQ("!A2J* &ote3 his pac0age r,ns 1ith the pri*ileges of calling ,ser, rather than the pac0age o1ner SKS' 'B<S=''L/A1ALAR"=%BS"C#)J#ABL"J,JA=(S"!J,JS%BSJ,JC%<P(#"J*

Copyright 2005, Infosys echnologies Lt!

>0

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

(sing the 'B<S=%(#P(# Package


he DBMS_OUTPUT pac0age ena(les yo, to o,tp,t -essages fro- PL/SQL (loc0s' 2*aila(le proce!,res incl,!e3 PUT NEW_LINE PUT_LINE GET_LINE GET_LINES ENABLE/DISABLE

Copyright 2005, Infosys echnologies Lt!

>C

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

'atabase #riggers
2 trigger3 .Is a PL/SQL (loc0 or a PL/SQL proce!,re associate! 1ith a ta(le, *ie1, sche-a, or the !ata(ase ."+ec,tes i-plicitly 1hene*er a partic,lar e*ent ta0es place .Can (e either3
42pplication trigger3 /ires 1hene*er an e*ent occ,rs 1ith a partic,lar application 4Data(ase trigger3 /ires 1hene*er a !ata e*ent 6s,ch as DFL7 or syste- e*ent 6s,ch as logon or sh,t!o1n7 occ,rs on a sche-a or !ata(ase

Copyright 2005, Infosys echnologies Lt!

>2

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

0uide ines for 'esigning #riggers


.Design triggers to3 4Perfor- relate! actions 4CentraliJe glo(al operations .Do not !esign triggers3 4@here f,nctionality is alrea!y (,ilt into the Oracle ser*er 4 hat !,plicate other triggers .Create store! proce!,res an! in*o0e the- in a trigger, if the PL/SQL co!e is *ery lengthy' . he e+cessi*e ,se of triggers can res,lt in co-ple+ inter!epen!encies, 1hich -ay (e !iffic,lt to -aintain in large applications'

Copyright 2005, Infosys echnologies Lt!

>3

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Data(ase rigger3 "+a-ple


App ication +1S"!# +1#% "<PL / / /-

%"'_C.EC# trigger
"<PL tab e

Copyright 2005, Infosys echnologies Lt!

>5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating '<L #riggers


2 triggering state-ent contains3 . rigger ti-ing 4/or ta(le3 $"/O#", 2/ "# 4/or *ie13 I&S "2D O/ . riggering e*ent3 I&S"# , DPD2 ", or D"L" " . a(le na-e3 On ta(le, *ie1 . rigger type3 #o1 or state-ent .@I"& cla,se3 #estricting con!ition . rigger (o!y3 PL/SQL (loc0

Copyright 2005, Infosys echnologies Lt!

>5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

DFL rigger Co-ponents

rigger ti-ing3 @hen sho,l! the trigger fireA .$"/O#"3 "+ec,te the trigger (o!y (efore the triggering DFL e*ent on a ta(le' .2/ "#3 "+ec,te the trigger (o!y after the triggering DFL e*ent on a ta(le' .I&S "2D O/3 "+ec,te the trigger (o!y instea! of the triggering state-ent' his is ,se! for *ie1s that are not other1ise -o!ifia(le'

Copyright 2005, Infosys echnologies Lt!

><

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

DFL rigger Co-ponents


riggering ,ser e*ent3 @hich DFL state-ent ca,ses the trigger to e+ec,teA Ko, can ,se any of the follo1ing3 . I&S"# . DPD2 " . D"L" "

Copyright 2005, Infosys echnologies Lt!

>>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

DFL rigger Co-ponents


rigger type3 Sho,l! the trigger (o!y e+ec,te for each ro1 the state-ent affects or only onceA . State-ent3 he trigger (o!y e+ec,tes once for the triggering e*ent' his is the !efa,lt' 2 state-ent trigger fires once, e*en if no ro1s are affecte! at all' . #o13 he trigger (o!y e+ec,tes once for each ro1 affecte! (y the triggering e*ent' 2 ro1 trigger is not e+ec,te! if the triggering e*ent affects no ro1s'

Copyright 2005, Infosys echnologies Lt!

>?

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

DFL rigger Co-ponents


rigger (o!y3 @hat action sho,l! the trigger perfor-A he trigger (o!y is a PL/SQL (loc0 or a call to a proce!,re'

Copyright 2005, Infosys echnologies Lt!

>B

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

/iring SeH,ence
(se the fo owing firing seFuence for a trigger on a tab e, when a sing e row is manipu atedH '<L statement +1S"!# +1#% dep )dep=id, dep=name, oc=id* ;AL("S )ECC, I!"S"A!C5J, GLCC*-

B":%!" statement trigger #riggering action B":%!" row trigger A:#"! row trigger A:#"! statement trigger
Copyright 2005, Infosys echnologies Lt! ?0 "#/CO#P/C#S/D$25/003 %ersion &o' 2'0

/iring SeH,ence
(se the fo owing firing seFuence for a trigger on a tab e, when many rows are manipu atedH (P'A#" emp S"# sa ary B sa ary 3 2/4G 65"!" dep=id B DC-

B":%!" statement trigger B":%!" row trigger A:#"! row trigger /// B":%!" row trigger A:#"! row trigger /// A:#"! statement trigger
Copyright 2005, Infosys echnologies Lt! ?C "#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Synta. for Creating '<L Statement #riggers


Synta.H C!"A#" $%! !"PLAC"& #!+00"! trigger_name timing event1 %! event2 %! %1 tablename Trigger body 1oteH #rigger names must be uniFue with respect to other triggers in the same schema/

Copyright 2005, Infosys echnologies Lt!

?2

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating '<L Statement #riggers


".amp eH C!"A#" %! !"PLAC" #!+00"! trig2 B":%!" +1S"!# %1 emp B"0+1 +: )#%=C5A!)SAS'A#",J'AJ* +1 )JSA#J,JS(1J** %! )#%=C5A!)SAS'A#",J554LJ* 1%# B"#6""1 JCEJ A1' J2>J* #5"1 !A+S"=APPL+CA#+%1="!!%! )M4C222,JAou are not a owed to operate beyond officehours/J*"1' +:"1'/

Copyright 2005, Infosys echnologies Lt!

?3

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating a '<L !ow #rigger


Synta.H C!"A#" $%! !"PLAC"& #!+00"! trigger_name timing event1 %! event2 %! %1 tablename $!":"!"1C+10 %L' AS old | 1"6 AS ne & :%! "AC5 !%6 65"1 )condition* trigger_body

Copyright 2005, Infosys echnologies Lt!

?5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating '<L !ow #riggers

C!"A#" %! !"PLAC" #!+00"! prevent=sa B":%!" +1S"!# %! (P'A#" %: sa ary %1 emp :%! "AC5 !%6 B"0+1 +: 1%# )H1"6/Nob=id +1 )I"'(CA#%!J, I<A1A0"!J** A1' H1"6/sa ary K GGCCC #5"1 !A+S"=APPL+CA#+%1="!!%! )M4C444,I Cannot earn this amountJ*"1' +:"1'/

Copyright 2005, Infosys echnologies Lt!

?5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

(sing %L' and 1"6 Qua ifiers


C!"A#" %! !"PLAC" #!+00"! audit=emp A:#"! '"L"#" %! +1S"!# %! (P'A#" %1 emp :%! "AC5 !%6 B"0+1 +1S"!# +1#% audit=emp=tab )user=name, timestamp, id, o d= ast=name, new= ast=name, o d=tit e, new=tit e, o d=sa ary, new=sa ary* ;AL("S )(S"!, SAS'A#", H%L'/emp oyee=id, H%L'/ ast=name, H1"6/ ast=name, H%L'/Nob=id, H1"6/Nob=id, H%L'/sa ary, H1"6/sa ary *"1'/

Copyright 2005, Infosys echnologies Lt!

?<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

(sing %L' and 1"6 Qua ifiersH ".amp e (sing Audit="mp=#ab e


+1S"!# +1#% emp )emp=id, name, Nob=id, sa ary, ///* ;AL("S )EEE, J#empJ, IC%1S(L#A1#J, 4CCC, ///*(P'A#" emp S"# sa ary B 4CCC, name B JSukumarJ 65"!" emp=id B EEE-

S"L"C# user=name, /// :!%< audit=emp=tab

Copyright 2005, Infosys echnologies Lt!

?>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

!estricting a !ow #rigger


C!"A#" %! !"PLAC" #!+00"! get=commission B":%!" +1S"!# %! (P'A#" %: sa ary %1 emp :%! "AC5 !%6 65"1 )1"6/Nob=id B I"'(CA#%!J* B"0+1 +: +1S"!#+10 #5"1 H1"6/commission HB C"LS+: H%L'/commission +S 1(LL #5"1 H1"6/commission HB C"LS" H1"6/commission HB H%L'/commission P C/24"1' +:"1'/

Copyright 2005, Infosys echnologies Lt!

??

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

I&S "2D O/ riggers


App ication +1S"!# +1#% my=view / / /+1S"!# #ABL"2 +1S#"A' %: #rigger

<A=;+"6

(P'A#" #ABL"4

Copyright 2005, Infosys echnologies Lt!

?B

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating an +1S#"A' %: #rigger


Synta.H C!"A#" $%! !"PLAC"& #!+00"! trigger_name +1S#"A' %: event1 %! event2 %! %1 vie _name $!":"!"1C+10 %L' AS old | 1"6 AS ne & $:%! "AC5 !%6& trigger_body

Copyright 2005, Infosys echnologies Lt!

B0

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Creating an I&S "2D O/ rigger


+1S"!# into "<P=;+"6 that is based on "<PL and '"P tab es +1S"!# +1#% emp=view)emp=id, /// * ;AL("S)222,I!A<J,7CCC,2C,IramOinfy/comJ,I<A1A0"!J*-

Copyright 2005, Infosys echnologies Lt!

BC

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

'ifferentiating Between 'atabase #riggers and Stored Procedures


#riggers 'efined with C!"A#" #!+00"! 'ata dictionary contains source code in (S"!=#!+00"!S +mp icit y invoked C%<<+#, SA;"P%+1#, and !%LLBACQ are not a owed Procedures 'efined with C!"A#" P!%C"'(!" 'ata dictionary contains source code in (S"!=S%(!C"

".p icit y invoked C%<<+#, SA;"P%+1#, and !%LLBACQ are a owed

Copyright 2005, Infosys echnologies Lt!

B2

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

'!%P #!+00"! Synta.


o re-o*e a trigger fro- the !ata(ase, ,se the DROP TRIGGER synta+3 '!%P #!+00"! triggername-

".amp eH '!%P #!+00"! trig2A triggers on a tab e are dropped when the tab e is dropped/

Copyright 2005, Infosys echnologies Lt!

B3

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

$enefits of Data(ase riggers

I-pro*e! !ata sec,rity3 4 Pro*i!e enhance! an! co-ple+ sec,rity chec0s 4 Pro*i!e enhance! an! co-ple+ a,!iting I-pro*e! !ata integrity3 4 "nforce !yna-ic !ata integrity constraints 4 "nforce co-ple+ referential integrity constraints 4 "ns,re that relate! operations are perfor-e! together i-plicitly

Copyright 2005, Infosys echnologies Lt!

B5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

%ie1ing rigger Infor-ation

Ko, can *ie1 the follo1ing trigger infor-ation3 . DS"#EO$M"C S !ata !ictionary *ie13 o()ect infor-ation . DS"#E #I88"#S !ata !ictionary *ie13 the te+t of the trigger . DS"#E"##O#S !ata !ictionary *ie13 PL/SQL synta+ errors 6co-pilation errors7 of the trigger

Copyright 2005, Infosys echnologies Lt!

B5

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Listing the Code of #riggers


S"L"C# trigger=name, trigger=type, triggering=event, tab e=name, referencing=names, status, trigger=body :!%< user=triggers 65"!" trigger=name B I#!+02J-

Copyright 2005, Infosys echnologies Lt!

B<

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

S,--ary
@e ha*e e+plaine! the follo1ing PL/SQL s,(progra-s ,nits . . . . Proce!,res /,nctions Pac0ages Data(ase riggers

Copyright 2005, Infosys echnologies Lt!

B>

"#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Thank You!
Copyright 2005, Infosys echnologies Lt! B? "#/CO#P/C#S/D$25/003 %ersion &o' 2'0

Das könnte Ihnen auch gefallen