Professional Documents
Culture Documents
Objectives
After completing this lesson, you should be able to do the following
!ecogni"e the basic #$%&'$ bloc( and its sections Describe the significance of variables in #$%&'$ Declare #$%&'$ variables )*ecute a #$%&'$ bloc(
1-2
BEGIN -/andatory.
BEGIN EXCEPTION
DECLARE END;
1-+ Copyright Oracle Corporation, 2001. All rights reserved.
BEGIN EXCEPTION
DECLARE END;
1-0 Copyright Oracle Corporation, 2001. All rights reserved.
,loc( 2ypes
Anonymous
,DECLAREBEGIN ../'a'e#e$'/ ,EXCEPTIONEND;
#rocedure
PROCED0RE $a#e IS BEGIN ../'a'e#e$'/ ,EXCEPTIONEND;
3unction
%0NCTION $a#e RET0RN 1a'a'2*e IS BEGIN ../'a'e#e$'/ RET0RN val"e; ,EXCEPTIONEND;
1-1
#rogram 5onstructs
BEGIN EXCEPTION
DECLARE END;
Tools Constructs
Anonymous blocks Application procedures or functions Application packages Application triggers Object types
1-4
7se of Variables
Variables can be used for
1-6
1-8
2ypes of Variables
#$%&'$ variables
&calar 5omposite
1-;
1-1=
2ypes of Variables
TR0E
21412=:=8
21-CA<-=1
3%!"r / !re a$1 /eve$ 2ear/ a4! !"r 5a'6er/ br!"46' 5!r'6 "*!$ '6i/ !$'i$e$'7 a $e8 $a'i!$7 !$ eive1 i$ LIBERT97 a$1 1e1i a'e1 '! '6e *r!*!/i'i!$ '6a' all #e$ are rea'e1 e:"al+;
Atlanta
1-11 Copyright Oracle Corporation, 2001. All rights reserved.
&ynta*
identifier ,CONSTANT- datatype ,NOT N0LL,<= > DE%A0LT expr-;
)*amples
DECLARE v_6ire1a'e v_1e*'$! v_l! a'i!$ _ !## DATE; N0&BER(2) NOT N0LL <= ?@; VARCHAR2(?A) <= BA'la$'aB; CONSTANT N0&BER <= ?C@@;
1-12
1-1+
<aming !ules
2wo variables can have the same name, provided they are in different bloc(s: 2he variable name -identifier. should not be the same as the name of table columns used in the bloc(:
DECLARE e#*l!2ee_i1 N0&BER(D); BEGIN SELECT e#*l!2ee_i1 INTO e#*l!2ee_i1 %RO& e#*l!2ee/ (HERE la/'_$a#e = BE! 66arB; END; F
1-10
)*amples
v_6ire1a'e <= B@?.GAN.2@@?B; v_e$a#e <= B&a1"r!B;
1-11
21-O52-;; 3%!"r
!$'i$e$'7 a
TR0E
21412=:=8
!$ eive1 i$
LIBERT97 a$1 1e1i a'e1 '! '6e *r!*!/i'i!$ '6a' all #e$ are rea'e1 e:"al+;
Atlanta
1-14
1-16
DATE TI&ESTA&P TI&ESTA&P (ITH TI&E HONE TI&ESTA&P (ITH LOCAL TI&E HONE INTERVAL 9EAR TO &ONTH INTERVAL DA9 TO SECOND
1-18
1-1;
1-2=
)*amples
+++ v_$a#e v_bala$ e v_#i$_bala$ e +++ e#*l!2ee/+la/'_$a#eNT9PE; N0&BER(L72); v_bala$ eNT9PE <= ?@;
1-21
1-22
? 2 A C
? 2 A C
1-2+
1-20
,ind Variables
1-21
1-24
!eference non-#$%&'$ variables as host variables: #refi* the references with a colon - .:
1-26
DB&S_O0TP0T+P0T_LINE
An Oracle-supplied pac(aged procedure An alternative for displaying data from a #$%&'$ bloc( /ust be enabled in i&'$>#lus with SET SERVERO0TP0T ON
SET SERVERO0TP0T ON DE%INE *_a$$"al_/al = D@@@@ DECLARE v_/al N0&BER(J72) <= O*_a$$"al_/al; BEGIN v_/al <= v_/alF?2; DB&S_O0TP0T+P0T_LINE (BT6e #!$'6l2 /alar2 i/ B >> TO_CHAR(v_/al)); END; F
1-28
&ummary
En this lesson you should have learned that
BEGIN EXCEPTION
DECLARE END;
1-2;
&ummary
En this lesson you should have learned that
#$%&'$ identifiers
Are defined in the declarative section 5an be of scalar, composite, reference, or LOB data type 5an be based on the structure of another variable or database object 5an be initiali"ed
Variables declared in an e*ternal environment such as i&'$>#lus are called host variables: 7se DB&S_O0TP0T+P0T_LINE to display data from a #$%&'$ bloc(:
1-+=