2010年5月13日星期四

Print the value of a PL/SQL local variable.

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:

  1. We declare a bind variable as follows:
  2.      VARIABLE  
    where the type can be only one of three things: NUMBER, CHAR, or CHAR(n).
  3. We may then assign to the variable in a following PL/SQL statement, but we must prefix it with a colon.
  4. Finally, we can execute a statement
  5.      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;