Sunday, March 11, 2012

PL/SQL Constant

You can declare a constant in PL/SQL as follows:

SQL> set serveroutput on
SQL> declare
  2  my_name constant varchar2(6) := 'Andrew';
  3  begin
  4  dbms_output.put_line('My name is '||my_name);
  5  end;
  6  /
My name is Andrew

PL/SQL procedure successfully completed.

SQL>

But if you try to change its value, Oracle will display an error message:

SQL> declare
  2  my_name constant varchar2(6) := 'Andrew';
  3  begin
  4  my_name := 'Fred';
  5  end;
  6  /
my_name := 'Fred';
*
ERROR at line 4:
ORA-06550: line 4, column 2:
PLS-00363: expression 'MY_NAME' cannot be used as an
assignment target
ORA-06550: line 4, column 2:
PL/SQL: Statement ignored

SQL>

No comments:

Post a Comment