You are on page 1of 30

Declaring Variables

Copyright Oracle Corporation, 2001. All rights reserved.

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

Copyright Oracle Corporation, 2001. All rights reserved.

#$%&'$ ,loc( &tructure


DECLARE -Optional.
Variables, cursors, user-defined e*ceptions

BEGIN -/andatory.

&'$ statements #$%&'$ statements Actions to perform when errors occur

EXCEPTION -Optional. END; -/andatory.

BEGIN EXCEPTION
DECLARE END;
1-+ Copyright Oracle Corporation, 2001. All rights reserved.

)*ecuting &tatements and #$%&'$ ,loc(s


DECLARE v_variable VARCHAR2(5); BEGIN SELECT !l"#$_$a#e INTO v_variable %RO& 'able_$a#e; EXCEPTION (HEN e) e*'i!$_$a#e THEN +++ END;

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

Copyright Oracle Corporation, 2001. All rights reserved.

#rogram 5onstructs

BEGIN EXCEPTION
DECLARE END;

Tools Constructs
Anonymous blocks Application procedures or functions Application packages Application triggers Object types

Database Server Constructs


Anonymous blocks Stored procedures or functions Stored packages Database triggers Object types

1-4

Copyright Oracle Corporation, 2001. All rights reserved.

7se of Variables
Variables can be used for

2emporary storage of data /anipulation of stored values !eusability )ase of maintenance

1-6

Copyright Oracle Corporation, 2001. All rights reserved.

9andling Variables in #$%&'$



Declare and initiali"e variables in the declaration section: Assign new values to variables in the e*ecutable section: #ass values into #$%&'$ bloc(s through parameters: View results through output variables:

1-8

Copyright Oracle Corporation, 2001. All rights reserved.

2ypes of Variables

#$%&'$ variables
&calar 5omposite

!eference LOB -large objects.

<on-#$%&'$ variables ,ind and host variables

1-;

Copyright Oracle Corporation, 2001. All rights reserved.

7sing i&'$>#lus Variables ?ithin #$%&'$ ,loc(s



#$%&'$ does not have input or output capability of its own: @ou can reference substitution variables within a #$%&'$ bloc( with a preceding ampersand: i&'$>#lus host -or AbindB. variables can be used to pass run time values out of the #$%&'$ bloc( bac( to the i&'$>#lus environment:

1-1=

Copyright Oracle Corporation, 2001. All rights reserved.

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.

Declaring #$%&'$ Variables

&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

Copyright Oracle Corporation, 2001. All rights reserved.

Duidelines for Declaring #$%&'$ Variables



3ollow naming conventions: Enitiali"e variables designated as NOT N0LL and CONSTANT: Declare one identifier per line: Enitiali"e identifiers by using the assignment operator -<=. or the DE%A0LT reserved word:
identifier <= expr;

1-1+

Copyright Oracle Corporation, 2001. All rights reserved.

<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

Adopt a naming convention for #$%&'$ identifiers for e*ample, vFemployeeFid

1-10

Copyright Oracle Corporation, 2001. All rights reserved.

Variable Enitiali"ation and Geywords

Assignment operator -<=. DE%A0LT (eyword NOT N0LL constraint &ynta*


identifier <= expr;

)*amples
v_6ire1a'e <= B@?.GAN.2@@?B; v_e$a#e <= B&a1"r!B;

1-11

Copyright Oracle Corporation, 2001. All rights reserved.

&calar Data 2ypes



9old a single value 9ave no internal components
/ !re a$1 /eve$ 2ear/

21-O52-;; 3%!"r

a4! !"r 5a'6er/ br!"46' 5!r'6 "*!$ '6i/ $e8 $a'i!$7

!$'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

Copyright Oracle Corporation, 2001. All rights reserved.

,ase &calar Data 2ypes



CHAR ,(maximum_length)VARCHAR2 (maximum_length) LONG LONG RA( N0&BER ,(precision, scale)BINAR9_INTEGER PLS_INTEGER BOOLEAN

1-16

Copyright Oracle Corporation, 2001. All rights reserved.

,ase &calar Data 2ypes


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

Copyright Oracle Corporation, 2001. All rights reserved.

&calar Variable Declarations


)*amples
DECLARE v_I!b v_ !"$' v_'!'al_/al v_!r1er1a'e _'a)_ra'e v_vali1 +++ VARCHAR2(J); BINAR9_INTEGER <= @; N0&BER(J72) <= @; DATE <= S9SDATE K L; CONSTANT N0&BER(A72) <= M+25; BOOLEAN NOT N0LL <= TR0E;

1-1;

Copyright Oracle Corporation, 2001. All rights reserved.

2he NT9PE Attribute



Declare a variable according to
A database column definition Another previously declared variable 2he database table and column 2he previously declared variable name

#refi* NT9PE with

1-2=

Copyright Oracle Corporation, 2001. All rights reserved.

Declaring Variables with the NT9PE Attribute


&ynta*
identifier Table+ !l"#$_$a#eNT9PE;

)*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

Copyright Oracle Corporation, 2001. All rights reserved.

Declaring ,oolean Variables



Only the values TR0E, %ALSE, and N0LL can be assigned to a ,oolean variable: 2he variables are compared by the logical operators AND, OR, and NOT: 2he variables always yield TR0E, %ALSE, or N0LL: Arithmetic, character, and date e*pressions can be used to return a ,oolean value:

1-22

Copyright Oracle Corporation, 2001. All rights reserved.

5omposite Data 2ypes


TR0E 2A.DEC.JM ATLANTA

#$%&'$ table structure

#$%&'$ table structure

? 2 A C

S&ITH GONES NANC9 TI&


VARCHAR2 BINAR9_INTEGER

? 2 A C

5@@@ 2AC5 ?2 AC5D


N0&BER BINAR9_INTEGER

1-2+

Copyright Oracle Corporation, 2001. All rights reserved.

$O, Data 2ype Variables


,oo( -CLOB. #hoto -BLOB. /ovie -B%ILE. NCLOB

1-20

Copyright Oracle Corporation, 2001. All rights reserved.

,ind Variables

O%& ,ind variable &erver

1-21

Copyright Oracle Corporation, 2001. All rights reserved.

7sing ,ind Variables


2o reference a bind variable in #$%&'$, you must prefi* its name with a colon - .: )*ample
VARIABLE 4_/alar2 N0&BER BEGIN SELECT /alar2 INTO <4_/alar2 %RO& e#*l!2ee/ (HERE e#*l!2ee_i1 = ?LM; END; F PRINT 4_/alar2

1-24

Copyright Oracle Corporation, 2001. All rights reserved.

!eferencing <on-#$%&'$ Variables


&tore the annual salary into a i&'$>#lus host variable:
<4_#!$'6l2_/al <= v_/al F ?2;

!eference non-#$%&'$ variables as host variables: #refi* the references with a colon - .:

1-26

Copyright Oracle Corporation, 2001. All rights reserved.

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

Copyright Oracle Corporation, 2001. All rights reserved.

&ummary
En this lesson you should have learned that

#$%&'$ bloc(s are composed of the following sections


Declarative -optional. )*ecutable -reHuired. )*ception handling -optional.

A #$%&'$ bloc( can be an anonymous bloc(, procedure, or function:

BEGIN EXCEPTION
DECLARE END;

1-2;

Copyright Oracle Corporation, 2001. All rights reserved.

&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-+=

Copyright Oracle Corporation, 2001. All rights reserved.

You might also like