You are on page 1of 97

Oracle database tips Summary: DB_BLOCK_BUFFERS size tuning. Find Mem r!"#$!sical read %it rati .

Select dec de&name' (p$!sical reads(' )alue*" &dec de&name' (c nsistent gets(')alue* + dec de&name'(db bl c, gets(')alue** as $it_rati Fr m )-s!sstat .$ere name /0 &(p$!sical reads(' (c nsistent gets(' (db bl c, gets(*1

i2 rati 3 45 ! u ma! decrease t$e bu22er size in case re6uired 2 r t$er purp se i2 $it_rati bet.een 78 and 49 t$en its OK i2 $it_rati :8 t :4 t$en ne $as t add m re bu22ers ;;;;;;;;;;;;;;;;;; supp se it is less t$an 78' t$en 6uesti n arises as t $ . man! bu22ers t add. /n rder t determ nl! n S=S l gin and DB_BLOCK_LRU_E<>E0DED_S>?>/S>/CS set t n n@zer )alue i Description: Select B88+>RU0C&indC"B88* D B EE (@( EE B88+&>RU0C&indC"B88*DB range' SUM&c unt* additi nal_$its FROM C-KCBRB% FROU# B= >RU0C&indC"B88*1 supp se t$e result range additi nal_$its ;;;;;;;;;;;;;;;;;;;;;;;;; B@B88 7G88H B8B@H88 B85888 H8B@I88 B885 in t$is case i2 ! u n tice increasing t$e bu22er size b! H88 is su22icient' 2urt$er increases .ill n decrease' t$en per2 rm ab )e perati n .it$ <-KCBCD% )ie.. F r t$is' t$e init. ra prameter t true Summary:

% . t 2ind )

Description:

E)er spend s )alues ! u ca can easil! 2in )-nls_)alid_ KL?0FU?F KC%?R?C> release 2 Or KSOR>K rde

Summary: Description:

0LS_L?0F parameter c

L$ile running an eCp rt c$aracter set 2 LEG/SO

generated a .arning mess being used and p ssible c tried setting t$e 0LS_L? Englis$_Canada.LEG/SO pr perl!. >$is is s met$i eCp rts n an! database u de2ault c$aracter set. Ot$ c$aracters li,e accents ca ! ur dataM >$e en)ir nme s met$ing similar t t$isA 0LS_L?0F;Englis$_Ca 0LS_L?0F Summary: Description: KNuer! is eCecuting...KJ KNuer! is eCecuting...K isn(t itJ L$ ,n .sJ >$is d es' i2 ! ur 6uer! uses r llbac, segments &updates' inserts' etc.* Using t$e statement bel .' ! u can 2ind ut i2 ! ur U#D?>E & r .$ate)er* is pr ceeding r r lling bac,. Step BA Obtain t$e S/D 2 r t$e sessi n in 6uesti n. >$ere are a 2e. .a!s t btain t$e &pr per* S/D' but t$is ma! . r, as .ell as an!A select sid' serialO' username' terminal 2r m )-sessi n1 L , t$r ug$ t$e results t 2ind t$e S/D ! u need. /n t$is eCample' t$e S/D is B8. Step HA select taddr from v$session where sid = '10' ; /n t$is eCample' t$e result is 8HIED7BC Step IA Using t$e )alue 2 taddr returned in step H' d t$isA select used_ublk from v$transaction

where addr='023ED 1!' or" #ou can combine ste$s 2 and 3 as follows% select used_ublk from v$transaction where addr= &select '(DD) from v$session where sid='10'*; Step 9A Lait a 2e. sec nds' and run S>E# I again. i2 t$e results s$ . an increasing number' t$e transacti n is pr ceeding. /2 t$e results s$ . a smaller number' t$en a r llbac, is ccurring. Migrating using ODM? /(m migrating m re t$an H9H racle 7.I.9 databases t Oracle G.B.: Using ODM? &Oracle Data Migrati n ?sistant* c uld 2ind a big err r in t$e Oracle 0 te 7:9:8.B 2r m Metalin,

Summary: Description:

&C$ec,list 2 r Migrating 2r m Oracle7 t OracleG.B n U0/<*. Step BI' .$en ! u $a)e t CO0PER> t$e database' d es n t . r, as indicated' s ' .$at ! u $a)e t d isA B* startup ! ur db H* bac,up ! ur c ntr l 2ile t trace I* s$utd .n ! ur db 9* startup n m unt 5* recreate ! ur c ntr l 2iles using script generated in step H*. :* alter database pen 7* c ntinue .it$ Oracle C$ec,list. Belie)e me' it . r,s 2ine MMM % pe t$is c uld be use2ul.

Email me i2 an! pr blem c mes up. Summary: Migrating using ODM? /(m migrating m re t$an H9H racle 7.I.9 databases t Oracle G.B.: Using ODM? &Oracle Data Migrati n ?sistant* c uld 2ind a big err r in t$e Oracle 0 te 7:9:8.B 2r m Metalin, &C$ec,list 2 r Migrating 2r m Oracle7 t OracleG.B n U0/<*. Step BI' .$en ! u $a)e t CO0PER> t$e database' d es n t . r, as indicated' s ' .$at ! u $a)e t d isA Description: B* startup ! ur db H* bac,up ! ur c ntr l 2ile t trace I* s$utd .n ! ur db 9* startup n m unt 5* recreate ! ur c ntr l 2iles using script generated in step H*. :* alter database pen 7* c ntinue .it$ Oracle C$ec,list. Belie)e me' it . r,s 2ine MMM % pe t$is c uld be use2ul. Email me i2 an! pr blem c mes up. Summary: Running SNL#LUS in 0> scripts Be a.are' ! u can(t setup an en)ir nment )ariable SNL#LUS in an 0> script. /t .ill 2ail. Oracle G.B.: $as c$anged 2r m plusG8.eCe t s6lplus.eCe. /2 ! u $a)e an en)ir nment )ariable set' i.e. Description: SNL#LUS;dAQ racleQ raGBQbinQs6lplus.eCe' t$e batc$ R b .ill 2ail. C$ange t$e en)ir nment )ariable t an!t$ing else t$er t$an SNL#LUS. i.e. set S#LUS;dAQ racleQ raGBQbinQs6lplus.eCe Summary: % . t cleanup unused temp rar! segments Description: > cleanup r rem )e unused temp rar! segments ! u can use t$e 2 ll .ing SNL statement n t$e temp rar! tablespaceA alter tables$ace default stora+e &$ctincrease * use t$e pctincrease )alue ! u()e alread! speci2ied 2 r t$is tablespace. >$is SNL K.a,es

upK t$e s!stem m nit r pr cess' .$ic$ rem )es all unused temp. segments % . t l ad data 2r m MS eCcel t an Oracle tableJ /2 ! ur ECcel s$eet $as a simple table 2 rmat t$en ! u can c p! it as teCt 2ile .it$ tab delimiters. >$en create a simple ctl@2ile 2r m SNL+L ader Rust li,e t$atA load data infile ,, a$$end into table fields terminated b# ' ' & * >$e ,e! is t$at in ap str p$es ! u s$ uld speci2! t$e tab c$aracter &?SC// c de 4*. Use ne 2 t$e teCt edit rs t$at supp rt 6u ting Description: &usuall! .it$ CtrlD6 c mbinati n ,e!str ,e*. ?2ter t$at ! u can issueA s-lldr.0 userid= control= lo+= data= >$at(s all. S me clari2icati n. Better use clause K...2ields terminated b! <(84(...K in rder t speci2! t$e tab c$aracter eCplicitl!. = u can als tr! sa)ing t$e 2ile 2r m ECcel in teCt 2ile CSP@2 rmat r >?B@delimited and l ad .it$ t$e SNL+L ader &Oracle Utilities* r B rland Data#ump &2r m Delp$i r CDD distribut*. % . d / restrict a 6uer! b! KROL0UMK rangeJ

Summary:

Summary:

Description: /2 ! u tr! t use r .num t restrict a 6uer! b! a range t$at d es n t start .it$ B' ! u .ill 6uic,l! 2ind t$at it d es n t . r,. F r eCampleA

/012 /E1E!' 3 from '(41E1 56E)E rownum 4E'5EE7 8 (7D 10 no rows selected >$e reas n 2 r t$is' is t$at r .num is a pseud @c lumn pr duced ?F>ER t$e 6uer! returns. 0 rmall!' it can nl! be used t restrict a 6uer! t return a r .number range t$at starts .it$ B &li,e r .num* S5*. % .e)er' t$ere is a .a! t ac$ie)e t$is using Kin@line )ie.sK. F r t$is c mplete tip' )isitA $ttpA""....arr .sent.c m" ratip"tip9B.$tm F r m re 2 Ken(s Oracle tips' )isit $is main site atA $ttpA""....arr .sent.c m" ratip"2rames.$tm

Tip #15: SQL scripts that compare schemas in two different instances for differences. (Type: SQL)
S ! u $a)e ! ur applicati n installed in t$ree di22erent instances &De)el pment' >est' #r ducti n*. Or ma!be t$at is siC &c n)ersi n' dem ' training*' r eig$tJ Lell' $ . d ! u ,eep all 2 t$at in s!ncJ 0 matter $ . g d ! ur migrati n pr cedures' di22erences bet.een t$e instances s me$ . seem t al.a!s creep in. &% . is it t$at s met$ing gets int pr ducti n' t$at $as ne)er been in de)el pmentJ* >$is tip is a c uple 2 SNL scripts t$at .ill use database lin,s and t$e SNL M/0US perat r t c mpare t$e bRects and table de2initi ns in t$e same sc$ema in t. di22erent instances. First' an eCample 2 using t$e t. scripts .ill be s$ .n' t$en t$e actual scripts t$emsel)es. >$e! can als be d .nl aded bel .. >$e 2irst script lists t$e bRects t$at are n t in b t$ 2 t$e selected instancesA
/012 select db_link from user_db_links; D4_197: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 'E/'197:<5=)1D /012 >ob?diff =b?ect =wner% /6()ED @irst instance D4 1ink &9nclude >*%

/econd instance D4 1ink &9nclude >*%>'E/'197: =4AD9@@ Date% 02C10CD Ba+e% 1 =57E)% /6()ED =4AE!' D9@@E)E7!E )EB=)' )e$ort

=b?ects in devl but not demo =b?ect '#$e /tatus ;;;;;;;;;;;;;;; @E7!'9=7 @E7!'9=7 97DEH 97DEH B(!:(JE B(!:(JE 4=DI B)=!EDE)E /E0EE7!E '(41E '(41E F9E5 F9E5 F(19D F(19D F(19D F(19D F(19D F(19D 97F(19D F(19D F(19D F(19D F(19D F(19D

=b?ect 7ame ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;; '/E_/E1E!'_!(D_@7 '/E_/E1E!'_@7 @GE_B1/ @GE_B)E!9B /I/B9BE /I/B9BE (41E_@:_!=7/')(97'/ 5')/6D_/E0 (DG97_!=DE @GB'_@=)G_6E1B @GE_!=E7'I_F @GE_')E/'_F 12 rows selected< =4AD9@@ Date% 02C10CD Ba+e% 1 =57E)% /6()ED

=4AE!' D9@@E)E7!E )EB=)'

)e$ort

=b?ects in demo but not devl =b?ect '#$e /tatus ;;;;;;;;;;;;;;; @E7!'9=7 97DEH /E0EE7!E ')9JJE) ')9JJE) ')9JJE) F9E5 F(19D F(19D F(19D 97F(19D 97F(19D F(19D F(19D

=b?ect 7ame ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;; BB)_1(4E1_7G @G(_BE/'_BE/'_!D_9 JE=_(DG97_E79'_/E0 ')_@G(_/'('E/ ')_97/E)'_@G(_()E( @G(_97/_@G(_()E(_') )H_/EG_)B'_F9E5 rows selected<

0 tesA /2 n database lin, is entered' t$en t$e script uses t$e CURRE0> instance. ?ls ' t$e title uses t$e instance name in eac$ instances )-parameter table &ObRects in Tinstance_B_nameU but n t in Tinstance_H_nameU. >$e sec nd script c mpares t$e actual table de2initi ns in t. instancesA
/012 >tabdiff 'able =wner% /6()ED @irst instance D4 1ink &9nclude >*% /econd instance D4 1ink &9nclude >*%>'E/'197:

'(4D9@@ Date% 02C10CD Ba+e% 1 =57E)% /6()ED

/!6EG( D9@@E)E7!E )EB=)'

)e$ort

devl and demo 9nstance 'able Data'#$e 1en Br 7ullK ;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;; demo @GE D('E =0 I demo @GE D('E =0 I 2 rows selected<

Differences between !olumn ;;;; ;;;; ;;;;; @GE_)E/')_4EJ_D'_4(D @GE_)E/')_E7D_D'_4(D

?nd n . listings 2 t$e actual scriptsA

C 33333333333333333333333333333333333333333333333333333333333 33333333333333C C3 ob?diff<s-l ; 1ists the ob?ects in a schema that are not in both of 3C C3 two instances< Eses database links and the /01 G97E/ 3C C3 o$erator to make the com$arison< 3C C3 3C C3 (uthor% :en (tkins &:en>arrowsent<com* 3C C3 htt$%CCwww<arrowsent<comCorati$ 3C C3 3C C3 5ritten% 8C11CD8 3C C3 3C C3 Iou need to have a database link setu$ for an# instance that #ou want 3C C3 to make a com$arison for< 3C C3 3C C3 Blease feel free to use and modif# this scri$t as lon+ it is not sold 3C C3 or included in an# software without the $rior $ermission of the author3C C3 9f #ou do make some +ood im$rovements" $lease send them to me" and 9 3C C3 can incor$orate them in a future version and make them available to 3C

C3 others &+ivin+ #ou credit of courseL*< 3C C3 3C C 33333333333333333333333333333333333333333333333333333333333 33333333333333C set $a+esiMe N0 set linesiMe .0 set verif# off set feedback off set $ause off; ;;define ob?_owner = 'O1' ;;define inst_1 = 'O2' ;;define inst_2 = 'O3' acce$t ob?_owner $rom$t '=b?ect =wner% ' acce$t inst_1 $rom$t '@irst instance D4 1ink &9nclude >*%' acce$t inst_2 $rom$t '/econd instance D4 1ink &9nclude >*%' clear breaks ttitle off set headin+ off column datetime no$rint new_value datetime column inst_code1 no$rint new_value inst_code1 column inst_code2 no$rint new_value inst_code2 select to_char&s#sdate"'GGCDDCII'* datetime from dual C select value inst_code1 from v$$arameterOinst_1 where name = 'db_name' C select value inst_code2 from v$$arameterOinst_2 where name = 'db_name' C set feedback on set headin+ on set new$a+e 0 ttitle left '=4AD9@@'; col 28 '=4AE!' D9@@E)E7!E )EB=)'' ; col 83 ')e$ort Date% ' datetime ; ski$ 1 col N0 'Ba+e% ' s-l<$no ; ski$ 1 col 10 '=57E)% ' ob?_owner ; ski$ 1 center '=b?ects in Oinst_code1 but not Oinst_code2' ; ski$ 2 set null=0 column ob?ect_t#$e format a18 headin+ '=b?ect '#$e'; column ob?ect_name format a38 headin+ '=b?ect 7ame'; column status format a10 headin+ '/tatus';

column inst_code format a10 headin+ '9nstance'; select ob?ect_name" ob?ect_t#$e" status from all_ob?ectsOinst_1 where owner = EBBE)&'Oob?_owner'* ;; and ob?ect_t#$e L= '/I7=7IG' G97E/ select ob?ect_name" ob?ect_t#$e" status from all_ob?ectsOinst_2 where owner = EBBE)&'Oob?_owner'* ;; and ob?ect_t#$e L= '/I7=7IG' order b# 2"3 C set headin+ off; set feedback off; select '

' from dual C set headin+ on; set feedback on; ttitle left '=4AD9@@'; col 28 '=4AE!' D9@@E)E7!E )EB=)'' ; col 83 ')e$ort Date% ' datetime ; ski$ 1 col N0 'Ba+e% ' s-l<$no ; ski$ 1 col 10 '=57E)% ' ob?_owner ; ski$ 1 center '=b?ects in Oinst_code2 but not Oinst_code1' ; ski$ 2 select ob?ect_name" ob?ect_t#$e" status from all_ob?ectsOinst_2 where owner = EBBE)&'Oob?_owner'* and ob?ect_t#$e L= '/I7=7IG' G97E/ select ob?ect_name" ob?ect_t#$e" status from all_ob?ectsOinst_1 where owner = EBBE)&'Oob?_owner'* and ob?ect_t#$e L= '/I7=7IG' order b# 2"3 C undefine datetime undefine inst_code1 undefine inst_code2 undefine ob?_owner

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;
C 33333333333333333333333333333333333333333333333333333333333 33333333333333C C3 tabdiff<s-l ; 1ists the differences in table definitions in the tables3C C3 for a schema in two different instances< Eses database3C C3 links and the /01 G97E/ o$erator to make the com$arison<3C C3 3C C3 (uthor% :en (tkins &:en>arrowsent<com* 3C C3 htt$%CCwww<arrowsent<comCorati$ 3C C3 3C C3 5ritten% 8C11CD8 3C C3 3C C3 Iou need to have a database link setu$ for an# instance that #ou want 3C C3 to make a com$arison for< 3C

C3 3C C3 Blease feel free to use and modif# this scri$t as lon+ it is not sold 3C C3 or included in an# software without the $rior $ermission of the author3C C3 9f #ou do make some +ood im$rovements" $lease send them to me" and 9 3C C3 can incor$orate them in a future version and make them available to 3C C3 others &+ivin+ #ou credit of courseL*< 3C C3 3C C 33333333333333333333333333333333333333333333333333333333333 33333333333333C set $a+esiMe N0 set linesiMe 108 set verif# off set feedback off set $ause off; ;;define ob?_owner = 'O1' ;;define inst_1 = 'O2' ;;define inst_2 = 'O3' acce$t ob?_owner $rom$t ''able =wner% ' acce$t inst_1 $rom$t '@irst instance D4 1ink &9nclude >*%' acce$t inst_2 $rom$t '/econd instance D4 1ink &9nclude >*%' clear breaks ttitle off set headin+ off column datetime no$rint new_value datetime column inst_code1 no$rint new_value inst_code1 column inst_code2 no$rint new_value inst_code2 select to_char&s#sdate"'GGCDDCII'* datetime from dual C select value inst_code1 from v$$arameterOinst_1 where name = 'db_name' C select value inst_code2 from v$$arameterOinst_2 where name = 'db_name' C set feedback on set headin+ on set new$a+e 0 ttitle left ''(4D9@@'; col 28 '/!6EG( D9@@E)E7!E )EB=)'' ; col 83 ')e$ort Date% ' datetime ; ski$ 1 col N0 'Ba+e% ' s-l<$no ; ski$ 1 col 10 '=57E)% ' ob?_owner ;

ski$ 1 center 'Differences between Oinst_code1 and Oinst_code2' ; ski$ 2 column table_name format a28 headin+ ''able'; column column_name format a28 headin+ '!olumn'; column data_t#$e format a. headin+ 'Data'#$e'; column data_len+th format DDD headin+ '1en'; column data_$recision format DDD headin+ 'Br'; column nullable format a8 headin+ '7ullK'; column inst_code format a. headin+ '9nstance'; & select 'Oinst_code1' inst_code" table_name" column_name" data_t#$e" data_len+th" data_$recision" nullable from all_tab_columnsOinst_1 where owner = EBBE)&'Oob?_owner'* and table_name in &select table_name from all_tablesOinst_2 where owner = EBBE)&'Oob?_owner'** G97E/ select 'Oinst_code1' inst_code" table_name" column_name" data_t#$e" data_len+th" data_$recision" nullable from all_tab_columnsOinst_2 where owner = EBBE)&'Oob?_owner'* * E79=7 & select 'Oinst_code2' inst_code" table_name" column_name" data_t#$e" data_len+th" data_$recision" nullable from all_tab_columnsOinst_2 where owner = EBBE)&'Oob?_owner'* and table_name in &select table_name from all_tablesOinst_1 where owner = EBBE)&'Oob?_owner'** G97E/ select 'Oinst_code2' inst_code" table_name" column_name" data_t#$e" data_len+th" data_$recision" nullable from all_tab_columnsOinst_1 where owner = EBBE)&'Oob?_owner'* * order b# 2" 3 C undefine datetime undefine inst_code1 undefine inst_code2 undefine ob?_owner

Tip #12: SQL Script to show hit ratio of current!y runnin" processes. (Type: SQL)
%a)e ! u e)er . ndered .$! ! ur ser)er .as running s sl .J L$ else is running 6ueries and .$! are t$e! b gging t$e s!stem d .nJJ S ! u g r und up a DB? and as,

t$em t m nit r t$e database using ne 2 t$ se s$nazz! DB? t!pe t ls. But DB?s are n t al.a!s $ad 2 r t$e as,ing' and ! u d n t $a)e access t t$e t ls' s .$at d ! u d J >$is tip is a c uple 2 simple SNL scripts .$ic$ .ill s$ . .$ic$ Oracle pr cesses are currentl! running in an instance' and .$at t$e bu22er $it rati is 2 r t$ se pr cesses &l . $it rati s are an indicati n 2 p rl! tuned SNL' .$ic$ can sl . t$e L%OLE instance d .n*. >$e 2irst script s$ .s t$e acti)e pr cesses and t$eir current $it rati .
C 33333333333333333333333333333333333333333333333333333333333 33333333333333C C3 list$roc<s-l ; 1ists currentl# runnin+ $rocesses and their hit ratios 3C C3 3C C3 (uthor% :en (tkins &:en>arrowsent<com* 3C C3 htt$%CCwww<arrowsent<comCorati$ 3C C3 3C C3 Iou need select access to F$/E//9=7" F$B)=!E//" and F$/E//_9= 3C C3 to run this scri$t< 3C C3 3C C3 'he columns returned b# this scri$t are% 3C C =racle 9D &schemaname* = 'he oracle 'schema' or 'user' that is 3C C3 runnin+ the /01 statement< 3C C3 /#stem 9D &username* = 'he s#stem id that the $rocess is 3C C3 runnin+ under< 5ill be the uniP userid 3C C3 if =racle runnin+ on uniP< 3C C3 Bro+ram = 'he name of the $ro+ram that is runnin+ the /01<3C C3 Bh#sical )eads = 'he number of $h#sical block reads< 3C C3 6it )atio = 'he ratio of buffer to $h#sical block reads< 3C C3 be an indication of the efficienc# of the -uer#3C C3 runnin+< (n#thin+ under D0Q is bad< Fer# low 3C C3 hit ratios &R 10;20Q* in a $rocess can slow 3C C3 down the whole s#stem< 3C

C 33333333333333333333333333333333333333333333333333333333333 33333333333333C column schemaname format a10 headin+ '=racle 9D' column username format a10 headin+ '/#stem 9D' column $ro+ram format a32 headin+ 'Bro+ram' column hit_ratio format D<D0 headin+ '6it )atio' column $h#sical_reads format DDDDDDD headin+ ')eads' column sid format DDDDD /E1E!' s<schemaname " $<username " s<$ro+ram "io<$h#sical_reads "&io<block_+etsSio<consistent_+ets*C &io<block_+etsSio<consistent_+etsSio<$h#sical_reads* hit_ratio @)=G F$/ession s "F$Brocess $ "F$/ess_io io 56E)E s<$addr = $<addr (7D s<sid = io<sid ;; =nl# look at active $rocesses (7D s<status = '(!'9FE' ;; 7eed this $redicate to $revent division b# 0 (7D &io<block_+etsSio<consistent_+etsSio<$h#sical_reads* 2 0 C

?n eCample 2 using t$e scriptA


/012 >hitratio =racle 9D /#stem 9D )eads 6it )atio ;;;;;;;;;; ;;;;;;;;;; ;;;;;;;; ;;;;;;;;; /I/ oracle 1<00 /I/ oracle 10.DT <.3 /I/ oracle 1. <D8 4DE/TD0 oracle 1<00 4DE/TD0 oracle 3T . <.3 Bro+ram ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

!%U=)(597U497UB1E/31<EHE s-l$lus>larabee &'7/ interface*

>$e neCt script is a simpler )ersi n t$at Rust s$ .s all 2 pr cesses and t$eir status' sid and serialO. >$e sid and serialO are used in t$e ?L>ER S=S>EM K/LL SESS/O0 c mmand t ,ill racle pr cesses t$at are (stuc,(.
C 33333333333333333333333333333333333333333333333333333333333 33333333333333C C3 list$roc<s-l ; 1ists currentl# $rocesses" status" sid O serialV 3C

C3 3C C3 (uthor% :en (tkins &:en>arrowsent<com* 3C C3 htt$%CCwww<arrowsent<comCorati$ 3C C3 3C C3 Iou need select access to F$/E//9=7" F$B)=!E// to run this scri$t 3C C3 3C C 33333333333333333333333333333333333333333333333333333333333 33333333333333C column schemaname format a10 headin+ '=racle 9D' column username format a10 headin+ '/#stem 9D' column $ro+ram format a30 headin+ 'Bro+ram' column user_name format a18 headin+ 'Eser 7ame' column sid format DDDDD /E1E!' s<schemaname "$<username "s<$ro+ram "s<sid "s<serialV "s<status @)=G F$/ession s "F$Brocess $ where s<$addr = $<addr C

?n eCample 2 running t$e scriptA


/012 >list$roc =racle 9D /#stem 9D /E)9(1V /'('E/ ;;;;;;;;;; ;;;;;;;;;; ;;;;;;;;; ;;;;;;;; :(':TD0 TT :911ED /I/ oracle 1 (!'9FE /I/ oracle 1 (!'9FE /I/ oracle 1 (!'9FE /I/ oracle 1 (!'9FE /I/ oracle 1 (!'9FE =)(B9BE ora$i$e .021 97(!'9FE 4DE/TD0 oracle 108 97(!'9FE A=AATD0 oracle 32ND1 97(!'9FE Bro+ram /9D

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;; !%U597D=5/U/I/'EG32U=1E2<D11 21 1 2 3 T 8 K >+amera &'7/ interface* D 12

!%U=)(597U497U!:)=7101<D11 !%U=)(597U497U)28DE/<EHE

4DE/TD0 oracle 2 8 (!'9FE ()AATD0 oracle 202D 97(!'9FE 46()TD0 oracle 28T8 97(!'9FE 4DE/TD0 oracle N1D (!'9FE G(AATD0 oracle 38 97(!'9FE 46()TD0 oracle 3D 97(!'9FE ()AATD0 oracle D1 3 97(!'9FE G(AATD0 oracle 22 3 97(!'9FE /6()ED oracle N 97(!'9FE 4DE/TD0 oracle 3D 97(!'9FE

!%U=)(597U497UB1E/31<EHE !%U=)(597U497U!:)=7101<D11 !%U=)(597U497UB1E/31<EHE s-l$lus>larabee &'7/ interface* !%U=)(597U497U!:)=7101<D11 !%U=)(597U497U)28DE/<EHE !%U=)(597U497U)28DE/<EHE !%U=)(597U497U)28DE/<EHE !%U=)(597U497UB1E/31<EHE !%U=)(597U497UB1E/31<EHE

1N N 10 1 13 1T . 11 18 1.

Tip #11: #rocedure to disa$!e %& constraints T' a ta$!e. (Type: D())
S ! u $a)e t rel ad t$e data in a table t$at is maintained in an t$er s!stem. But t$ere are t$ese pes,! F reign Ke!s de2ined >O t$is table 2r m t$er tables in ! ur database. O$ Lell' Select t$e names 2 t$e FKs 2r m t$e c nstraints table' enter t$e c mmands t disable t$em' n . l ad t$e data. L$atJ = u missed neJ Disable it' rel ad. 0 . enable all 2 t$e c nstraints again. Kind 2 tedi us. >$is tip details a st red pr cedure t$at can aut maticall! disable r enable all 2 t$e FK c nstraints +>O+ a speci2ied table. >$e 2 ll .ing pr cedure uses t$e 2 ll .ing steps t enable r disable all 2 t$e FK c nstraints +>O+ a speci2ied tableA B. Finds t$e #K 2 t$e speci2ied table. H. Uses t$is #K t 2ind all 2 t$e FKs t$at are lin,ed t t$e #K. I. #uts t get$er an ?L>ER >?BLE D/S?BLE CO0S>R?/0> c mmand t disable eac$ FK. 9. Uses d!namic SNL t eCecute t$e c mmands.
B)=GB' B)=GB' !reatin+ Brocedure able_fk_constraints !)E('E =) )EB1(!E B)=!EDE)E able_fk_constraints& $'able 97 F()!6()2 " $(ble 97 F()!6()2 * 9/ vB:7ame F()!6()2&.0*; ;; 'his cursor returns the list of @: constraints linked to the s$ecified ;; B: constraint< !E)/=) cur@:&$cB:7ame 97 F()!6()2* 9/ /E1E!' constraint_name" table_name

@)=G user_constraints 56E)E r_constraint_name = $cB:7ame; ;; 'hese two variables are used for the d#namic /01 nDD1!ursor 97'EJE); nDD1)eturn 97'EJE); 4EJ97 C 33333333333333333333333333333333333333333333333333333333333 33333333333333333333C C3 (41E_@:_!=7/')(97'/ ; 'his $rocedure easil# enablesCdisables @: constraints 3C C3 $ointin+ '= the s$ecified table< 3C C3 3C C3 Barameters% $'able ; 'he name of the table to disCenable @: 3C C3 constraints to< 3C C3 $(ble ; =ne of% D9/(41E or E7(41E 3C C 33333333333333333333333333333333333333333333333333333333333 33333333333333333333C ;; Jet the name of the B: constraint for the s$ecified table< 4EJ97 /E1E!' constraint_name 97'= vB:7ame @)=G user_constraints 56E)E table_name = $'able (7D constraint_t#$e = 'B'; E7D; ;; 7ow +et the @: constraints linked to the B: constraint of the s$ecified table< @=) fk 97 cur@:&vB:7ame* 1==B ;; Ese d#namic /01 to ePecute the (1'E) '(41E command and disCenable the constraint nDD1!ursor %= dbms_s-l<o$en_cursor; dbms_s-l<$arse&nDD1!ursor"'(1'E) '(41E 'WW fk<table_nameWW' ' WW$(bleWW' !=7/')(97' 'WW fk<constraint_name" 1*; nDD1)eturn %= dbms_s-l<ePecute&nDD1!ursor*; dbms_s-l<close_cursor&nDD1!ursor*; E7D 1==B; E7D (41E_@:_!=7/')(97'/; C

?n eCample 2 using t$e scriptA


ePecute able_fk_constraints&'GI'(41E'"'D9/(41E'*; truncate table m#table; >load_m#table

ePecute able_fk_constraints&'GI'(41E'"'E7(41E'*;

'f course* the procedure has to $e insta!!ed in a schema that has the )LT+, T)(L+ system pri-e!e"e* and security to modify the specified ta$!e. )!so* the data that is !oaded into the ta$!e may cause an e.istin" %& contraint to no !on"er $e -a!id (!i/e if an e.pected code is no !on"er there). 0n this case* the +1)(L+ wi!! $om$* and the data wi!! ha-e to $e fi.ed $efore the constraint can $e re2ena$!ed. Tip #52: 3ettin" ,id of 40nput truncated to # characters4 (Type: SQL5#!us)
?re ! u getting t$e ann !ing message K/nput truncated t O c$aractersK .$ene)er ! u run a SNL script in SNL+#lusJ >$is can be )er! ann !ing' especiall! i2 ! u are running SNL scripts t$at pr duce rep rts r generate t$er SNL scripts. >$is tip .ill tell ! u $ . t get rid 2 t$is messageM

)n +.amp!e of the #ro$!em


C nsider t$e 2 ll .ing SNL+#lus rep rtA

set pagesize I8 set linesize 98 set 2eedbac, 22 ttitle CE0>ER (>est Empl !ee Rep rt( s,ip H brea, n dname s,ip B sp l tstrep.lst SELEC> d.dname' e.empn ' e.ename FROM Dept d' Emp e L%ERE d.deptn ; e.deptn ORDER B= d.dname' e.ename " sp l 22 /2 eCecuted ! u mig$t seeA
'est Em$lo#ee )e$ort

D7(GE EGB7= E7(GE ;;;;;;;;;;;;;; ;;;;;;;;;; ;;;;;;;;;; (!!=E7'97J .2 !1(): .3D :97J D3T G911E) )E/E()!6 . N D02 8NN .. 3ND TDD ND. D00 N8T .TT 821 (D(G/ @=)D A=7E/ /!='' /G9'6 (11E7 41(:E A(GE/ G()'97 'E)7E) 5()D Problem Message!

/(1E/

9n$ut truncated to D characters

?s ! u can see' ! u $a)e t$e un.anted message at t$e b tt m 2 t$e rep rt.

6hat 7auses The #ro$!em8


>$is pr blem is caused b! $a)ing an!t$ing O>%ER t$an a blan, line at t$e b tt m 2 ! ur SNL+#lus scriptM >$e last line 2 t$e script +must+ be a blan, line' t$at is a line .it$ a carriage return and 0O>%/0F ELSE. F r eCampleA set pagesize I8 set linesize 98 set 2eedbac, 22 ttitle CE0>ER (>est Empl !ee Rep rt( s,ip H brea, n dname s,ip B sp l tstrep.lst SELEC> d.dname' e.empn ' e.ename FROM Dept d' Emp e L%ERE d.deptn ; e.deptn ORDER B= d.dname' e.ename " sp l 22 (!an/ Line9

Tip #::: 'rderin" $y a ;ierarchy (Type: SQL)


%a)e ! u e)er tried t rder a $ierarc$ical 6uer!J >$e results are n t enc uraging. >$e rdering returned b! Oracle is based n t$e $ierarc$!' and t$ere is n eas! .a! t rder

L/>%/0 t$e $ierarc$! le)els. S $ . d .e get ar und t$is pr blemJ Lell' t$ere is n eas! .a! t d it. % .e)er' .it$ a little . r,' t$e s luti n presented in t$is tip .ill d it.

6hat ;appens if 0 'rder $y


/ .ill use t$e in2am us EM#"DE#> tables t illustrate t$is tec$ni6ue. Using t$ese tables' ! u mig$t use t$e 2 ll .ing SNL 2 r a standard $ierarc$ical 6uer!A
/012 1 /E1E!' level" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 2 @)=G Em$ 3 !=77E!' 4I B)9=) em$<em$no = em$<m+r T3 /'()' 59'6 em$<em$no = .3D /012 C

1EFE1 E7(GE EGB7= GJ) DEB'7= ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; 1 :97J .3D 10 2 A=7E/ 8NN .3D 20 3 /!='' .. 8NN 20 T (D(G/ . N .. 20 3 @=)D D02 8NN 20 T /G9'6 3ND D02 20 2 41(:E ND. .3D 30 3 (11E7 TDD ND. 30 3 5()D 821 ND. 30 3 G()'97 N8T ND. 30 3 'E)7E) .TT ND. 30 3 A(GE/ D00 ND. 30 2 !1(): .2 .3D 10 3 G911E) D3T .2 10

0 . let(s sa! ! u .ant t rder alp$abeticall! .it$in eac$ le)el &i.e. BL?KE' CL?RK' VO0ES 2 r le)el H' and ?LLE0' V?MES' M?R>/0' >UR0ER' L?RD 2 r le)el I under BL?KE*. %ere are s me standard attempts at t$isA
/012 l 1 /E1E!' level" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 2 @)=G Em$ 3 !=77E!' 4I B)9=) em$<em$no = em$<m+r T3 /'()' 59'6 em$<em$no = .3D 1EFE1 E7(GE EGB7= GJ) DEB'7= ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; T (D(G/ . N .. 20 T /G9'6 3ND D02 20 3 (11E7 TDD ND. 30 3 @=)D D02 8NN 20 3 A(GE/ D00 ND. 30 3 G()'97 N8T ND. 30 3 G911E) D3T .2 10 3 /!='' .. 8NN 20

3 'E)7E) 3 5()D 2 41(:E 2 !1(): 2 A=7E/ 1 :97J 1T rows selected<

.TT 821 ND. .2 8NN .3D

ND. ND. .3D .3D .3D

30 30 30 10 20 10

/012 l 1 /E1E!' level" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 2 @)=G Em$ 3 !=77E!' 4I B)9=) em$<em$no = em$<m+r T /'()' 59'6 em$<em$no = .3D 83 order b# em$<ename /012 C 1EFE1 E7(GE EGB7= GJ) DEB'7= ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; T (D(G/ . N .. 20 3 (11E7 TDD ND. 30 2 41(:E ND. .3D 30 2 !1(): .2 .3D 10 3 @=)D D02 8NN 20 3 A(GE/ D00 ND. 30 2 A=7E/ 8NN .3D 20 1 :97J .3D 10 3 G()'97 N8T ND. 30 3 G911E) D3T .2 10 3 /!='' .. 8NN 20 T /G9'6 3ND D02 20 3 'E)7E) .TT ND. 30 3 5()D 821 ND. 30 1T rows selected< /012 l 1 /E1E!' level" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 2 @)=G Em$ 3 !=77E!' 4I B)9=) em$<em$no = em$<m+r T /'()' 59'6 em$<em$no = .3D 83 order b# level"em$<ename /012 C 1EFE1 E7(GE EGB7= GJ) DEB'7= ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; 1 :97J .3D 10 2 41(:E ND. .3D 30 2 !1(): .2 .3D 10 2 A=7E/ 8NN .3D 20 3 (11E7 TDD ND. 30 3 @=)D D02 8NN 20 3 A(GE/ D00 ND. 30 3 G()'97 N8T ND. 30 3 G911E) D3T .2 10

3 3 3 T T

/!='' 'E)7E) 5()D (D(G/ /G9'6

.. .TT 821 . N 3ND

8NN ND. ND. .. D02

20 30 30 20 20

0 ne 2 t$ese gi)e us .$at .e .ant.

<se a ;ierarchy 'rder &ey


>$e nl! .a! / $a)e 2 und t trul! res l)e t$is pr blem is t add a $ierarc$! rdering ,e! c lumn t t$e table .it$ t$e $ierarc$!. >$is c lumn needs t be p pulated pr grammaticall! in suc$ a .a! t$at ! u get t$e desired rdering. >$is ,e! $as t be t$e c ncatenati n 2 s me s rt 2 rder ,e! 2 r E?C% 2 t$e parent le)els ab )e t$e $ierarc$! n de. >$is .ill all . t$e $ierarc$! t be rdered .it$in eac$ le)el .$ile all .ing t$e c$ildren t be placed directl! underneat$ t$eir parent. F r eCample' c nsider t$e EM#_%/ER_ORDER c lumn t$at / added t t$e standard emp table bel .A
EGB7= ;;;;;;;;; 3ND TDD 821 8NN N8T ND. .2 .. .3D .TT . N D00 D02 D3T E7(GE ;;;;;;;;;;;;;;;;;;;; /G9'6 (11E7 5()D A=7E/ G()'97 41(:E !1(): /!='' :97J 'E)7E) (D(G/ A(GE/ @=)D G911E) EGB_69E)_=)DE) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 000.000 00080012 000.00030002 000.0003001T 000.000 000.0003000D 000.0003 000.000T 000.000 0011 000. 000.00030013 000.000 00110001 000.0003000N 000.000 0008 000.000T0010

0 . i2 / rder b! EM#_%/ER_ORDER / getA


/012 l 1 /E1E!' level" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" 2 @)=G Em$ 3 !=77E!' 4I B)9=) em$<em$no = em$<m+r T /'()' 59'6 em$<em$no = .3D 8 order b# em$_hier_order /012 C 1EFE1 E7(GE EGB7= GJ) EGB_69E)_=)DE) ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;;;;;;;;; 1 :97J .3D 000. 2 41(:E ND. .3D 000.0003 3 (11E7 TDD ND. 000.00030002

3 A(GE/ 3 G()'97 3 'E)7E) 3 5()D 2 !1(): 3 G911E) 2 A=7E/ 3 @=)D T /G9'6 000.000 00080012 3 /!='' T (D(G/ 000.000 00110001

D00 N8T .TT 821 .2 D3T 8NN D02 3ND .. . N

ND. ND. ND. ND. .3D .2 .3D 8NN D02

000.0003000N 000.0003000D 000.00030013 000.0003001T 000.000T 000.000T0010 000.000 000.000 0008

8NN 000.000 0011 ..

L$ic$ is eCactl! .$at / .ant. >$e 2irst 2 ur c$aracters 2 EM#_%/ER_ORDER are used 2 r rdering t$e t p le)el 2 t$e $ierarc$! &K888GK*' t$e sec nd 2 ur are used 2 r rdering t$e sec nd le)el &K888IK'K8889K'K8887K*' and t$e t$ird 2 ur 2 r t$e t$ird le)el' etc. 0O>EA >$e ab )e 6uer! used t$e $ierarc$ical clauses &CO00EC> B=' etc.* Using t$e $ierarc$! rdering c lumn ! u c uld c nstruct a 6uer! t$at d es n t need it. F r instanceA
/E1E!' len+th&em$_hier_order*CT lvl" 1B(D&' '" &len+th&em$_hier_order*C2*;2*WWem$<ename ename" @)=G Em$ order b# em$_hier_order

#opu!atin" The ;ierarchy 'rderin" &ey


>$e main pr blem .it$ t$is tec$ni6ue is t$at it re6uires t$at eCtra c de be .ritten and eCecuted t p pulate t$e $ierarc$! rdering ,e!. / used t$e 2 ll .ing st red pr cedure t p pulate t$e EM#_%/ER_ORDER ,e! in t$e ab )e eCampleA
!)E('E =) )EB1(!E B)=!EDE)E E$date_Em$_6ier 9/ ;; !ursor to return the orderin+ ke# for em$ !E)/=) em$_order_cur 9/ /E1E!' em$no @)=G Em$ =)DE) 4I ename; ;; 6ierarch# -uer# !E)/=) hier_cur 9/ /E1E!' 1EFE1 lvl" em$no @)=G Em$ /'()' 59'6 em$<em$no = .3D !=77E!' 4I B)9=) em$<em$no = em$<m+r; 'IBE vc_tabt#$e 9/ '(41E =@ F()!6()2&T* 97DEH 4I 497()I_97'EJE); t_ordke# vc_tabt#$e; t_ke# vc_tabt#$e; v_hier_ke# F()!6()2&30*;

v_=rd!nt 7EG4E) %= 0; 4EJ97 ;; ;; 1oad the orderin+ ke# into a B1C/01 table to save table access @=) e 97 em$_order_cur 1==B v_=rd!nt %= v_=rd!nt S 1; t_ordke#&e<em$no* %= 1B(D&'=_!6()&v_=rd!nt*"T"'0'*; E7D 1==B; ;; 7ow o$en the hierarch# -uer# @=) h 97 hier_cur 1==B ;; /tore the order ke# for the current level in the hierarch# t_ke#&h<lvl* %= t_ordke#&h<em$no*; ;; 4uild the full orderin+ ke# for the current record< will 'his

;; consist of the current record's orderin+ ke# $receded in order ;; b# the orderin+ ke#s of ever# level above it in the hierarch#< v_hier_ke# %= ''; @=) i 97 1<<h<lvl 1==B v_hier_ke# %= v_hier_ke#WWt_ke#&i*; E7D 1==B; EBD('E Em$ /E' em$_hier_order = v_hier_ke# 56E)E em$no = h<em$no; E7D 1==B; E7D; C

>$is st red pr cedure can be called 2r m t$e client t$at maintains t$e $ierarc$!' eCecuted eit$er manuall! &i.e. .$en t$e user sa!s t$e! are d ne editing t$e $ierarc$!*' r aut maticall!. But a better met$ d . uld be t put a call t t$is pr cedure int a trigger 2 r t$e table. >$e 2 ll .ing trigger de2initi n . uld . r,A
!)E('E =) )EB1(!E ')9JJE) em$hierorder (@'E) 97/E)' =) DE1E'E =) EBD('E =@ m+r =7 Em$ 4EJ97 u$date_em$_hier; E7D;

>$is trigger . uld aut maticall! maintain t$e $ierarc$! a2ter an! updates t t$e table t$at . uld a22ect t$e $ierarc$! &i.e. updates t t$e MFR c lumn*.

Draw$ac/s to This Techni=ue


O2 c urse t$ere are a 2e. dra.bac,s t t$is tec$ni6ueA

B. = u $a)e t create and maintain a Kden rmalizedK c lumn. H. = u $a)e t .rite and maintain t$e c de t$at p pulates t$e c lumn. I. Since ?0= update t t$e table causes ?LL 2 t$e r .s t be updated' t$ere ma! be s me per2 rmance pr blems 2 r large 2re6uentl! updated $ierarc$ies. &>$ere are .a!s t reduce t$is impact' but t$e! are usuall! design speci2ic' and ut 2 t$e sc pe 2 t$is tip*.

Tip #:2: ) Sin"!e ;ierarchy >iew for ?u!tip!e ;ierarchies (Type: SQL)
L$en ! u use $ierarc$ical 6ueries &6ueries using CO00EC> B= and #R/OR*' ! u al.a!s $a)e t speci2! t$e t p 2 a particular $ierarc$! using t$e KS>?R> L/>%K s!ntaC. >$is is 2ten d ne b! $ard c ding t$e #K 2 t$e t p 2 t$e $ierarc$! in t$e KS>?R> L/>%K clause. % .e)er' i2 ! u $a)e man! $ierarc$ies in t$e same table' ! u mig$t .ant t be able t $a)e t$e same pr gram use ?0= 2 t$e $ierarc$ies' and speci2! .$ic$ $ierarc$! &and t$us' .$ic$ KS>?R> L/>%K ,e!* at runtime. L uldn(t it be nice i2 ! u c uld put t$e $ierarc$! 6uer! in a )ie.' and simpl! speci2! t$e $ierarc$! t use at runtimeJ Lell' ! u canM >$is tip .ill s$ . ne tec$ni6ue 2 r d ing t$is.

Startin" 6ith ) Standard ;ierarchy Query


/ .ill use t$e in2am us EM# table t illustrate t$is tec$ni6ue. % .e)er' since t$e standard emp table nl! $as ne $ierarc$! &starting .it$ KK/0FK*' / added a sec nd $ierarc$!. / als updated t$e DE#>0O 2 r all 2 t$e standard rec rds t $a)e t$e same DE#>0O &.$ic$ / use t di22erentiate t$e t. $ierarc$ies*. Using t$is tables' ! u mig$t use t$e 2 ll .ing SNL 2 r a standard $ierarc$ical 6uer!A
/012 1 /E1E!' level" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 2 @)=G Em$ 3 !=77E!' 4I B)9=) em$<em$no = em$<m+r T3 /'()' 59'6 em$<em$no = .3D /012 C 1EFE1 E7(GE EGB7= GJ) DEB'7=

;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; 1 :97J .3D 10 2 41(:E ND. .3D 10 3 G()'97 N8T ND. 10 3 (11E7 TDD ND. 10 3 'E)7E) .TT ND. 10 3 A(GE/ D00 ND. 10 3 5()D 821 ND. 10 2 !1(): .2 .3D 10 3 G911E) D3T .2 10 2 A=7E/ 8NN .3D 10 3 @=)D D02 8NN 10 T /G9'6 3ND D02 10 8 :en DDD 3ND 10 3 /!='' .. 8NN 10 T (D(G/ . N .. 10

9 $laced a second hierarch# in the same table" this one startin+ with ,/=7J,% /012 1 /E1E!' level" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 2 @)=G Em$ 3 !=77E!' 4I B)9=) em$<em$no = em$<m+r T3 /'()' 59'6 em$<em$no = N000 /012 C
1EFE1 E7(GE EGB7= GJ) DEB'7= ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; 1 /=7J N000 T0 2 J=GEX N001 N000 T0 3 59119(G/ N002 N001 T0 T D9):/E7 N003 N002 T0 8 (':97/ N00T N003 T0 8 DE/XE11 N008 N003 T0 8 DEF9'' N00N N003 T0 2 /G9'6 N00 N000 T0 3 JE=)JE N00. N00 T0 3 A=7E/ N00D N00 T0 T G911E) N010 N00D T0 T 4(:E) N011 N00D T0

Tryin" to ?a/e The Query more 3eneric


Let(s tr! lea)ing 22 t$e KS>?R> L/>%K in a )ie. in an attempt t ma,e a generic $ierarc$! )ie.A
/012 !)E('E =) )EB1(!E F9E5 Em$_6ier (/ 2 /E1E!' level lvl" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 3 @)=G Em$ T !=77E!' 4I B)9=) em$<em$no = em$<m+r 8 C

0 . i2 .e select 2r m t$is )ie. .it$ ut an! predicates' t$e 6uer! .ill still return' but it .ill return t$e results 2 a $ierarc$! starting .it$ EPER= rec rd in t$e table. F r eCampleA
/012 /E1E!' lvl" ename" em$no" m+r" de$tno 2 @)=G Em$_6ier 3 C 1EFE1 E7(GE EGB7= GJ) DEB'7= ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; 1 :97J .3D 10 2 41(:E ND. .3D 10 3 G()'97 N8T ND. 10 3 (11E7 TDD ND. 10 3 'E)7E) .TT ND. 10 3 A(GE/ D00 ND. 10 3 5()D 821 ND. 10 2 !1(): .2 .3D 10 3 G911E) D3T .2 10 2 A=7E/ 8NN .3D 10 3 @=)D D02 8NN 10 T /G9'6 3ND D02 10 8 :en DDD 3ND 10

< < 1 /=7J 2 J=GEX 3 59119(G/ T D9):/E7 8 (':97/ 8 DE/XE11 8 DEF9'' 2 /G9'6 3 JE=)JE 3 A=7E/ T G911E) T 4(:E) 1 J=GEX 2 59119(G/ 3 D9):/E7

3 T 1 2 2 2 2 2 1 2 1 2 3 T 2 3 1 1 1 1 1 < <

/!='' (D(G/ 41(:E G()'97 (11E7 'E)7E) A(GE/ 5()D !1(): G911E) A=7E/ @=)D /G9'6 :en /!='' (D(G/ G()'97 (11E7 'E)7E) A(GE/ 5()D < <

< N000 N001 N002 N003 N00T N008 N00N N00 N00. N00D N010 N011 N001 N002 N003

<

< <

.. . N ND. N8T TDD .TT D00 821 .2 D3T 8NN D02 3ND DDD .. . N N8T TDD .TT D00 821
< T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0

< <

8NN .. .3D ND. ND. ND. ND. ND. .3D .2 .3D 8NN D02 3ND 8NN .. ND. ND. ND. ND. ND.

10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 < <

N000 N001 N002 N003 N003 N003 N000 N00 N00 N00D N00D N000 N001 N002

< < < <


< <

<

< <

<

<

< <

< <

&0O>EA / did n t displa! t$e c mplete results 2 t$is 6uer!* 0 tice t$at t$e results start .it$ t$e standard $ierarc$! &beginning .it$ KK/0FK*' 2 ll .ed b! an t$er $ierarc$! starting .it$ KBL?KEK &.$ic$ is a c$ild 2 KK/0FK' and s$ uld n t $a)e it(s .n $ierarc$!*' 2 ll .ed b! KCL?RKK' KVO0ESK' etc. >$ere are e)en ne le)el $ierarc$ies 2 r t$e rec rds at t$e b tt m 2 t$e tree &i.e. KM?R>/0K' K?LLE0K' etc.*. 0 . t$is 6uer! is generic' and it .ill als pic, up t$e Hnd c mplete $ierarc$! &starting .it$ KSO0FK*. = u can use t$e DE#>0O c lumn t select ne r t$e t$er 2 t$e $ierarc$iesA
/012 /E1E!' lvl" ename" em$no" m+r" de$tno 2 @)=G Em$_6ier 3 56E)E de$tno = T0 3 C

1EFE1 E7(GE EGB7= GJ) DEB'7= ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;

1 /=7J 2 J=GEX 3 59119(G/ T D9):/E7 8 (':97/ 8 DE/XE11 8 DEF9'' 2 /G9'6 3 JE=)JE 3 A=7E/ T G911E) T 4(:E) 1 J=GEX 2 59119(G/ 3 D9):/E7 T T 1 2 3 3 3 1 2 2 2 1 1 1 1 2 2 3 3 1 1 2 2 1 1

N000 N001 N002 N003 N00T N008 N00N N00 N00. N00D N010 N011 N001 N002 N003 N008 N00N N002 N003 N00T N008 N00N N003 N00T N008 N00N N00T N008 N00N N00 N00. N00D N010 N011 N00. N00D N010 N011 N010 N011

N000 N001 N002 N003 N003 N003 N000 N00 N00 N00D N00D N000 N001 N002

T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0

DE/XE11 DEF9'' 59119(G/ D9):/E7 (':97/ DE/XE11 DEF9'' D9):/E7 (':97/ DE/XE11 DEF9'' (':97/ DE/XE11 DEF9'' /G9'6 JE=)JE A=7E/ G911E) 4(:E) JE=)JE A=7E/ G911E) 4(:E) G911E) 4(:E)

(':97/

N00T
N003 N003 N001 N002 N003 N003 N003 N002 N003 N003 N003 N003 N003 N003 N000 N00 N00 N00D N00D N00 N00 N00D N00D N00D N00D

N003

T0

>$is limits t$e 6uer! t ne 2 t$e $ierarc$ies' but it d es n t eliminate t$e spuri us $ierarc$ies. >$ere2 re' lea)ing 22 t$e S>?R> L/>% predicate is 2airl! useless

<sin" a Data$ase %unction to Dynamica!!y Determine the Top of the ;ierarchy


/n rder t add t$e KS>?R> L/>%K bac, int t$e )ie.' !et ma,e t$e )ie. d!namic' ! u can create a database 2uncti n t$at returns t$e t p parent 2 a $ierarc$! gi)en a ,e! t$at identi2ies t$e $ierarc$! &t$e deptn in t$is eCample*. >$e 2 ll .ing 2uncti n d es t$is 2 r ur eCampleA
!)E('E =) )EB1(!E @E7!'9=7 Jet_Em$_'o$&$_De$t7o 97 7EG4E)* )E'E)7 7EG4E) 9/ v_'o$Barent 7EG4E); 4EJ97 /E1E!' @)=G 56E)E (7D em$no 97'= v_'o$Barent Em$ De$tno = $_De$tno m+r 9/ 7E11;

)E'E)7&v_'o$Barent*;

E7D; C

0 .' .e update t$e )ie.' adding a S>?R> L/>% clause t$at uses t$e 2uncti nA
/012 !)E('E =) )EB1(!E F9E5 Em$_6ier (/ 2 /E1E!' level lvl" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 3 @)=G Em$ T !=77E!' 4I B)9=) em$<em$no = em$<m+r 8 /'()' 59'6 em$<em$no = Jet_Em$_'o$&em$<de$tno* N C /012 /E1E!' lvl" ename" em$no" m+r" de$tno 2 @)=G Em$_6ier 3 C
1F1 ;;;;;;;;; 1 2 3 3 3 3 3 2 3 2 3 T 8 3 T 1 2 3 T 8 8 8 2 3 3 T T E7(GE EGB7= GJ) DEB'7= ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; :97J .3D 10 41(:E ND. .3D 10 G()'97 N8T ND. 10 (11E7 TDD ND. 10 'E)7E) .TT ND. 10 A(GE/ D00 ND. 10 5()D 821 ND. 10 !1(): .2 .3D 10 G911E) D3T .2 10 A=7E/ 8NN .3D 10 @=)D D02 8NN 10 /G9'6 3ND D02 10 :en DDD 3ND 10 /!='' .. 8NN 10 (D(G/ . N .. 10 /=7J N000 T0 J=GEX N001 N000 T0 59119(G/ N002 N001 T0 D9):/E7 N003 N002 T0 (':97/ N00T N003 T0 DE/XE11 N008 N003 T0 DEF9'' N00N N003 T0 /G9'6 N00 N000 T0 JE=)JE N00. N00 T0 A=7E/ N00D N00 T0 G911E) N010 N00D T0 4(:E) N011 N00D T0

?s ! u can see' .e n . $a)e a )ie. t$at .ill return O0L= t$e c mplete $ierarc$! 2 b t$ $ierarc$ies .e $a)e de2ined. Le can simpl! add a predicate t t$e SELEC> 2r m t$e )ie. t nl! displa! ne 2 t$e $ierarc$ies' t$ereb! gi)ing us t$e d!namic selecti n 2 t$e $ierarc$! 2r m t$e )ie.A
/012 /E1E!' lvl" ename" em$no" m+r" de$tno 2 @)=G Em$_6ier 3 56E)E DEB'7= = T0 T C

1F1 E7(GE EGB7= GJ) DEB'7= ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; 1 /=7J N000 T0 2 J=GEX N001 N000 T0 3 59119(G/ N002 N001 T0 T D9):/E7 N003 N002 T0 8 (':97/ N00T N003 T0 8 DE/XE11 N008 N003 T0

8 2 3 3 T T

DEF9'' /G9'6 JE=)JE A=7E/ G911E) 4(:E)

N00N N00 N00. N00D N010 N011

N003 N000 N00 N00 N00D N00D

T0 T0 T0 T0 T0 T0

#uttin" a coup!e of Tips To"ether


/n a pre)i us tip &>ip O98*' / detailed a met$ d t all . R ins t $ierarc$ical 6ueries. Le can c mbine t$at tec$ni6ue .it$ t$e ne 2r m t$is tip t gi)e us a )er! p .er2ul )ie.A
!)E('E =) )EB1(!E F9E5 em$_hier (/ /E1E!' em$hier<em$level" em$hier<ename ind_ename" em$hier<ename "em$hier<em$no "de$t<de$tno" de$t<dname" de$t<loc "em$<ename m+r_ename @)=G De$t" Em$ "&select level em$level" 1B(D&' '"23level;2*WWename ename" em$no" m+r" de$tno from Em$ connect b# $rior em$no = m+r start with em$no = Jet_Em$_'o$&em$<de$tno* * em$hier 56E)E em$hier<de$tno = de$t<de$tno (7D em$hier<m+r = em$<em$no &S* /012 2 3 T select ind_ename" m+r_ename" dname" loc from em$_hier where de$tno = T0 C
GJ)_E7(GE D7(GE ;;;;;;;;;; ;;;;;;;;;;;;;; =BE)('9=7/ /=7J =BE)('9=7/ J=GEX =BE)('9=7/ 59119(G/ =BE)('9=7/ D9):/E7 =BE)('9=7/ D9):/E7 =BE)('9=7/ D9):/E7 =BE)('9=7/ /=7J =BE)('9=7/ /G9'6 =BE)('9=7/ /G9'6 =BE)('9=7/ A=7E/ =BE)('9=7/ A=7E/ =BE)('9=7/ 1=! ;;;;;;;;;;;;; 4=/'=7 4=/'=7 4=/'=7 4=/'=7 4=/'=7 4=/'=7 4=/'=7 4=/'=7 4=/'=7 4=/'=7 4=/'=7 4=/'=7

%ere is an eCample 2 using t$e )ie.A

97D_E7(GE ;;;;;;;;;;;;;;;;;;;; /=7J J=GEX 59119(G/ D9):/E7 (':97/ DE/XE11 DEF9'' /G9'6 JE=)JE A=7E/ G911E) 4(:E)

7!ic/ @hereA for a SQL script that creates and popu!ates the +?# ta$!e used in this e.amp!e* then runs the e.amp!e =ueries. Tip #:B: <sin" 40n!ine >iews4 to Coin to ;ierarchica! Queries (Type: SQL)
%a)e ! u e)er tried t R in t a $ierarc$ical 6uer! &a 6uer! using CO00EC> B= and #R/OR* nl! t get t$is messageA
=)(;01T3 % cannot have ?oin with !=77E!' 4I

One 2 t$e limitati ns 2 $ierarc$ical 6ueries is t$at ! u cann t R in t t$em. % .e)er' t$ere are 2ten times ! u . uld li,e t R in t t$em an!.a!. F r instance' i2 t$e $ierarc$! table nl! $as surr gate ,e!s' and ! u . uld li,e t displa! t$e real )alue. >$is tip s$ .s $ . ! u can use K/nline Pie.sK &.$ic$ are SELEC>s in t$e FROM clause* t R in tables t a $ierarc$ical 6uer!.

Startin" 6ith ) Standard ;ierarchy Query


/ .ill use t$e in2am us EM#"DE#> tables t illustrate t$is tec$ni6ue. Using t$ese tables' ! u mig$t use t$e 2 ll .ing SNL 2 r a standard $ierarc$ical 6uer!A
/012 1 /E1E!' level" 1B(D&' '"23level;2*WWem$<ename ename" em$<em$no" em$<m+r" em$<de$tno 2 @)=G Em$ 3 !=77E!' 4I B)9=) em$<em$no = em$<m+r T3 /'()' 59'6 em$<em$no = .3D /012 C 1EFE1 E7(GE EGB7= GJ)

;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; 1 :97J .3D 10 2 41(:E ND. .3D 30 3 G()'97 N8T ND. 30 3 (11E7 TDD ND. 30 3 'E)7E) .TT ND. 30 3 A(GE/ D00 ND. 30 3 5()D 821 ND. 30 2 !1(): .2 .3D 30 3 G911E) D3T .2 10 2 A=7E/ 8NN .3D 20 3 @=)D D02 8NN 20 T /G9'6 3ND D02 20 8 :en DDD 3ND 20 3 /!='' .. 8NN 20 T (D(G/ . N .. 20

DEB'7=

Try to Coin This Query To the D+#T Ta$!e


/2 ! u tr! t R in t$is 6uer! t t$e DE#> table' it . n(t . r,A
/012 l 1 select level" 1B(D&' '"23level;2*WWename ename" em$no" m+r" de$t<de$tno" de$t<dname 2 from em$" de$t 3 where em$<de$tno = de$t<de$tno T connect b# $rior em$no = m+r 83 start with em$no = .3D /012 C from em$" de$t 3 E))=) at line 2% =)(;01T3 % cannot have ?oin with !=77E!' 4I

#!ace the ;ierarchica! Query in an 40n!ine >iew4

Since Oracle 7.I' .e c uld actuall! use a c mplete SELEC> statement as ne 2 t$e KtablesK in a 6uer!. Using t$is tec$ni6ue' .e can turn t$e $ierarc$ical 6uer! int a KtableK and R in it d t$e DE#> tableA
/012 l 1 /E1E!' em$hier<em$level" em$hier<ename" em$hier<em$no" de$t<de$tno" de$t<dname 2 @)=G De$t 3 "&select level em$level" 1B(D&' '"23level;2*WWename ename" em$no" m+r" de$tno T from Em$ 8 connect b# $rior em$no = m+r N start with em$no = .3D * em$hier .3 56E)E em$hier<de$tno = de$t<de$tno /012 C
EGB1EFE1 E7(GE EGB7= DEB'7= D7(GE ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;;;;;; 1 :97J .3D 10 (!!=E7'97J 2 41(:E ND. 30 /(1E/ 3 G()'97 N8T 30 /(1E/ 3 (11E7 TDD 30 /(1E/ 3 'E)7E) .TT 30 /(1E/ 3 A(GE/ D00 30 /(1E/ 3 5()D 821 30 /(1E/ 2 !1(): .2 30 /(1E/ 3 G911E) D3T 10 (!!=E7'97J 2 A=7E/ 8NN 20 )E/E()!6 3 @=)D D02 20 )E/E()!6 T /G9'6 3ND 20 )E/E()!6 8 :en DDD 20 )E/E()!6 3 /!='' .. 20 )E/E()!6 T (D(G/ . N 20 )E/E()!6

>$e SELEC> statement inside t$e parent$eses is treated Rust as i2 it .ere a )ie. t$at ! u are R ining t . /t is gi)en an alias' Kemp$ierK' .$ic$ is used t re2er t it in t$e SELEC> clause &i.e. Kemp$ier.enameK*' and in t$e L%ERE clause &i.e. Kemp$ier.deptn K*. Since it is treated li,e a )ie.' .e can R in it t t$e Dept table .it$ t$e 2 ll .ing predicateA
56E)E em$hier<de$tno = de$t<de$tno

>$is .ill all . ! u t displa! t$e department name &KD0?MEK* in ! ur $ierarc$ical 6uer!.

#uttin" the Query into a >iew


Nuite 2ten' t$ese $ierarc$ical 6ueries can be use2ul in man! pr grams and rep rts. /t is 2ten $elp2ul t create a )ie. t$at lists t$e $ierarc$! and R ins t use2ul tables. %ere is an eCample 2 a )ie. using t$e EM#"DE#> tables. >$is )ie. all .s ! u t list t$e department name and l cati n and t$e manager name in t$e 6uer!A
!)E('E =) )EB1(!E F9E5 em$_hier (/ /E1E!' em$hier<em$level" em$hier<ename ind_ename" em$hier<ename "em$hier<em$no "de$t<de$tno" de$t<dname" de$t<loc "em$<ename m+r_ename @)=G De$t" Em$ "&select level em$level" 1B(D&' '"23level;2*WWename ename" em$no" m+r" de$tno from Em$

connect b# $rior start with em$no * em$hier 56E)E em$hier<de$tno = (7D em$hier<m+r =

em$no = m+r = .3D de$t<de$tno em$<em$no &S*

%ere is an eCample 2 using t$e )ie.A

/012 select ind_ename" m+r_ename" dname" loc 2 from em$_hier 3 /012 C


97D_E7(GE ;;;;;;;;;;;;;;;;;;;; :97J 41(:E G()'97 (11E7 'E)7E) A(GE/ 5()D !1(): G911E) A=7E/ @=)D /G9'6 :en /!='' (D(G/ GJ)_E7(GE D7(GE ;;;;;;;;;; ;;;;;;;;;;;;;; (!!=E7'97J :97J /(1E/ 41(:E /(1E/ 41(:E /(1E/ 41(:E /(1E/ 41(:E /(1E/ 41(:E /(1E/ :97J /(1E/ !1(): (!!=E7'97J :97J )E/E()!6 A=7E/ )E/E()!6 @=)D )E/E()!6 /G9'6 A=7E/ /!='' )E/E()!6 )E/E()!6 )E/E()!6 1=! ;;;;;;;;;;;;; 7E5 I=): !69!(J= !69!(J= !69!(J= !69!(J= !69!(J= !69!(J= !69!(J= 7E5 I=): D(11(/ D(11(/ D(11(/ D(11(/ D(11(/ D(11(/

Tip #DE: Listin" ,ecords with the ;i"hest >a!ues usin" SQL 'n!y. (Type: SQL)
>$ere are times .$ere ! u .ant t simpl! return t$e r .s .it$ a certain number 2 t$e $ig$est & r l .est* )alues 2 r a certain c lumn. >$is t!pe 2 2uncti nalit! is eas! t implement in #L"SNL &Rust rder b! t$e c lumn and grab t$e 2irst n r .s 2r m t$e 6uer!*' but m re di22icult t d using SNL nl!. >$is tip s$ .s ! u a met$ d t d t$is in SNL.

Data <sed for The +.amp!es in this Tip


>$e 2 ll .ing data &2r m t$e in2am us EM# table* .ill be used 2 r all 2 t$e eCamples in t$is >ipA
/012 desc em$ 7ame ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; EGB7= E7(GE A=4 GJ) 69)ED('E /(1 !=GG DEB'7= /012 7ullK '#$e ;;;;;;;; ;;;; 7=' 7E11 7EG4E)&T* !6()&10* !6()&D* 7EG4E)&T* D('E 7EG4E)& "2* 7EG4E)& "2* 7=' 7E11 7EG4E)&2*

/E1E!' em$no" sal @)=G EGB;

EGB7= /(1 ;;;;;;;;; ;;;;;;;;; T1 T200 TN N.00 DD D000 23 2000 11 T000 10 3800 81 T800 82 T800 83 .000 8T 2D00 10 rows selected<

,'61<? does not wor/9


Man! SNL begginers are tempted t tr! t use ROL0UM al ng .it$ an ORDER B= t limit t$e r .s returned t t$e $ig$est )alues. % .e)er' t$is d es n t . r,' becuase Oracle sets t$e ROL0UM )alue before t$e 6uer! results are rderedM C nsider t$e 2 ll .ing 6uer!A
/012 /E1E!' em$no" sal" rownum 2 @)=G Em$ 3 =)DE) 4I sal DE/! T /012 C EGB7= /(1 )=57EG ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; DD D000 3 83 .000 D TN N.00 2 81 T800 82 T800 . T1 T200 1 11 T000 8 10 3800 N 8T 2D00 10 23 2000 T

0 tice t$at t$e rec rds are rdered b! t$e S?L c lumn' but n t t$e ROL0UM c lumn. /2 ! u added a .$ere clause t limit t$e 6uer! t t$e 2irst t$ree ROL0UMs' ! u . uld getA
/012 1 2 3 T3 /012 l /E1E!' em$no" sal" rownum @)=G Em$ 56E)E )=57EG R T =)DE) 4I sal DE/! C

EGB7= /(1 )=57EG ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; DD D000 3

TN T1

N.00 T200

2 1

L$ic$ d es 0O> return t$e t$ree $ig$est S?LsM

So!ution: 7orre!ated Su$Query to Same Ta$!e


One s luti n 2 r t$is pr blem is t use a c rrelated sub6uer! t t$e same table. >$e 2 ll .ing select .ill return t$e c rrect r .sA
/012 1 2 3 T 83 /012 l /E1E!' em$no" sal @)=G Em$ e1 56E)E 3 2 &/E1E!' !=E7'&3* @)=G Em$ e2 56E)E e1<sal R e2<sal* =)DE) 4I /(1 desc C

EGB7= /(1 ;;;;;;;;; ;;;;;;;;; DD D000 83 .000 TN N.00

F r e)er! r . pr cessed b! t$e main 6uer!' t$e c rrelated sub6uer! returns a c unt &COUNT(*) * 2 t$e number 2 r .s .it$ $ig$er salaries &WHERE e1.sal < e2.sal). >$en t$e main 6uer! nl! returns r .s t$at $a)e 2e.er t$an t$ree salaries t$at are $ig$er &WHERE 3 > ...). F r eCample' 2 r EM#0O;9:' t$e salar! is K:G88K. >$ere is nl! B r . .it$ a $ig$er salar! &EM#0O;44*' s t$e sub6uer! returns KBK' .$ic$ is less t$an I' causing t$e KL%ERE I 3 ...K t e)aluate t >RUE' t$ereb! returning t$e r ..

) #ro$!em 6ith This Techni=ue


% .e)er' t$ere is a pr blem .it$ t$is met$ d. L$at i2 t$ere are m re t$an ne r . .it$ t$e same salar!J C nsider t$e 2 ll .ing 6uer!' .$ere .e c$ange it t return t$e 2irst 9 r .sA
/012 1 2 3 T 83 /012 l /E1E!' em$no" sal @)=G Em$ e1 56E)E T 2 &/E1E!' !=E7'&3* @)=G Em$ e2 56E)E e1<sal R e2<sal* =)DE) 4I /(1 desc C

EGB7= /(1 ;;;;;;;;; ;;;;;;;;; DD D000 83 .000 TN N.00

81 82

T800 T800

/nstead 2 returning 9 r .s' it returned 5M >$is is because t$is tec$ni6ue returns ?LL 2 t$e r .s .it$ t$e $ig$est 9 salaries' n t t$e 2irst 9 r .s. >$is is a pr blem .it$ t$is tec$ni6ue' s ! u need t ma,e sure t$at it is acceptible in ! ur design be2 re ! u use it.

)n )!ternati-e Techni=ue which Lists ,an/


/2 ! u .ant t use a R in instead 2 a c rrelated sub6uer!' ! u c uld use t$e 2 ll .ing selectA
/012 1 2 3 T 83 /012 l /E1E!' e1<de$tno" e1<em$no" e1<sal" !=E7'&distinct e2<em$no* @)=G Em$ e1" Em$ e2 56E)E e1<sal R= e2<sal J)=EB 4I e1<de$tno" e1<em$no" e1<sal =)DE) 4I !=E7'&distinct e2<em$no* C

DEB'7= EGB7= /(1 !=E7'&D9/'97!'E2<EGB7=* ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;; 30 DD D000 1 80 83 .000 2 20 TN N.00 3 T0 81 T800 8 80 82 T800 8 20 T1 T200 N T0 11 T000 T0 10 3800 . 80 8T 2D00 D 30 23 2000 10

>$is select turns t$e c rrelated sub6uer! int a sel2@R in .it$ a FROU# B=. >$is all .s us t c$ange t$e c unt int a s rt 2 R?0K. % .e)er' t$e pr blem .it$ e6ual salaries remains &n tice t$e t. rec rds .it$ a Kran,K 2 5*. >$is ran, can t$en be used t select t$e 2irst t$ree r .sA
/012 1 2 3 T 8 N3 /012 l /E1E!' e1<de$tno" e1<em$no" e1<sal" !=E7'&distinct e2<em$no* @)=G Em$ e1" Em$ e2 56E)E e1<sal R= e2<sal J)=EB 4I e1<de$tno" e1<em$no" e1<sal 6(F97J !=E7'&distinct e2<em$no* R T =)DE) 4I !=E7'&distinct e2<em$no* C

DEB'7= EGB7= /(1 !=E7'&D9/'97!'E2<EGB7=* ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;; 30 DD D000 1

80 20

83 TN

.000 N.00

2 3

One ad)antage 2 t$is met$ d is t$at it can be easil! used t return t$e rec rd 2 r Rust ne ran,ing. F r eCampleA
/012 1 2 3 T 8 N3 /012 l /E1E!' e1<de$tno" e1<em$no" e1<sal" !=E7'&distinct e2<em$no* @)=G Em$ e1" Em$ e2 56E)E e1<sal R= e2<sal J)=EB 4I e1<de$tno" e1<em$no" e1<sal 6(F97J !=E7'&distinct e2<em$no* = =)DE) 4I !=E7'&distinct e2<em$no* C

DEB'7= EGB7= /(1 !=E7'&D9/'97!'E2<EGB7=* ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;; T0 11 T000

Tip #D2: Script to List Tri""er +rrors F Line 1um$ers (Type: #LGSQL)
L$en a trigger is created 2 r a table' s metimes t$ere are c mpilati n err rs &$e!' .ere n t all per2ect pr grammers*. L$en ! u $a)e err rs' ! u can list t$e trigger(s s!ntaC err rs b! using t$e S%OL ERRORS >R/FFER S>rigger_0ame3 c mmand. % .e)er' man! times t$ese messages are cr!ptic' and it can be di22icult t matc$ t$e err r t t$e speci2ic line 2 c de in t$e trigger. >$is tip s$ .s a met$ d t$at can be used t list t$e err rs' al ng .it$ t$e trigger s urce' and indicating .$ic$ line 2 s urce $as eac$ err r. M! t$an,s t >im Oni ns' a #rincipal >ec$nical C nsultant at ?>W> in t$e UK 2 r as,ing i2 it .as p ssible t d t$is' and spurring me n t attempting it. %e als re)ie.ed t$e script and made impr )ement suggesti ns. ?ls ' t$an,s t VuriR M dic 2 t$e Republic 2 Sl )enia Ministr! 2 Finance 2 r p inting ut a maR r 2la. in t$e riginal tip. The Stanard S;'6H+,,',S command %ere is an eCample 2 using t$e S%OL ERRORS c mmand 2 r triggersA
:(':97/2 !)E('E =) )EB1(!E ')9JJE) 'est_'ri++er 2 before insert or u$date of $rice on items 3 for each row T 4EJ97 8 ;; 9f ePtended amout is +reater than $10"000 set the status N ;; to $endin+ a$$roval" else a$$rove the item< 9@ &new<$rice3new<amount* 2 10000 '6E7 . %new<status = 'B'; D E1/E 10 %new<status %= '('; 11 E7D 9@; 12 E7D;

C 5arnin+% 'ri++er created with com$ilation errors< :(':97/2 show errors tri++er 'est_'ri++er 197EC!=1 E))=) ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 8C1 B1/;00103% Encountered the s#mbol ,=, when eP$ectin+ one of the followin+% %= < & > Q ; indicator 'he s#mbol ,%= was inserted before ,=, to continue<

>$e err rs g int USER_ERRORS' Rust li,e st red pr cedure err rsA
DE/2=57E)2 l 1 /E1E!' line" tePt @)=G Eser_Errors 2 56E)E name = 'BF7'_DEB_E7'_E)_')' 33 order b# se-uence DE/2=57E)2 C 2 197E 'EH' ;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 8 B1/;00103% Encountered the s#mbol ,=, when eP$ectin+ one of the followin+% %= < & > Q ; indicator 'he s#mbol ,%= was inserted before ,=, to continue< DE/2=57E)2

?er"in" the errors with the source. ?s ! u can see' it is 2airl! eas! t select 2r m t$is table t see t$e err rs. But / .anted t d s met$ing m re. / .ant t list t$e c de 2 t$e trigger al ng .it$ t$e arr .s' and $a)e t$e listing p int t t$e line in t$e c de .it$ t$e err r' Rust li,e / did 2 r pac,ages' pr cedures' W Functi ns in >ip OI. >$e #L"SNL s urce 2 r pac,ages' pr cedures' W 2uncti ns is st red in USER_SOURCE. % .e)er' t$e s urce 2 t$e trigger is 0O> st red in USER_SOURCE. /nstead' it is st red in t$e >R/FFER_BOD= c lumn 2 USER_>R/FFER. Since >R/FFER_BOD= is a LO0F c lumn' it is di22icult t . r, .it$ directl! &i.e. ! u cann t use SUBS>R and /0S>R n it*. Because 2 t$is' / .r te a st red pr cedure t$at parses it int lines' gi)es t$e lines line numbers' t$en matc$es t$ se lines t t$e )alue in t$e L/0E c lumn 2 USER_ERRORS. >$en' using DBMS_OU>#U> t displa! t$e results' / can get t$e

utput 2 t$is pr gram t l , )er! similar t t$e st red pr grams err r utput / g t in >ip OI. %ere is t$e pr cedureA
C 33333333333333333333333333333333333333333333333333333333333333333333333 3C C3 19/'_')9J_E)) ; ( $rocedure that uses D4G/_=E'BE' to list the 3C C3 com$ilation errors of a tri++er" alon+ with the 3C C3 tri++er's source< (lso indicates the source line 3C C3 with the error< 3C C3 3C C3 9n$uts% $_'ri++er = 'he tri++er name< 3C C3 3C C3 (uthor% :en (tkins &:en>arrowsent<com* 3C C3 Brinci$al !onsultant ; ()9/ !or$oration 3C C3 3C C3 Blease feel free to use and modif# this scri$t as lon+ as it is not 3C C3 sold or included in an# software without the $rior $ermission of 3C C3 the author< 9f #ou do make +ood im$rovements" $lease send them to 3C C3 me and 9 will incor$orate them in a future version of the scri$t 3C C3 &+ivin+ #ou credit of courseL*< 3C C3 3C C3 Godifications% 3C C 3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 3C C3 Fer Date 4# !han+e 3C C3 ;;; ;;;;;;;;; ;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;3C C3 1<0 08;G();D. :en (tkins 5ritten< 3C C 33333333333333333333333333333333333333333333333333333333333333333333333 3C !)E('E =) )EB1(!E B)=!EDE)E 19/'_')9J_E))&$_'ri++er 97 F()!6()2* (/

v_'ri+ 1=7J; b_!ontinue 4==1E(7 %= 'rue; v_7um1ines 7EG4E) %= 0; v_1ine F()!6()2&2T0*; v_7Pt!hr 7EG4E) %= 0; v_1st!hr 7EG4E) %= 0; 'IBE 1ine'ab'#$ 9/ '(41E =@ F()!6()2&2T0* 97DEH b# 497()I_97'EJE); t_1ines 1ine'ab'#$; !E)/=) err_cur 9/ /E1E!' line" tePt @)=G Eser_Errors 56E)E name = $_'ri++er (7D tePt not like 'Q/tatement i+noredQ'; i 7EG4E); v_BrefiP !6()&10*; v_DD1!ursor 7EG4E); v_DD1)eturn 7EG4E); 4EJ97 ;; ;; @etch the tri++er code into a variable that will be used to $arse it< ;; 4EJ97 /E1E!' tri++er_bod# 97'= v_'ri+ @)=G Eser_'ri++ers 56E)E 'ri++er_7ame = $_'ri++er; EH!EB'9=7 56E7 7=_D('(_@=E7D '6E7 )aise_($$lication_Error&;20001"''ri++er does not ePist% 'WW $_'ri++er*; E7D; ;; ;; Ese D4G/_/01 to ePecute the command that $laces the errors into E/E)_E))=)/ ;; v_DD1!ursor %= dbms_s-l<o$en_cursor; D4G/_/01<Barse&v_DD1!ursor"'(1'E) ')9JJE) 'WW$_'ri++erWW' !=GB91E DE4EJ'"1*; v_DD1)eturn %= dbms_s-l<ePecute&v_DD1!ursor*; D4G/_/01<!lose_!ursor&v_DD1!ursor*; ;; ;; 7ow loo$ throu+h the lines in the tri++er code and $arse it into se$arate ;; record in a B1C/01 table< ;; 5691E b_!ontinue 1==B v_7um1ines %= v_7um1ines S 1; v_7Pt!hr %= 97/')&v_'ri+" !6)&10*"1"v_7um1ines*; v_1ine %= /E4/')&v_'ri+" v_1st!hrS1" &v_7Pt!hr;v_1st!hr**; t_1ines&v_7um1ines* %= v_1ine; 9@ v_7Pt!hr = 0 '6E7 b_!ontinue %= @alse; E1/E v_1st!hr %= v_7Pt!hr; E7D 9@;

E7D 1==B; ;; ;; 1oo$ throu+h all of the errors in E/E)_E))=)/ for this tri++er" dis$la#in+ ;; each error" followed b# the tri++ers code" with an ===2 $ointin+ to the ;; error line< ;; D4G/_=E'BE'<But_1ine&'<'*; @=) e 97 err_cur 1==B D4G/_=E'BE'<But_1ine&e<tePt*; D4G/_=E'BE'<But_1ine&'<'*; @=) i 97 1<<v_7um1ines 1==B 9@ e<line = i '6E7 v_BrefiP %= '========2 '; E1/E ;; 7ote% 'he dots &<<<* are used because D4G/_=E'BE' normall# stri$s leadin+ s$aces v_BrefiP %= '<<<<<<<<< '; E7D 9@; D4G/_=E'BE'<But_1ine&v_BrefiPWWto_char&i*WW'% 'WWt_1ines&i**; E7D 1==B; D4G/_=E'BE'<But_1ine&'<'*; E7D 1==B; E7D; C

%ere is an eCample 2 using t$e pr cedure. First' / created a simple s6l script t call t$e pr cedureA
set serverout$ut on siMe 100000 ePecute list_tri+_err&EBBE)&'O1'**;

>$en' a2ter creating t$e trigger' / can call t$is script t detail t$e err rs. )n +.amp!e of <sin" the Scripts %ere is an eCample 2 listing t$e err rs 2 r a triggerAA
:(':97/2 !)E('E =) )EB1(!E ')9JJE) 'est_'ri++er 2 before insert or u$date of $rice on items 3 for each row T 4EJ97 8 ;; 9f ePtended amout is +reater than $10"000 set the status N ;; to $endin+ a$$roval" else a$$rove the item< 9@ &new<$rice3new<amount* 2 10000 '6E7 . %new<status = 'B'; D E1/E 10 %new<status %= '('; 11 E7D 9@; 12 E7D; C 5arnin+% 'ri++er created with com$ilation errors<

:(':97/2 >tri+err 'E/'_')9JJE) B1/;00103% Encountered the s#mbol ,=, when eP$ectin+ one of the followin+% %= < & > Q ; indicator 'he s#mbol ,%= was inserted before ,=, to continue< < <<<<<<<<< 1% 4EJ97 <<<<<<<<< 2% ;; 9f ePtended amout is +reater than $10"000 set the status <<<<<<<<< 3% item< <<<<<<<<< T% ========2 8% <<<<<<<<< N% <<<<<<<<< % ;; to $endin+ a$$roval" else a$$rove the

9@ &new<$rice3new<amount* 2 10000 '6E7 %new<status = 'B'; E1/E %new<status %= '('; E7D 9@;

<<<<<<<<< .%

<<<<<<<<< D% E7D; <<<<<<<<< 10% <

Tip #DB: )n easy way to +I#L)01 and "et some statistics on your SQL. (Type: SQL5#!us)
%a)en(t ! u e)er t$ ug$t t$ere s$ uld be an easier .a! t d t$e E<#L?/0 #L?0 and >K#ROF statistics t$an t edit ! ur 6ueries t add t$e c mmands &li,e E<#L?/0 #L?0 SE>...*' r t $a)e t 2ind r .rite a script t$at aut mates t$isJ /t s$ uld be an aut matic part 2 SNL+#lus. Lell' as 2 SNL+#lus I.I it isMM >$e c mmand is called (SE> ?U>O>R?CE O0(M M! t$an,s g ut t Vac, ?pple.$ite 2 r p inting ut t$is c mmand in a p st t t$e OD>UF email lists. The S+T )<T'T,)7+ 7ommand

/n SNL+#lus I.I t$ere is a little ,n .n c mmand &at least / didn(t ,n . ab ut it until recentl!* called SE> ?U>O>R?CE. /t is d cumented in t$e ne.est SNL+#lus d cument set' but .$ reall! reads t$e .$ le d cument set 2 r c$angesJ Lell / did n t. /t is )er! simple t use. Vust t!pe t$e c mmandA

/E' (E'=')(!E =7

?nd t$en run ! ur select statement. ECampleA

/012 /E' (E'=')(!E =7 /012 /E1E!' d<de$tno" d<dname" e<em$no" e<ename 2 3 T C @)=G de$t d" em$ e 56E)E d<de$tno = e<de$tno

DEB'7= D7(GE

EGB7= E7(GE

;;;;;;;;;; ;;;;;;;;;;;;;; ;;;;;;;;;; ;;;;;;;;;; 10 (!!=E7'97J < < 30 /(1E/ 30 /(1E/ D00 A(GE/ 821 5()D .3D :97J

1T rows selected<

EPecution Blan ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 0 /E1E!' /'('EGE7' =$timiMer=!6==/E

1 2 3 T 8

0 1 2 1 T

GE)JE A=97 /=)' &A=97* '(41E (!!E// &@E11* =@ 'EGB' /=)' &A=97* '(41E (!!E// &@E11* =@ 'DEB''

/tatistics ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 0 T 2 0 0 N 0 3 N 3 2 0 1T recursive calls db block +ets consistent +ets $h#sical reads redo siMe b#tes sent via /0137et to client b#tes received via /0137et from client /0137et roundtri$s toCfrom client sorts &memor#* sorts &disk* rows $rocessed

>$ere are als s me t$er pti ns' 2 r eCample t$ere is a >R?CEO0L= pti n .$ic$ supresses t$e SNL utput. See t$e SNL+#lus I.I manual 2 r a 2ull descripti n. Some setup issues: /2 ! u g 22 and tr! t$is n ! ur instance' ! u ma! run int s me pr blems. >$ere are a 2e. setup steps t$at need t be ta,en t ma,e t$is . r,A B. Ma,e sure ! u $a)e access t #L?0_>?BLE. /2 ! u d n(t' create it using utlx lan.s!l &/t s$ uld be in a direct r! li,e -OR?CLE_%OME"rdbms7I"admin"* and ma,e sure ! u $a)e access t it 2r m t$e user ! u are using t tune t$e SNL.

H. = u als need t create t$e #LUS>R?CE r le' and grant it t t$e desired users. >$e script t create t$is r le is inA -OR?CLE_%OME"plusII"#lustrce.s6l /t $as t be run 2r m S=S in rder t $a)e t$e c rrect securit! access. >$en grant t$e r le t t$e desired users r ROLEs.

Tip #2E: Settin" the SQL5#!us prompt to the current directory. (Type: SQL5#!us)
%a)e ! u e)er $ad a SNL+#lus .ind . pen' and did n t remember .$at it(s current direct r! .asJ >$is tip .ill s$ . a met$ d t set ! ur SNL+#lus pr mpt t include t$e current direct r!. >$e SE> SNL#ROM#> c mmand can be used t set t$e SNL+#lus pr mpt t an! teCt string ! u .ant. >$e tric, is t get access t t$e current direct r! 2r m SNL+#lus' s ! u can use it in SE> SNL#ROM#>. Le $a)e access t t$e current direct r! 2r m t$e OS &)ia (cd( in DOS"0> and (p.d( in UniC*. Le can als call an OS script b! using t$e SNL+#lus %OS> c mmand. Using t$ese t. capabilities' / .r te t. scripts t$at t get$er per2 rmed t$e 2uncti n / .antedA B. ? .bat script t$at .rites a SNL script t d t$e actual SE> SNL#ROM#>' using t$e (cd( c mmand t insert t$e current direct r!. H. ? s6l script t$at eCecutes t$e .bat script and runs t$e SNL script t$at it $as .ritten. %ere are t$e t. scriptsA
set$rm$t<bat ;;;;;;;;;;;;;;;;; echo set define $ 2 tm$<s-l echo column curdir no$rint new_value curdir 22 tm$<s-l echo /E1E!' )EB1(!E&' 22 tm$<s-l cd 22 tm$<s-l echo '"!6)&10*"''* curdir @)=G DE(1; 22 tm$<s-l echo set s-l$rom$t ,&$curdir* /012 , 22 tm$<s-l set$rm$t<s-l ;;;;;;;;;;;;;;;;; host set$rm$t<bat >tm$ set define O

%ere is an eCample 2 using t$e scripts t set t$e pr mptA

/012 >set$rm$t

&!%Ud2kUworkin+* /012

;ow these scripts wor/ >$e ($ st( c mmand in t$e .s6l scripteCecutes t$e .bat script. >$e .bat script t$en .rites t$e 2 ll .ing c mmands t a temp rar! s6l script &called tmp.s6l*A

set define $ column curdir no$rint new_value curdir /E1E!' )EB1(!E&' !%Ud2kUworkin+ '"!6)&10*"''* curdir @)=G DE(1; set s-l$rom$t ,&$curdir* /012 ,

>$e .s6l script t$en eCecutes t$is temp rar! script 2ile. >$e >M#.SNL script $ad t be s c mplicated because / .as nl! using t$e DOS utput redirecti n capabilities &(3( and (33(* and / c uld nl! get t$e current direct r! int t$e 2ile n it(s .n line. >$e RE#L?CE&..'C%R&B8*'((* c mmand rem )es t$e carriage return be2 re and a2ter t$e direct r! line. >$e (c lumn .. ne._)alue( c mmand is a SNL+#LUS c mmand t$at all .s t$e )alue 2 a selected c lumn t be placed int a SNL+#lus )ariable &in t$is case (curdir(*. >$ere2 re' .$en t$e 2 ll .ing SELEC>.. is run' t$e teCt string 2 t$e current direct r! is placed in t$e (curdir( SNL+#lus )ariable. >$en t$e SE> SNL#ROM#> uses t$is )ariable t set t$e pr mpt. ?n t$er 2eature 2 t$is tec$ni6ue' is t$at ! u n . $a)e t$e SNL+#lus )ariable .it$ t$e current direct r! a)ailable 2 r t$er uses in t$is SNL+#lus sessi n. F r eCample' it can be used in a select li,eA

&!%Ufm$tUs-l* /012 select 'Ocurdir' from dual; old new 1% select 'Ocurdir' from dual 1% select '!%Ufm$tUs-l' from dual

'!%U@GB'U/0 ;;;;;;;;;;; !%Ufm$tUs-l

>$e ab )e .bat 2ile is 0O> prett!. Using #erl r s me t$er scripting language' / c uld .rite a simple script t$at . uld Rust .rite t$e SE> SNL#ROM#> c mmand directl! .it$ ut t$e use 2 t$e (c lumn( c mmand r t$e (SELEC>(.

Tip #2:: 'rderin" numerica!!y in a >),7;),2 co!umn. (Type: SQL)


%a)e ! u e)er tried t rder b! a P?RC%?RH c lumn t$at $as numeric in2 rmati n in itJ = ur 6uer! is s rted ?L#%?BE>/C?LL= instead 2 numericall!. >$at is' ! ur rder is B'B8'H'H8'H88'I'9... instead 2 B'H'I'9'B8'H8'H88. /2 ! u tr! t use >O_0UMBER in t$e rder_b! ! ur 6uer! bl .s up i2 t$ere are an! alp$a c$aracters in t$e c lumn. >$is tip details a met$ d t$at can be used t $a)e t$e rder_b! return t$e c lumns numericall! e)en i2 t$ere are s me alp$a c$aracters in t$e c lumn. C nsider t$e 2 ll .ing tableA
/012 desc 7EG4E)_/=)' 7ame 7ullK '#$e ;;;;;;;;;;;;;;;;;;; ;;;;;;;; ;;;;;;;;;;;; sortb# 7=' 7E11 F()!6()2&20* /012 /E1E!' 3 from 7EG4E)_/=)' /=)'4I ;;;;;;;;;;;;;;;;;;;; 100 ( 1 10 1(4 2 20 4444

1000 11 30 3 200 21

1T rows selected<

/2 ! u d a simple rder_b! ! ur result .ill beA


/012 /E1E!' sortb# 2 @)=G 7umber_/ort 3 =)DE) 4I sortb#; /=)'4I ;;;;;;;;;;;;;;;;;;;; 1 10 100 1000 11 1(4 2 20 200 21 3 30 ( 4444 1T rows selected<

L$ic$ is n t .$at ! u .antM % .e)er' t$e 2 ll .ing select L/LL return t$e c lumn rdered numericall!A
/012
/E1E!' sortb# 2 @)=G 7umber_/ort 3 =)DE) 4I DE!=DE&'=_!6()&7F1&1E7J'6&')(7/1('E&sortb#"'(123T8N .D0'"'('**"0**"'0'"1B(D&sor tb#".*"sortb#* T C

/=)'4I ;;;;;;;;;;;;;;;;;;;; 1 2 3 10 11 20 21

30

100 200 1000 1(4 ( 4444 1T rows selected<

0 . let(s ta,e t$at c nstruct apart t see $ . it . r,sA B. T,)1SL)T+(sortby* )12D:5JKELB * ) ) @ >$is usage 2 >R?0SL?>E strips all 2 t$e numeric c$aracters &B..4* ut 2 t$e )alue 2 sortb". F r an! r . .$ere sortb" O0L= c ntains numeric c$aracters' it returns a null string &((*. H. 1>L(L+13T;(....)*B)) @ >$is part determines t$e lengt$ 2 t$e >R?0SL?>Ed string' and 0PLs it t 8 i2 t$e string is null. >$e SNL c nstruct up t t$is p int .ill return 8 i2 sortb" $as nl! numeric c$aracters' and .ill return a p siti)e integer i2 t$ere are an! n n@numeric c$aracters. .

I. T'H7;),(....) @ C n)erts t$e number returned b! t$e LE0F>% int a )arc$ar. >$is needs t be d ne s t$at t$e DECODE .ill . r, c rrectl!. 9. D+7'D+(....* B *L#)D(sort$y*E)*sort$y) @ 0 . .e c me t t$e guts 2 t$is tec$ni6ue. >$is DECODE c$ec,s t see i2 t$e )alue is numeric nl! &a LE0F>% 2 (8(*' and i2 s ' returns t$e )alue 2 sortb" L#?Ded t G c$aracters. /2 t$e )alue $as an! alp$a c$aracters &LE0F>% 3 8* it Rust returns sortb" .it$ ut an! m di2icati n. 0 . .$! d .e .ant t L#?D t$e numeric )alues nl!J Because space &( (* s rts BEFORE t$e numbersM >$is all .s 2 r a decimal place b! decimal place c mparis n 2 t$e t. numbers. >$e (B( .ill return .it$ 7 leading blan,s' t$e (B8( .it$ :' ect. L$en t$e alp$abetical s rt is d ne' t$e )alues are c mpared' c$aracter b! c$aracter' and blan, s rts be2 re t$e numbers. /2 ! u selected t$e SNL c nstruct t$at ! u are s rting b!' ! u . uld see s met$ing li,eA
/012
/E1E!' DE!=DE&'=_!6()&7F1&1E7J'6&')(7/1('E&sortb#"'(123T8N .D0'"'('**"0**"'0'"1B(D&sortb#".*"s ortb#* 2 @)=G 7umber_/ort 3 =)DE) 4I DE!=DE&'=_!6()&7F1&1E7J'6&')(7/1('E&sortb#"'(123T8N .D0'"'('**"0**"'0'"1B(D&sortb#".*"s ortb#* T C

/=)'4I ;;;;;;;;;;;;;;;;;;;; 1 2 3 10 11 20 21

30

100

200 1000 1(4 ( 4444 1T rows selected<

The E 0 used in the L#)D is Must ar$itrary. 0f 0 had 15 di"it num$ers in the S',T(N co!umn* 0 wou!d use a -a!ue "reater than 15 so that any num$er wou!d sort correct!yTip #2D: <sin" a Data$ase %unction to Query $y a L'13 7o!umn. (Type: SQL)
%a)e ! u e)er tried t use LO0F c lumns in t$e L%ERE clause 2 ! ur SNL statementJ S met$ing li,e (L%ERE l ng_c lumn li,e (XSE?RC%X(J /2 s ' ! u ,n . t$is d es n t . r,M &= u get (OR?@884IHA inc nsistent datat!pes(* >$is tip s$ .s $ . ! u can use database 2uncti ns t a) id t$is limitati n' and 6uer! b! LO0F c lumns an!.a!. C nsider t$e 2 ll .ing tableA
1=7J_56E)E ========== 7ame 7ullK ;;;;;;;;;;;;;;;;;;; ;;;;;;;; 1=7J_9D 7=' 7E11 1=7J_DE/!

'#$e ;;;; 7EG4E) 1=7J

/2 ! u tried t select b! t$e l ng c lumn' ! u mig$t use s met$ing li,eA


/012 /E1E!' 3 @)=G 1on+_5here 2 56E)E lon+_desc like 'Q/E()!6Q'; 56E)E lon+_desc like 'Q/E()!6Q' 3 E))=) at line 2% =)(;00D32% inconsistent datat#$es

L$ic$ d es n t . r,M One .a! ar und t$is limitati n is t .rite a database 2uncti n (.rapper( 2 r t$e l ng c lumn. >$is 2uncti n . uld accept t$e #K 2 t$e table as an input parameter' and return t$e LO0F c lumn(s )alue' c n)erted t a P?RC%?RH. %ere is an eCample 2 suc$ a 2uncti nA

!)E('E =) )EB1(!E @E7!'9=7 vc_desc&$_9D 97 7EG4E)* )E'E)7 F()!6()2 9/ v_desc F()!6()2&2000*; v_1on+ 1=7J; 4EJ97 /E1E!' lon+_desc 97'= v_1on+ @)=G 1on+_where 56E)E lon+_id = $_9D; v_Desc %= /E4/')&v_1on+"1"2000*; )E'E)7&v_Desc*; E7D;

0 . ! u can use t$is 2uncti n in t$e .$ere clause instead 2 using t$e LO0F c lumn directl!. F r eCampleA
/012 /E1E!' lon+_id" lon+_desc 2 @)=G 1on+_5here 3 56E)E vc_desc&lon+_id* like 'Q/E()!6Q' T C 1=7J_9D 1=7J_DE/! ;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 2 (nother bunch of tePt to $lace into a lon+ value< /earch for /E()!62 somewhere T /earch for /E()!63 in here

= u are limited t searc$ing t$e 2irst H888 c$aracters 2 t$e l ng c lumn' but t$is is 2ten g d en ug$.

Tip #1L: Se!ectin" '1LN the "roup with the ma.imum Sum in a "roup =uery. (Type: SQL)
Let(s sa! ! u $a)e a select t$at is summing b! a ,e! )alue' and ! u .ant t nl! return t$e ,e! t$at $as t$e maCimum sum &n t ?LL 2 t$e r .s li,e a gr up b! .ill*. >$is tip .ill s$ . a SNL statement t$at d es t$is. C nsider t$e 2 ll .ing tables and dataA
/EG_B()E7' ;;;;;;;;;; 7ame 7ullK ;;;;;;;;;;;;;;;;;;; ;;;;;;;; /EG_9D /EG_7(GE /E1E!' 3 @)=G /um_Barent; /EG_9D /EG_7(GE ;;;;;;;;; ;;;;;;;;;; 1 =7E

'#$e ;;;; 7EG4E) F()!6()2&10*

2 3 T 8

'5= '6)EE @=E) @9FE

/EG_!691D ;;;;;;;;; 7ame 7ullK ;;;;;;;;;;;;;;;;;;; ;;;;;;;; /EG_9D 0'I /E1E!' 3 @)=G /um_!hild; /EG_9D 0'I ;;;;;;;;; ;;;;;;;;; 1 10 1 20 1 8 2 10 2 8 3 3 3 2 T 30 T 2 8 10

'#$e ;;;; 7EG4E) 7EG4E)

>$e 2 ll .ing simple FROU# B= select .ill return t$e sum 2 t$e N>= 2 r eac$ ,e! &SUM_0?ME*A
/E1E!' $<sum_name" sum&c<-t#* @)=G /um_Barent $" /um_!hild c 56E)E $<sum_id = c<sum_id J)=EB 4I $<sum_name C /EG_7(GE /EG&!<0'I* ;;;;;;;;;; ;;;;;;;;;; @9FE 10 @=E) 32 =7E 38 '6)EE 8 '5= 18

Let(s sa! ! u nl! .ant t return t$e r . .it$ t$e M?</MUM 6uantit! &SUM_0?ME;O0E*. > d t$is' ! u can add a %?P/0F predicate related t a sub@ 6uer!. >$e %?P/0F is needed t be able t use t$e SUM&c.6t!* in a predicate. ?n eCample 2 t$is 2 ll .sA
/E1E!' $<sum_name" sum&c<-t#* @)=G /um_Barent $" /um_!hild c 56E)E $<sum_id = c<sum_id J)=EB 4I $<sum_name 6(F97J /EG&c<-t#* = &/E1E!' G(H&/EG&c2<-t#** @)=G /um_!hild c2 J)=EB 4I c2<sum_id *

C /EG_7(GE /EG&!<0'I* ;;;;;;;;;; ;;;;;;;;;; =7E 38

L$ic$ is t$e desired result. One cauti n $ .e)er' i2 t$ere are m re t$an ne ,e! .it$ t$e maCimum sum' t$e 6uer! .ill return ?LL 2 t$em. F r instance' i2 t$e 2 ll .ing data is added t t$e SUM_C%/LD tableA
/EG_9D 0'I ;;;;;;;;; ;;;;;;;;; 3 30

>$e ab )e 6uer! .ill return t$e 2 ll .ing result.


/EG_7(GE /EG&!<0'I* ;;;;;;;;;; ;;;;;;;;;; =7E 38 '6)EE 38

>$is 6uer! is n t )er! e22icient $ .e)er. /2 ! u $a)e #L"SNL a)ailable t ! u' ! u can get t$e same result b! creating a curs r .it$ t$e 2irst 6uer!' adding an rder b! clause' and nl! 2etc$ing t$e 2irst r .. >$is .ill a) id t$e sec nd sum' .$ic$ .ill gi)e it better per2 rmance. %ere is an eCample 2 t$isA
set serverout$ut on DE!1()E !E)/=) sum_cur 9/ /E1E!' $<sum_name" sum&c<-t#* @)=G /um_Barent $" /um_!hild c 56E)E $<sum_id = c<sum_id J)=EB 4I $<sum_name =)DE) 4I sum&c<-t#* desc; /um7ame F()!6()2&10*; /um0t# 7EG4E); 4EJ97 =BE7 sum_cur; @E'!6 sum_cur 97'= /um7ame" /um0t#; !1=/E sum_cur; D4G/_=E'BE'<BE'_197E&'/EG_7(GE% 'WW/um7ameWW' /um&0t#*% 'WWto_char&/um0t#**; E7D; C /EG_7(GE% =7E /um&0t#*% 38 B1C/01 $rocedure successfull# com$leted<

/2 t$ere are m re t$an ne r . .it$ t$e same maCimum sum' t$is #L"SNL pr gram .ill nl! return t$e 2irst ne it enc unters. >$ere2 re' t$e pr gram s$ uld be eCpanded t $andle t$is b! eit$er returning multiple )alues' r rdering b! t$e ,e! als &.$ic$ .ill ensure c nsistanc! i2 n t$ing else*.

Tip #1E: <sin" Data$ase %unctions to +!iminate 'uter Coins. (Type: #LGSQL F SQL)

Outer R ins are )er! use2ul in SNL t return data 2r m 6ueries .$ere s me 2 t$e relati ns$ips are pti nal. % .e)er' t$ere are times .$en t$e uter R ins can cause s me pr blems. S metimes t$e! .ill ma,e t$e 6uer! run )er! sl .l!. >$ere is als a restricti n t$at ! u can nl! uter a table t O0E t$er table. /n t$ese cases' database 2uncti ns can be used t eliminate t$e need 2 r t$e uter R ins. >$is tip .ill detail $ . t d t$is 2 r t$e 2 ll .ing t. eCamplesA

Eliminati n 2 Multiple Outer V ins in ?RC usage. ?ll .ing (Outer V in( t Multiple >ables.

7onsider the fo!!owin" data mode!:

?ssuming t$e data l ,s li,eA

=E'E)_E19G ========== B: :'IBE :EI1 :EI2 :EI3 ;;;;;;;;; ;;;;; ;;;;;;;;; ;;;;;;;;; ;;;;;;;;; 1 1 1 2 1 2 3 1 3 8 2 1 N 2 2 2 3 D 10 3 1 11 3 2 1==:EB1 ======= :EI1 1(4E11 1(4E13 ;;;;;;;;; ;;;;;;;;;; ;;;;;;;;;; 1 1;=7E 1;'6)EE 2 2;=7E 2;'6)EE 3 3;=7E 3;'6)EE =E'E)_E19G_!1D ============== B: :EIT !)=//_1(4E1 ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;;;;;;; 1 1 1T;2 1 2 1T;1 1 3 2 1 2 2 2 3 3 1 3 2 3 3 1==:EB2 ======= :EI2 1(4E12 ;;;;;;;;; ;;;;;;;;;; 1 1;'5= 2 2;'5= 3 3;'5= 1==:EB3 ======= :EI3 ;;;;;;;;; 1 2 3

!)=//_1(4E1 =========== :EI1

:EIT

;;;;;;;;; ;;;;;;;;; 1 3 2 1 11;1 H 11;3 H

>$is data m del .ill be used t illustrate b t$ eCamples 2 using a database 2uncti n t eliminate uter R insA +!imination of ?u!tip!e 'uter Coins in ),7 usa"e. Let(s sa! ! u need a SNL select statement t implement t$e ab )e arc &2r m OU>ER_EL/M t LOOKU#B'H'I*. F r eCample' ! u .ant t displa! nl! O0E 2 t$e

labels &eit$er L?BELB' L?BELH' r L?BELI* depending n t$e )alue 2 t$e KE=_>=#E c lumn. >$e 2 ll .ing SNL statement c uld be used t d t$isA
C3 ti$1.-1<s-l 3C /E1E!' oe<$k" DE!=DE&kt#$e"'1'"l1<label1"'2'"l2<label2"'3'"l3<label3* 1abel @)=G outer_elim oe" looku$1 l1" looku$2 l2" looku$3 l3 56E)E oe<ke#1 = l1<ke#1 &S* (7D oe<ke#2 = l2<ke#2 &S* (7D oe<ke#3 = l3<ke#3 &S* =)DE) 4I DE!=DE&kt#$e"'1'"l1<label1"'2'"l2<label2"'3'"l3<label3* C

>$is SNL statement .ill returnA


B: ;;;;;;;;; 1 10 8 2 11 N 3 D 1(4E1 ;;;;;;;;;; 1;=7E 1;'6)EE 1;'5= 2;=7E 2;'6)EE 2;'5= 3;=7E 3;'5=

% .e)er' s metimes $a)ing multiple uter R ins can cause per2 rmance pr blems. Especiall! .$en t$ere are man! tables R ined t get$er. ? database 2uncti n can be used t get t$e same 6uer! results .it$ ut using an uter R in. > d t$is' 2irst create t$e 2 ll .ing database 2uncti nA
C3 ti$1.fun<s-l 3C !)E('E =) )EB1(!E @E7!'9=7 comb_label&$'#$e 97 F()!6()2 "$:e#1 97 7EG4E)"$:e#2 97 7EG4E)"$:e#3 97 7EG4E)* )E'E)7 F()!6()2 9/ v)eturn F()!6()2&10*; 4EJ97 9@ $'#$e = '1' '6E7 4EJ97 /E1E!' label1 97'= v)eturn @)=G looku$1 56E)E ke#1 = $:e#1; E7D; E1/9@ $'#$e = '2' '6E7 4EJ97 /E1E!' label2 97'= v)eturn @)=G looku$2 56E)E ke#2 = $:e#2; E7D; E1/9@ $'#$e = '3' '6E7

4EJ97 /E1E!' label3 97'= v)eturn @)=G looku$3 56E)E ke#3 = $:e#3; E7D; E7D 9@; )E'E)7&v)eturn*; E7D; C

>$is 2uncti n can be used in t$e 2 ll .ing SNL t return t$e same results as t$e 2irst 6uer!' but .it$ ut an! uter R insA
C3 ti$1.-2<s-l 3C column label format a12; /E1E!' oe<$k" comb_label&kt#$e"ke#1"ke#2"ke#3* 1abel @)=G outer_elim oe =)DE) 4I comb_label&kt#$e"ke#1"ke#2"ke#3* C

<sin" a Data$ase %unction to a!!ow 'uter Coin to ?u!tip!e Ta$!es. >$ere are times .$en ! u reall! .ant t uter R in ne table t t. di22erent tables. L$en t$is $appens' t$e restricti n can be 2rustrating. /n t$e ab )e data m del' ! u mig$t .ant t d an uter R in 2r m t$e CROSS_LOOKU# table t b t$ t$e OU>ER_EL/M and OU>ER_EL/M_CLD tables. > d t$is' ! u mig$t tr! t use a SNL statement li,eA
C3 ti$1.-3<s-l 3C /E1E!' oe<$k" l1<label1" lT<labelT" cl<cross_label @)=G outer_elim oe" outer_elim_cld cld" looku$1 l1" looku$T lT "cross_looku$ cl 56E)E oe<$k = cld<$k (7D oe<ke#1 = l1<ke#1 (7D cld<ke#T = lT<ke#T (7D cl<ke#1 &S* = oe<ke#1 (7D cl<ke#T &S* = cld<ke#T C

L$ic$ .ill cause t$e 2 ll .ing err rA


(7D cl<ke#1 &S* = oe<ke#1 3 E))=) at line % =)(;01T1 % a table ma# be outer ?oined to at most one other table

>$is err r ccurs because Oracle .ill n t let ! u $a)e an uter R in 2r m O0E table t t. D/FFERE0> tables. % .e)er' ! u C?0 get t$e desired e22ect using a database 2uncti n. /2 t$e 2 ll .ing 2uncti n is createdA
C3 ti$1.fn2<s-l 3C !)E('E =) )EB1(!E @E7!'9=7 cross_label&$:e#1 97 7EG4E)"$:e#T 97 7EG4E)* )E'E)7 F()!6()2 9/ v)eturn F()!6()2&18*; 4EJ97

/E1E!' cross_label 97'= v)eturn @)=G cross_looku$ 56E)E ke#1 = $:e#1 (7D ke#T = $:e#T; )E'E)7&v)eturn*; EH!EB'9=7 56E7 7=_D('(_@=E7D '6E7 )E'E)7&''*; E7D; C

t$en t$e 2 ll .ing SNL can be used t return t$e ( uter R in( )alues 2r m t$e CROSS_LOOKU#A

C3 ti$1.-T<s-l 3C column cross_label format a18 /E1E!' oe<$k" l1<label1" lT<labelT" cross_label&oe<ke#1" cld<ke#T* cross_label @)=G outer_elim oe" outer_elim_cld cld" looku$1 l1" looku$T lT 56E)E oe<$k = cld<$k (7D oe<ke#1 = l1<ke#1 (7D cld<ke#T = lT<ke#T C B: ;;;;;;;;; 1 1 1 2 2 2 3 3 3 1(4E11 ;;;;;;;;;; 1;=7E 1;=7E 1;=7E 2;=7E 2;=7E 2;=7E 3;=7E 3;=7E 3;=7E 1(4E1T ;;;;;;;;;; 1;@=E) 2;@=E) 3;@=E) 1;@=E) 2;@=E) 3;@=E) 1;@=E) 2;@=E) 3;@=E) !)=//_1(4E1 ;;;;;;;;;;;;;;; 11;1 H 1T;2

>$is select .ill returnA

11;3 H 1T;1

St red pr cedures and 2uncti ns are )er! p .er2ul. >$ese are Rust s me simple eCamples t get pe ple t$in,ing ab ut s me p ssible .a!s 2 using t$em.

Tip #1K: 7a!cu!atin" a runnin" tota! with SQL on!y. (Type: SQL)
>$ere are applicati ns .$ere t$e running t tal 2 r a series 2 numbers needs t be calculated and displa!ed. L$ile t$is mig$t be n rmall! c nsidered s met$ing t$at . uld be d ne .it$ IFL pr gramming tec$ni6ues' it /S p ssible t calculate and 6uer! a running t tal .it$ SNL nl!. %irst* we need to ha-e a /ey co!umn to order the =uery $y Let(s assume .e $a)e a table .it$ a numeric c lumn t$at .e .ant. Le als need t $a)e a c lumn t rder t$e displa! b!' r a running t tal d es n t ma,e senseM Man! times t$is .ill be a date c lumn. >$is c uld als be t$e #K 2 t$e table' r a single r multiple c lumn UK. / .ill use a date c lumn in t$e 2 ll .ing eCample. C nsider t$e tableA

/012 desc )un_'otal 7ame 7ullK ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;; )E7_D('E )E7_F(1EE /012 select 3 from )un_'otal; )E7_D('E )E7_F(1EE ;;;;;;;;; ;;;;;;;;; 02;(B);D 10 03;(B);D 8 0T;(B);D 20 08;(B);D 18 0N;(B);D T8 0 ;(B);D 12 0.;(B);D 3 0D;(B);D D 10;(B);D 23 11;(B);D 1D 12;(B);D 10 11 rows selected< '#$e ;;;; D('E 7EG4E)

) se!f2Moin is needed to perform the correct sum /n rder t pr duce t$e running t tal' a sel2@R in is needed t sum all 2 t$e )alues 2 t$e table less t$an r e6ual t eac$ r .. >$e 6uer! is gr uped b! t$e rec rds in t$e (dri)ing( table in t$e R in' and rdered b! t$e run_dateA
/E1E!' )unnin+_'otal @)=G 56E)E J)=EB =)DE) C r1<run_date" r1<run_value" sum&r2<run_value* )un_'otal r1" )un_'otal r2 r2<run_date R= r1<run_date 4I r1<run_date" r1<run_value 4I r1<run_date

>$is 6uer! .ill pr duce t$e 2 ll .ing utputA


)E7_D('E )E7_F(1EE )E7797J_'='(1 ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;;;;; 02;(B);D 10 10 03;(B);D 8 18 0T;(B);D 20 38 08;(B);D 18 80 0N;(B);D T8 D8 0 ;(B);D 12 10 0.;(B);D 3 1TT 0D;(B);D D 183 10;(B);D 23 1 N 11;(B);D 1D 1D8 12;(B);D 10 208

11 rows selected<

Tip #J: <sin" SQL on!y to Determine the (usiness Days (etween Two Dates. (Type: SQL)
>$ere are man! times in rep rts r in calculati ns 2 r 2 rms .$ere t$e number 2 business da!s bet.een t. dates needs t be determined. %ere is a met$ d 2 r caluculating t$is &eCcluding $ lida!s* using SNL nl!. >$e 2 ll .ing SNL script s$ .s an alg r!t$m t$at uses t$e standard Oracle date 2uncti ns t calculate t$e number 2 business da!s bet.een t dates. >$is met$ d cann t eCclude $ lida!s & b)i usl!*' $ .e)er t$ere are man! times t$at Rust t$e standard business da!s is use2ul. / am sure t$ere are t$er alg r!t$ms t$at c uld be used' $ .e)er t$is ne $as . r,ed 2 r me. >$e alg r!t$m is described bel . in t$e c mments 2 t$e script.
C 33333333333333333333333333333333333333333333333333333333333 333333C C3 (n ePam$le of business da#s calculation in /01 3C C3 3C C3 'he al+or#thm is% 3C C3 3C C3 1* 'ake the absolute difference between the dates 3C C3 to_date&'Otodate'* ; to_date&'Ofrdate'* 3C C3 2* /ubtract the weekends &number of weeks in the ran+e 3C C3 ')E7!&to_date&'Otodate'*"'D'* = 1st da# of week that 3C C3 end of $eriod is in 3C C3 ')E7!&to_date&'Ofrdate'*"'D'* = 1ast da# of week that 3C C3 start of $eriod is in 3C C3 /o subtractin+ these two +ives the number of da#s 3C C3 between the two dates but includin+ all of the da#s in 3C C3 the weeks that the dates start and end in< 5hen this 3C C3 number is divided b# it +ives the number of weeks< 3C C3 Gulti$l#in+ b# 2 +ives the number of weekend da#s< 3C

C3 3* /ubtract 1 da# if the endin+ date is on a saturda# 3C C3 DE!=DE&to_char&to_date&'Otodate'*"'D'*" ";1"0* 3C C3 ;;2 9f the da# of the week is saturda# & *" returns ;1 3C C3 T* /ubtract 1 da# if the start date is on a sunda# 3C C3 DE!=DE&to_char&to_date&'Ofrdate'*"'D'*"1";1* 3C C3 ;;2 9f the da# of the week is sunda# &1*" returns 1 3C C3 8* (dd one da# to make the ran+e inclusive &'he '1 S ' * 3C C 3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;3C C3 (uthor% :enneth (tkins &:en>arrowsent<com* 3C C3 htt$%CCwww<ol#wa<netCkatkinsCorati$ 3C C 33333333333333333333333333333333333333333333333333333333333 333333C define frdate = 'O1' define todate = 'O2' set verif# off select 'Ofrdate' @rom_Date "'Otodate' 'o_Date" 1 S to_date&'Otodate'* ; to_date&'Ofrdate'* ; &&')E7!&to_date&'Otodate'*"'D'* ; ')E7!&to_date&'Ofrdate'*"'D'**C *32 S DE!=DE&to_char&to_date&'Otodate'*"'D'*" ";1"0* S DE!=DE&to_char&to_date&'Ofrdate'*"'D'*"1";1"0* 4usiness_Da#s from dual C

%ere is an eCample 2 running t$e scriptA


/012 >busda#s 01;(EJ;DN 18;(EJ;DN @)=G_D('E '=_D('E 4E/97E//_D(I/ ;;;;;;;;; ;;;;;;;;; ;;;;;;;;;;;;; 01;(EJ;DN 18;(EJ;DN 11 1 row selected<

>$is same alg r!t$m can als be put int a st red 2uncti nA
!)E('E =) )EB1(!E @E7!'9=7 business_da#s&$_from_date 97 D('E" $_to_date 97 D('E* )E'E)7 7EG4E) 9/ busda#s 7EG4E);

4EJ97 C 33333333333333333333333333333333333333333333333333333333333 333333C C3 4E/97E//_D(I/ ; Database @unction to !alculate number of 3C C3 business da#s between two dates 3C C 3;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;3C C3 (uthor% :enneth (tkins &:en>arrowsent<com* 3C C3 htt$%CCwww<ol#wa<netCkatkinsCorati$ 3C C 33333333333333333333333333333333333333333333333333333333333 333333C ;; Jet the absolute date ran+e busda#s %= ')E7!&$_to_date* ; ')E7!&$_from_date* ;; 7ow subtract the weekends ;; this statement rounds the ran+e to whole weeks &usin+ ;; ')E7! and determines the number of da#s in the ran+e< ;; then it divides b# to +et the number of weeks" and ;; multi$lies b# 2 to +et the number of weekend da#s< ; &&')E7!&$_to_date"'D'*; ')E7!&to_date&$_from_date*"'D'**C *32 ;; (dd one to make the ran+e inclusive S 1; C3 (d?ust for endin+ date on a saturda# 3C 9@ '=_!6()&$_to_date"'D'* = ' ' '6E7 busda#s %= busda#s ; 1; E7D 9@; C3 (d?ust for startin+ date on a sunda# 3C 9@ '=_!6()&$_from_date"'D'* = '1' '6E7 busda#s %= busda#s ; 1; E7D 9@; )E'E)7&busda#s*; E7D; C show errors;

%ere is an eCample 2 calling t$is database 2uncti nA


/012 select business_da#s&'01;(EJ;DN'"'18;(EJ;DN'* from dual; 4E/97E//_D(I/&'01;(EJ;DN'"'18;(EJ;DN'* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

11 1 row selected<

O2 c urse in a st red 2uncti n' ! u c uld add c de t substract $ lida!s als . #er$aps s met$ing li,eA
/E1E!' !=E7'&3* 97'= n6olida#s @)=G 6olida#_'able 56E)E holida#_date 4E'5EE7 $_from_date (7D $_to_date;

>$en substract n% lida!s 2r m ! ur business_da!s )ariable be2 re returning.

Tip #D: Script to List #LGSQL +rrors F Line 1um$ers (Type: #LGSQL)
L$en #L"SNL st red pac,ages' pr cedures' r 2uncti ns are l aded int t$e database and c mpiled &using CRE?>E OR RE#L?CE* an! s!ntaC err rs in t$e c de can be listed using t$e (S%OL ERROR( c mmand. % .e)er' man! times t$ese messages are cr!ptic' and t$e line numbers speci2ied d n t c rresp nd t t$e line number in t$e SNL 2ile used t l ad t$e c de &because blan, lines and c mments be2 re t$e (CRE?>E( statement are n t l aded. F r large pr cedures' t$is can be )er! 2rustrating. >$is $int .ill s$ . $ . a SNL script can be used t list t$e err rs al ng .it$ t$e actual s urce lines' indicating t$e line .it$ t$e err r &.it$ a (@@3(*. >$e Oracle )ie.s USER_SOURCE' and USER_ERRORS & r ?LL_SOURCE and ?LL_ERRORS* can be used t list t$e s urce 2 t$e pr gram t$e .a! t$e c mpiler sees it &.it$ ut blan, lines and leading c mments*. USER_ERRORS is t$e )ie. used b! S%OL ERRORS t displa! t$e err rs 2 r a #L"SNL pr gram. >$e 2 ll .ing select statement .ill R in t$ese t. )ie.s t list all 2 t$e c mpile err rs' and p int ut speci2icall! .$ic$ line t$e err rs are n &using (@@3(*.
C 33333333333333333333333333333333333333333333333333333333333 333333333333333333C C3 listerr<s-l ; 1ists errors and source for errors for B1C/01 $ro+rams< 3C C3 3C C3 Barameter% O1 = 'he name of the B)=!EDE)E" B(!:(JE" or @E7!'9=7 3C C3 3C C3 (uthor% :en (tkins &:en>arrowsent<com* 3C C3 htt$%CCwww<arrowsent<comCorati$ 3C C3 3C C3 'his scri$t uses the 'E/E)' views< 9t could easil# be modified to use 3C C3 the '(11' views b# chan+in+ the 'user_' to 'all_' in the view names 3C C3 and b# addin+ an owner as another $arameter 3C

C 33333333333333333333333333333333333333333333333333333333333 333333333333333333C set verif# off define ob?_name = 'O1'; column outline format a108 headin+ 'Error 1istin+'; break on err_tePt ski$ 2; set linesiMe 108; set $a+esiMe 0; set $ause off; s$ool listerr /E1E!' decode&to_char&us<line*" to_char&ue<line; *"ue<tePt" to_char&ue<line;N*"''" to_char&ue<lineSN*"''" to_char&ue<line* "' ;;2 'WW to_char&us<line"'DDDD0'* WW' 'WWus<tePt "' 'WW to_char&us<line"'DDDD0'* WW' 'WWus<tePt* outline from user_source us" user_errors ue where us<name = 'Oob?_name' and us<line between &ue<line; * and &ue<lineSN* and us<name = ue<name and us<t#$e = ue<t#$e ;; 'his $redicate is $ut here to elminate this useless fallout error and ue<tePt L= 'B1C/01% /tatement i+nored' C s$ool off set $ause on; set $a+esiMe 22;

%ere is an eCample 2 using t$e script. ? SNL script called ($intIpc,( $as t$e de2initi n 2 a pac,age .it$ a 2e. err rs. >$e ab )e SNL script is called (listerr.s6l(A
!(/E%:E77E(2 >hint3$ck Backa+e created< 7o errors< 5arnin+% Backa+e 4od# created with com$ilation errors< Errors for B(!:(JE 4=DI B(!:_59'6_E))=)% 2 197EC!=1 E))=) ;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;; C1 B1C/01% /tatement i+nored DC1 B1/;00201% identifier 'FF()' must be declared DC1 B1C/01% /tatement i+nored 1DC11 B1/;00201% identifier '7=_D'(_@=E7D' must be declared

!(/E%:E77E(2 >listerr B(!:_59'6_E))=) B1/;00201% identifier 'FF()' must be declared T vE/E) F()!6()2&30*; 8 v'est F()!6()2&30*; N n'est 7EG4E); 4EJ97 . C3 !omment line 3C ;;2 D vFar %= to_num&'12'*; 10 C3 !omment line 3C 11 C3 'he nePt line has an error &missin+ semicolon* 3C 12 nFar %= 1; 13 C3 more comments 3C 1T nFar %= 3; B1/;00201% identifier '7=_D'(_@=E7D' must be declared 1T 18 1N 1 1. 1D 20 21 22 23 2T nFar %= 3; 4EJ97 /E1E!' user 97'= vE/E) @)=G dual; EH!EB'9=7 56E7 7=_D'(_@=E7D '6E7 7E11; E7D; 4EJ97 /E1E!' user 97'= vE/E) @)=G dual;

;;2

2. rows selected<

>$e 2 ll .ing select can als be used t Rust list t$e s urce 2 r a st red pr cedure' pac,age r 2uncti n' putting in t$e line numbers t$e c mpiler usesA
C 33333333333333333333333333333333333333333333333333333333333 333333333333333333C C3 listsource<s-l ; 1ists source for B1C/01 $ro+rams< 3C C3 3C C3 Barameter% O1 = 'he name of the B)=!EDE)E" B(!:(JE" or @E7!'9=7 3C C3 3C C3 (uthor% :en (tkins &:en>arrowsent<com* 3C C3 htt$%CCwww<arrowsent<comCorati$ 3C C3 3C C 33333333333333333333333333333333333333333333333333333333333 333333333333333333C define ob?_name = 'O1'; column tePt format a T headin+ '/ource 1istin+';

column line format DDDD headin+ '1ine'; set verif# off set linesiMe .0; set $a+esiMe 0; set $ause off; s$ool Oob?_name /E1E!' us<line" us<tePt from user_source us where us<name = 'Oob?_name' order b# t#$e" line C s$ool off set $ause on; set $a+esiMe 22;

Tip #2: Determinin" 0nstance 1ame from SQL. (Type: SQL5#!us)


S metimes it is use2ul t be able t get t$e name 2 t$e current instance 2r m .it$in SNL+#lus r an t$er de)el pment t l &suc$ as Oracle+F rms r Oracle+Rep rts*. %ere is a simple SNL statement t$at can be used t get t$e instance name. ?ls presented is an eCample 2 using t$e SNL statement t set t$e SNL pr mpt t t$e instance name. >$ere is an internal racle )ie. called (P-#?R?ME>ER( .$ic$ $ lds t$e )alues 2 man! parameters t$at t$e database uses. One 2 t$ese parameters is called (DB_0?ME(. >$is parameter $ lds t$e name 2 t$e database &,ind 2 ma,es sense' d esn(t itJ*. >$e P-#?R?ME>ER )ie. l ,s li,eA
desc v$$arameter 7ame 7ullK ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;; 7EG 7(GE 'IBE F(1EE 9/DE@(E1' '#$e ;;;; 7EG4E) F()!6()2&NT* 7EG4E) F()!6()2&812* F()!6()2&D*

>$e 2 ll .ing SNL statement .ill return t$e database name 2r m t$is )ie.A
/E1E!' EBBE)&value* @)=G F$Barameter 56E)E EBBE)&name* = 'D4_7(GE';

? SNL statement li,e t$is can be used t replace t$e standard s6l pr mpt &(SNL3(* .it$ t$e instance name. >$is can be use2ul i2 ! u are accessing man! di22erent instances in SNL+#lus. /t $as sa)ed me 2r m messing up data in t$e .r ng instance man! times &li,e deleting data 2r m t$e pr ducti n instance instead 2 test*. ?dd t$e 2 ll .ing SNL t ! ur l gin.s6l 2ileA
rem !reate _D4_7ame variable for +eneral use< set termout off column u$$er&F(1EE* new_value _D4_7(GE; select u$$er&value*" from v$$arameter where u$$er&name* = 'D4_7(GE';

rem rem But /J( 7ame in s-l $rom$t set /01B)=GB' 'O_D4_7(GE<2 ' rem rem 9f $ause is on" sa# somethin+ when needed< set $ause '2 ' rem clear breaks set termout on set $ause on set feedback on

/2 t$e instance name is s met$ing li,e (>ES>/0S>( t$en t$e ab )e SNL in t$e l gin.s6l 2ile .ill c$ange t$e pr mpt t A >ES>/0S>3

&en )t/ins 'rac!e Data$ase Tip of the wee/.


Tip #1: 7onditiona! !oadin" of #LGSQL code. (Type: #LGSQL)
Because 2 )ar!ing re6uirements in distributed databases r multi@ rganizati nal c mpanies' t$ere is s metimes a need t impliment a #L"SNL pac,age r pr cedure .it$ slig$t di22erences in t$e c de .$en installed n separate instances r sc$emas. Nuite 2ten t$is .as d ne b! maintaining a )ersi n 2 t$e c de 2 r eac$ site. % .e)er' t$e 2 ll .ing tec$ni6ue can be used t ,eep all 2 t$e s urce in ne 2ile' and $a)e t$e di22erences implemented .$en t$e c de is installed. ? SNL+#lus )ariable &W &ampersand* )ariable* can be d!namicall! l aded using t$e (ne._)alue( clause 2 t$e SNL+#lus c lumn c mmand. >$is )ariable can be d!namicall! based up n t$e sc$ema' r instance' r data in an eCisting table. >$ese )ariables are scanned and replaced in t$e SNL c de be2 re t$e c de is installed r c mpiled. >$ere2 re t$e (W( )ariables can be used t c$ange t$e c de t$at is installed. Bel . is an eCample 2 using t$is tec$ni6ue t (c mment ut( a call t a pr cedure 2 r ne sc$ema nl!A
set $ause off column comvar new_value comment_var /E1E!' DE!=DE&user"'!5F7D'"';;'"''* comvar @)=G DE(1 C !)E('E =) )EB1(!E B(!:(JE test_$acka+e (/ B)=!EDE)E main; B)=!EDE)E conditional_$roc&$arm1 97 F()!6()2*; E7D test_$acka+e; C !)E('E =) )EB1(!E B(!:(JE 4=DI test_$acka+e (/ B)=!EDE)E main 9/ 4EJ97 D4G/_=E'BE'<BE'_197E&'4e+innin+ of main $rocedure<'*; Ocomment_var conditional_$roc&''est'*; D4G/_=E'BE'<BE'_197E&'End of main $rocedure<'*; E7D main;

B)=!EDE)E conditional_$roc&$arm1 97 F()!6()2* 9/ 4EJ97 D4G/_=E'BE'<BE'_197E&'!onditional_$roc called with $arm1='WW$arm1*; E7D conditional_$roc; E7D test_$acka+e; C

/2 t$e ab )e pac,age is installed in t$e (USERB( sc$ema' t$en t$e (c nditi nal_pr c( pr cedure .ill be c mmented ut' and t$ere2 re .ill n t run. F r all t$er sc$emas' t$e pr cedure .ill 0O> be c mmented ut' and .ill run. S me t$er .a!s t use t$is tec$ni6ueA Ma,e t$e parameter t a pr cedure c nditi nal. F r eCample' t$e call t t$e (c nditi nal_pr c( pr cedure ab )e c uld be c$anged t A
column userval new_value user_val; /E1E!' user userval @)=G DE(1; < < conditional_$roc&'Ouser_val'*;

Ma,e t$e name 2 a pr cedure c nditi nal. >$e 2 ll .ing lines c uld be used t call a pr cedure t$at is called _setup' .$ere is t$e name 2 t$e racle instance.
column $rocname new_value $roc_name; /E1E!' valueWW'_setu$' $rocname @)=G v$$arameter 56E)E u$$er&name* = 'D4_7(GE'; < < O$roc_name;

Tip #5K: #rocedure to show a!! of an 'rac!e #orta! session -aria$!e s attri$utes (Type: 'rac!e on the 6e$)
Oracle L$en ! u are using Oracle # rtal sessi n )ariables' it is use2ul t be able t see t$e )alues 2 it(s attributes .$en ! u are testing and debugging an applicati n t$at uses t$em. >$is tip is a #L"SNL pac,age t$at can be run 2r m t$e br .ser t list all 2 a sessi n )ariable(s attributes t t$e br .ser.

) #LGSQL #rocedure to do the Mo$


%ere is a simple #L"SNL pr cedure t$at accepts t$e d main and sub@d main 2 t$e Oracle # rtal sessi n and prints t$e names and )alues 2 all 2 t$e sessi ns attributes t t$e br .ser.
!)E('E =) )EB1(!E B)=!EDE)E show_session&$_domain 97 F()!6()2" $_subdomain 97 F()!6()2* 9/

C 33333333333333333333333333333333333333333333333333333333333 33333333333333333333333333333C C3 /6=5_/E//9=7 ; /hows the names and values of all of the attributes of an =racle 3C C3 Bortal session variable< 3C C3 3C C3 Barameters% $_domain = 'he name of the session domain< 3C C3 $_subdomain = 'he name of the session subdomain< 3C C3 3C C3 5ritten b# ken atkins &ken>arrowsent<com*< !o$#write 2001" all ri+hts reserved< 3C C3 Iou ma# use this scri$t for an# $ur$ose as lon+ as #ou do not include it in an# 3C C3 commercial software for sale< 9f #ou make chan+es to im$rove the scri$t" $lease 3C C3 send them to me so 9 can make them available for other users< 3C C3 3C C3 !heck out m# oracle ti$ site at% htt$%CCwww<arrowset<comCorati$s 3C C3 3C C3 Fers Date 4# !han+e 6istor# 3C C3 ;;;;; ;;;;;;;;; ;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 3C C3 1<0 1T;@E4;01 :en (tkins 5ritten< 3C C 33333333333333333333333333333333333333333333333333333333333 33333333333333333333333333333C v_session $ortal30<wwsto_a$i_session; v_elements $ortal30<wwsto_session_elements; v_num_attr 97'EJE); v_element $ortal30<wwsto_session_element; 4EJ97 v_session %= wwsto_a$i_session<load_session&$_domain" $_subdomain*; v_elements %= v_session<,_element_data,; v_num_attr %= v_elements<count; ht$<$&'/ession attributes for%R4)2R4)2R'(41E 4=)DE)=02R')2R'D2D=G(97%RC'D2R'D2R42'WW $_domainWW'RC42RC'D2RC')2'*; ht$<$&'R')2R'D2/E4;D=G(97%RC'D2R'D2R42'WW $_subdomainWW'RC42RC'D2RC')2RC'(41E2'*; ht$<br; ht$<$&'7umber of (ttributes% 'WWto_char&v_num_attr**; ht$<br; ht$<br;

ht$<$&'R'(41E 4=)DE)=,0,2'*; @=) i 97 1<<v_num_attr 1==B ht$<$&'R')2R'D2'WW v_elements&i*<nameWW'RC'D2R'D2=RC'D2R'D2'WW v_elements&i*<varchar2_dataWW'RC'D2RC')2'*; E7D 1==B; ht$<$&'RC'(41E2'*; E7D; C show errors

= u can d .nl ad t$e script b! clic,ing $ere.

0nsta!!ation
> install t$e pr cedure' d t$e 2 ll .ingA B. /nstall t$e pr cedure eit$er in t$e #OR>?LI8 sc$ema' r a sc$ema t$at $as access t t$e #OR>?LI8 pr grams &2 r instance a pr )ider sc$ema*. H. Frant eCecute n t$e pr cedure t t$e p rtalI8 public sc$ema &usuall! #OR>?LI8_#UBL/C*. F r instanceA
3< J)(7' EHE!E'E =7 show_session '= $ortal30_$ublic;

<sin" the procedure


> use t$e pr cedure' simpl! pen a separate na)igat r .ind . &a2ter ! u $a)e run t$e applicati n .$ic$ $as set s me sessi n attributes*' and t!pe a call t t$e pr cedure int t$e URL 2ield. F r eCample' t$e 2 ll .ing URL .ill call t$e pr cedure n a l cal install 2 p rtalI8 .it$ t$e standard dadA $ttpA""l cal$ st"pls"p rtalI8"p rtalI8.s$ ._sessi nJ p_d main;CO0>E<>Wp_subd main;SESS_CR> >$is call assumes t$e pr cedure is installed in t$e #OR>?LI8 sc$ema. /2 it is installed in an t$er sc$ema' replace t$e Kp rtalI8K immediatel! be2 re Ks$ ._sessi nK .it$ t$e name 2 t$e sc$ema in .$ic$ it is installed. Replace Kl cal$ st"pls"p rtalI8K .it$ t$e appr priate dad@pat$ 2 r ! ur installati n. >$e )alue 2 r #$o%a&n and #sub$o%a&n are set t t$e d main and subd main 2 t$e sessi n )ariable ! u .ant t see. %ere is an eCample 2 t$e utputA

Summary:

% . d / implemen

Man! 2 rm implem c n2usi n and be an Description:

>$is tip details $ .

F r t$e c mplete tip

F r m re 2 Ken(s O Summary:

Description:

Summary:

Description:

Summary:

Description:

Summary: Description:

Summary: Description: Summary: Description:

Summary:

Description:

Summary: Description:

Summary: Description:

Summary: Description:

Summary:

Description:

Summary: Description:

Summary:

Description:

Summary: Description:

Summary:

Description:

Summary:

Description:

Summary: Description:

Summary:

Description:

Summary: Description: Summary: Description:

Summary:

% . t determine t$e time and

>$ere is in2 rmati n a)ailable in2 rmat in is n t in a c n)eni 2 und it t be $andier t$an l

/E1E!' '=_D('E&F(1EE" @)=G F$97/'(7!E 56E)E ke# = '/'()'EB Description:

/E1E!' ')E7!&F(1EEC&N timeZ @)=G F$97/'(7!E 56E)E :EI = '/'()'EB

>$is in2 rmati n applies t )er

>$is .ill n t . r, 2 r Oracle G

select to_char&startu from v$instance Summary:

% . d / l ad a large &m re t$an 9K* %

> l ad a large %>ML 2ile int a )arc$a split t$e data int multiple r .s be2 re

Description: >$en ! u . uld als need a pr gram t .n. >$e t$er pti n is t use t$e LO0 datat!pe. >$is can $andle l ng data t!pe restricti ns .it$ t$e LO0F datat!pe Summary:

% . d / imp rt data 2r m an eCcel

tableJ

>. .a!s / can t$in, 2 t imp rt d int an Oracle tableA

B. Use an ODBC c nnecti n 2r m E spreads$eet int table. Description:

H. ECp rt data 2r m ECcel spreads$e t l ad t$e table 2r m t$e 2lat 2ile.

Eit$er .a! is a)ailable' $ .e)er' O s6l+net $as t be set up n t$e pc. S but it is usuall! 2aster t l ad an ent rat$er t$an insert r . b! r . )ia OD Summary:

Use 0LS_U##E 2 case@insensit

>$e 0LS_U##E .ant t create a ! ur searc$ 6ue t$e data c uld c case. > useA

create inde &71/_EBBE)&

Description:

select 3 fr 71/_EBBE)&c

>$is is muc$ 2a

create inde table_#&col

select 3 fr u$$er&col_1

..because in t$e alread! c n)ert and in t$e sec n c n)ersi n as it

Summary:

G.B.5 ECp rt and it(s parameters b

/ $appened n a pr blem regardin parameter 2iles in G.B.5. / $a)e t$e 2ileA userid;sc tt"tiger r .s;n indeCes;n grants;n 2ile;temp_inside.dmp tables;&userB.>able_C*

/n 7.I.I i2 / run t$e 2 ll .ing stat eCp par2ile;temp.par 2ile;temp_

t$e 2ile .ritten t is temp_ utside

/n G.B.5 i2 / run t$e same statemen temp_inside.dmp. Description:

?cc rding t Oracle(s G.B.5 d cum

(= u can use a c mbinati n 2 t$e pti ns. >$at is' ! u can list para parameters 2ile and n t$e c mma can speci2! t$e same parameter in p siti n 2 t$e #?RF/LE parame parameters n t$e c mmand line parameters )erride t$ers. F r eC parameters 2ile params.dat c ntain /0DE<ES;= and ECp rt is in) , 2 ll .ing lineA

eCp s!stem"manager #?RF/LE;p /0DE<ES;0

/n t$is case' because /0DE<ES;0 #?RF/LE;params.dat' /0DE<ES )alue 2 t$e /0DE<ES parameter Clearl! t$is is n t true. Be.are 2 Summary: Description:

% . muc$ KtrueK data is in a ta bl c,sJ

0eed t ,n . $ . muc$ data i table' / d n(t mean reser)ed spa

2r m dba_eCtents' / mean real a in s met$ing use2ul li,e bl c,s F r )7.C DBsA

select c unt&distinct substr&r .id'B'G*EEsubstr&r .id' 2r m 1 F r )G.C DBsA

select c unt&distinct substr&r .id'B8':*EEsubstr&r .id 2r m 1

Replace .it$ ! ur table name. .ill pr duce a t tal 2 bl c,s t$ $a)e been used b! t$e data in t$ table' simpl! multipl! t$e 2igur ! ur bl c, size t get t$e actua 2igure. ?dRustment 2 scripts 2 ll .ing de@supp rt 2 s)rmgrl. Oracle $as ann unced 2uture desupp rt 2 s)rmgrl. = u ma! as, ! ursel2' sel2' $ . am / g ing t adRust m! scripts t SNL#LUS and include t$e s$utd .n"startup stu22. Use t$e 2 ll .ing c mmandA s-l$lus Cnolo+ /012connect internal; connected

Summary: Description:

/012shutdown immediate; C % . can / .rite a message t t$e OR?CLE ?lert l g 2r m a #L"SNL pr gramJ Use t$e 2 ll .ing pl"s6lA dbms_s!stem.,sd.rt&H'(? line 2 teCt(*1 Lill .rite t$e teCt t t$e alert l g. Description: Use B instead 2 H t .rite t t$e trace 2ile Use I t .rite t b t$. >$an,s t A V nat$an Le.is =et an t$er Oracle@related .eb siteA $ttpA""....Rlc mp.dem n.c .u, l $u$M

Summary:

Summary: Description:

Summary:

Description:

Summary: Description:

Summary:

Description:

Summary: Description:

Summary:

OracleG installati n creates a ne. acc unt' traces)r"trace' .it$ SELEC> ?

Description:

>$e OracleG install $as pr )ided a )er! danger us bac, d r. /t creates a >race. >$e ne. acc unt is >R?CESPR' and t$e de2ault pass. rd is .ell acc unt is granted SELEC> ?0= >?BLE. >$is 2eature .as disc )ered n a PG.8.: install.

Summary:

SNL L ader parameters .$en 2ields c ntain line 2eeds r carriage returns

SNL L ader c ntr l 2iles use line 2eed r CR t signi2! t$e end 2 t$e rec entr! 2 t$ese c$aracters &m st c mm nl! as part 2 a descripti n r teCt 2 enc unters t$ese line 2eeds' it assumes t$e end 2 t$e rec rd and generates

Description:

> get ar und t$isA B. .$en ! u generate t$e ascii 2ile' put a 2ield delimiter a2ter eac$ c lumn c$aracter as t$e delimiter. H. in t$e SNL L ader c ntr l 2ile' add t$e lineA c ntinuei2 last M;(E(

>$is .ill cause SNL L ader t c rrectl! interpret data .$ere line 2eeds r CRs are a legitimate part 2 t$e data. Summary:

SNL L ader parameters .$en 2ields c ntain line 2eeds r carriage returns

SNL L ader c ntr l 2iles use line 2eed r CR t signi2! t$e end 2 t$e rec entr! 2 t$ese c$aracters &m st c mm nl! as part 2 a descripti n r teCt 2 enc unters t$ese line 2eeds' it assumes t$e end 2 t$e rec rd and generates

Description:

> get ar und t$isA B. .$en ! u generate t$e ascii 2ile' put a 2ield delimiter a2ter eac$ c lumn c$aracter as t$e delimiter. H. in t$e SNL L ader c ntr l 2ile' add t$e lineA c ntinuei2 last M;(E(

>$is .ill cause SNL L ader t c rrectl! interpret data .$ere line 2eeds r CRs are a legitimate part 2 t$e data. Summary: Description: St red pr cedure t rec mpile sc$ema E<EC DBMS_U>/L/>=.COM#/LE_SC%EM?& (sc$ema@name( *1

You might also like