1.

How to declare and initialize Variables in PL/SQL

Answer»

PL/SQL allocates memory for the variable's value when a variable is declared. The storage location is IDENTIFIED by the variable name.

Declaration

The following is the syntax:

variable_name [CONSTANT] datatype [NOT NULL] [:= | DEFAULT initial_value]

Here,

  • variable_name: Name of the variable
  • datatype: The datatype

Example for a string variable:

name varchar2(15);

Let us see an example for a number precision limit. This is called CONSTRAINT declaration that needs less memory:

a number(5, 2)

Initialization

To INITIALIZE a variable, you can use the DEFAULT keyword as well as the assignment operator. Let us see them one by one.

Initialization with DEFAULT keyword:

DECLARE   msg varchar2(15) DEFAULT 'House'; BEGIN   dbms_output.put_line(msg); END; /

The output: 

House

Initialization with assignment operator: 

DECLARE   DEVICE varchar2(11) := 'Laptop'; BEGIN   dbms_output.put_line(device); END; /

The output: 

Laptop



Discussion

No Comment Found