How to declare reserved words as variables in Oracle PL/SQL?

Posted on

Question :

What is the best way to bypass the Oracle SQL reserved-word constraint for PL/SQL value declaration? I realize bind and substitution may be acceptable alternatives for an actual query, but what about Oracle PL/SQL? For example I would like to declare a numeric variable as “num”, structured below within the Oracle PL query:

SET SERVEROUTPUT ON
declare
  num number;
begin
   num := 10;
   DBMS_OUTPUT.put_line('The value of num ' || num); 
end;

Answer :

NUM is not a reserved word.

The best way is not to use reserved words.

If you insist on using them, you can put them between quotation marks. This does not work:

SET SERVEROUTPUT ON
declare
  begin number;
begin
   begin := 10;
   DBMS_OUTPUT.put_line('The value of begin ' || begin); 
end;
/

This does:

SET SERVEROUTPUT ON
declare
  "begin" number;
begin
   "begin" := 10;
   DBMS_OUTPUT.put_line('The value of begin ' || "begin"); 
end;
/

Quotation marks also make the variable name case-sensitive. "begin", "Begin" and "BEGIN" are different variables.

Leave a Reply

Your email address will not be published. Required fields are marked *