Sometimes we might want to print the value of a PL/SQL local variable. A ``quick-and-dirty'' way is to store it as the sole tuple of some relation and after the PL/SQL statement print the relation with a
SELECT statement. A more couth way is to define a
bind variable, which is the only kind that may be printed with a
print command. Bind variables are the kind that must be prefixed with a colon in PL/SQL statements, such as
:new discussed in the section on
triggers.
The steps are as follows:
- We declare a bind variable as follows:
VARIABLE
where the type can be only one of three things: NUMBER, CHAR, or CHAR(n). - We may then assign to the variable in a following PL/SQL statement, but we must prefix it with a colon.
- Finally, we can execute a statement
PRINT :;
outside the PL/SQL statement
Here is a trivial example, which prints the value 1.
VARIABLE x NUMBER
BEGIN
:x := 1;
END;
.
run;
// print the variable after running the former commands
PRINT :x;