DECLARARTIVE PART
DECLARARTIVE PART:
You can declare variables and constants in the declarative part of the PL/SQL block.The declaration section starts with the declare keyword ,follwed by a list of variables and cursor definitions.
Declaring Variable
Declarations allocate storage space for a value, specify its datatype, and name the storage location so that you can reference it.
eg:
name varchar(20);
salary number(7,2);
Here the first declaration names a variable of type varchar. The second declaration names a variable of type number .You can also assign value to the variable at the time of declaration.
By default, variables are initialized to NULL.
Note:PL/SQL does not allow forward references. You must declare a variable or constant before referencing it in other statements, including other declarative statements. For example, the following declaration of maxi is illegal:
maxi INTEGER := 2 * mini; -- illegal
mini INTEGER := 15;
Declaring Constants
In the declaration of a constant, the keyword CONSTANT must precede the type specifier, as the following example shows:
pi CONSTANT number(5,2) := 3.14;
You can also assign constant values via the default keyword.
pi number (9,7) default 3.14
Assigning pre-defined data type to a variable
You can also use pre-defined data type for declaring a variable.The %TYPE attribute is used for this purpose.The %TYPE attribute provides the datatype of a pre defined variable or database column. In the following example, %TYPE provides the datatype of a variable:
Eg:
Bonus number(7,2)
DA bonus%type;
So here the variable DA use the same data type of the variable bonus.
You can also use the %type attribute to declare variables based on definitions of column in a table.
%ROWTYPE:Allows you to declare record type that represent a row in a table.
Eg:
Consider the table AREA,which contains two fields area and radius.
If you want to declare a variable based on the datatype of field radius ,you can use the following statement in the declarative part
rad AREA.radius%type.
So here the variable rad use the same data type of the field radius in the table area.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home