PLSQL Variable Types
PLSQL Variable Types
http://www.tutorialspoint.com/plsql/plsql_variable_types.htm
Copyright tutorialspoint.com
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in PL/SQL has a specific data type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory and the set of operations that can be applied to the variable. The name of a PL/SQL variable consist of a letter optionally followed by more letters, numerals, dollar signs, underscores, and number signs and should not exceed 30 characters. By default, variable names are not case-sensitive. You cannot use a reserved PL/SQL keywords as a variable name. PL/SQL programming language allows to define various type of variables which we will cover in subsequent chapters like date time data types, records, collections etc. For this chapter, let us study only basic variable types.
Where, variable_name is a valid identifier in PL/SQL, datatype must be a valid PL/SQL data type or any user defined data type which we already have discussed in last chapter. Some valid variable declarations along with their definition are shown below:
sales number(10, 2); pi CONSTANT double precision := 3.1415; name varchar2(25); address varchar2(100);
When you provide a size, scale or precision limit with the data type, it is called a constrained declaration. Constrained declarations require less memory than unconstrained declarations, For example:
sales number(10, 2); name varchar2(25); address varchar2(100);
You can also specify that a variable should not have a NULL value using the NOT NULL constraint. If you use the NOT NULL constraint, you must explicitly assign an initial value for that variable.
It is a good programming practice to initialize variables properly otherwise, sometime program would produce unexpected result. Try following example which makes use of various types of variables:
DECLARE a integer := 10; b integer := 20; c integer; f real; BEGIN c := a + b; dbms_output.put_line('Value of c: ' || c); f := 70.0/3.0; dbms_output.put_line('Value of f: ' || f); END; /
num1); num2);
|| num1); || num2);
You can use the SELECT INTO statement of SQL to assign values to PL/SQL variables. For each item in the SELECT list, there must be a corresponding, type-compatible variable in the INTO list. The following example illustrates the concept: Let us create a table named CUSTOMERS: (For SQL statements please look at the SQL tutorial)
CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); Table Created
The following program assigns values from the above table to PL/SQL variables using the SELECT INTO clause of SQL:
DECLARE c_id customers.id%type := 1; c_name customers.name%type; c_addr customers.address%type; c_sal customers.salary%type; BEGIN SELECT name, address, salary INTO c_name, c_addr, c_sal FROM customers WHERE id = c_id; dbms_output.put_line ('Customer ' ||c_name || ' from ' || c_addr || ' earns ' || c_sal); END; /