Although it is not required, it is generally a good idea to initialize a variable when creating it. When variables are created, they can be assigned a value using the assignment operator, :=, or the DEFAULT keyword. Oracle suggests that you use DEFAULT for variables that have a typical value and the assignment operator for variables that do not. A declaration can also specify a NOT NULL constraint, to prevent the assignment of a NULL value to the variable. Variables that are not initialized have the value NULL. As with SQL, literal character data is always enclosed in single quotes. Similarly, quoted strings may not contain line breaks.
DECLARE my_variable VARCHAR2(11); BEGIN NULL; END;
This example consists of the simple declaration of a VARCHAR2 variable. Verify that code can compile (run will do nothing).
DECLARE my_variable VARCHAR2(11) := 'Hello World'; BEGIN NULL; END;
This example involves both the declaration and initialization of a VARCHAR2 variable. This code can also compile but there will be no visible results. Variable assignment can be performed using the := operator. (Keep this in mind if you have a background in a language where = is used for assignment).
--using default keyword DECLARE my_variable VARCHAR2(11) NOT NULL default 'Hello World'; BEGIN dbms_output.put_line(my_variable); END;
This exampledeclares, initializes and sets the default value of a VARCHAR2 variable.
Variables can also be assigned values after declaration, in the executable section. This is typically done in one of two ways, using the assignment operator (:=) or a SELECT INTO statement.
DECLARE my_date DATE; BEGIN my_date := current_date; DBMS_OUTPUT.PUT_LINE('My date is: ' || my_date); END;
The DATE data type is assigned in this example using the := operator.
DECLARE my_date DATE; BEGIN select sysdate into my_date from dual; DBMS_OUTPUT.PUT_LINE('My date is (from select into): ' || my_date); END;
The SELECT INTO statement is used above to assign a value (rather than using the := operator as in the previous example).
Once you have declared a variable and assigned it a value, you can display its assigned value in the context of PL/SQL using the DBMS_OUTPUT.PUT_LINE stored procedure.
DECLARE my_variable VARCHAR2(11) := 'Hello World'; BEGIN DBMS_OUTPUT.PUT_LINE(my_variable); END;
This demonstration illustrates the declaration, initialization and display of a VARCHAR2 variable. As discussed previously, ensure that serveroutput is on to display the results.
As is the case with most languages, special care must be taken when dealing with NULL values. For example, a variable can be assigned the value of NULL + 1. The resulting assignment sets the variable to NULL, which might not be the intent.
DECLARE my_number NUMBER; BEGIN my_number := my_number + 1; DBMS_OUTPUT.PUT_LINE('My number is: ' || my_number); END;
This demonstration involves the declaration of a NUMBER which is not initialized but subsequently incremented. The absence of a displayed result illustrates that NULL is not the same as zero, and variables are not initialized to zero or another value by default.
DECLARE my_number NUMBER := 0; BEGIN my_number := my_number + 1; DBMS_OUTPUT.PUT_LINE('My number is: ' || my_number); END;
The proper declaration, initialization, incrementing and display of a variable is shown in this example.
At times you may want to utilize a special character in a character string such as a tab or a newline. The CHR function can be used to insert these types of special characters.
BEGIN dbms_output.put_line('9:' || CHR(9) || '9.' || '10:' || CHR(10) || 'end 10.' || '13:' || CHR(13) || 'end 13.' || '15:' || CHR(15) || 'end 15.'); END;
In this example characters 9 (tab), 10 (line feed), 13 (carriage return), and 15 (shift in) are used to influence the output.