A variable is a name for a specific memory location, which is human readable. With the help of variable (data container you can say) our program can store data. Every variable has a datatype. Datatype defines the type of data and size of data , the variable is going to have. Consequently, the operation on it differs.
- The variable name must be less than 31 characters.
- The variable name must begin with an ASCII letter. Notice that PL/SQL is case-insensitive.
- Followed by the first character are any number, underscore (
_), and dollar sign (
Naming convention :
Though each organisation has its own naming convention, however oracle uses following naming
For example, to declare a name you can write v_name VARCHAR2(100);
Here v_name means, name which will contain a varchar2 data.
PLSQL variable declaration:
To declare a variable, you need to mention datatype , scale (depending on situations we will discuss in subsequent tutorials) and ends with semicolon ( ; ).
DECLARE v_fname varchar2(20); v_lname varchar2(20); n_emp_id number; d_hire_dt date; BEGIN NULL; END;
Variable Assignment :
Variables can be assigned with values either at the time of declaration ( also termed as data initialization at the time of declaration ) or inside the begin and end block .
DECLARE v_name VARCHAR2(50) := 'CodeBook.co.in' ; --generally used to give a initial value BEGIN dbms_output.put_line('v_name value is : ' || v_name); END: /
DECLARE v_name VARCHAR2(50); BEGIN v_name := 'PLSQL is simple.'; dbms_output.put_line('v_name value is : ' || v_name); END: /
PLSQL Constants :
A constant is a special type of variable whose value remains unchanged throughout the program execution. The key thing is for constant , value has to be assigned at the time of declaration.
Declaration of Constants :
variable_name CONSTANT datatype(scale);
DECLARE pi CONSTANT NUMBER := 3.14; BEGIN dbms_output.put_line('value of pi is :' || pi); END:
A literal is an explicit boolean, string, numeric, string value. It does not have any variable name as its reference is not stored.
|Character Literals||‘A’ ‘&’|
|Boolean Literals||TRUE FALSE NULL|
|Date and Time Literals||DATE ‘2001-03-03’; TIMESTAMP ‘2016-11-20 02:22:22’;|
DECLARE text varchar2(30):= 'WOW..PLSQL is simple'; BEGIN dbms_output.put_line(text); END; /