Monday, January 5, 2009

ORA-01756: quoted string not properly terminated

Problem Description:
-----------------------------------

Single quote within a string data type like CHAR, VARCHAR2, NVARCHAR etc. will end up during runtime with error
ORA-01756: quoted string not properly terminated

Cause of The Error:
--------------------------------

In Oracle, single quote is the string delimiter for all string data types. That is data type of CHAR, VARCHAR2 etc. If single quote is one of the characters inside the string, then it should be represented by two single quotes (''). The first single quote is an escape character which signals that a special character is following - here another single quote.

If one single quote is used, Oracle will interpret it as the ending string delimiter and fail with ORA-1756.

Example of The Error And Solution:
---------------------------------------------------

Within PL/SQL,

SQL> !vi my_proc.sql
create or replace procedure my_proc(var in varchar2)as
begin
dbms_output.put_line(var);
end;
/
SQL> @my_proc.sql

Procedure created.

SQL> exec my_proc('can't this be run');

ERROR:
ORA-01756: quoted string not properly terminated

SQL> exec my_proc('can''t this be run');

can't this be run

In first example it returned error and in second case it does not because to include single quote(') another single quote (') is used which is termed as escape character.

PL/SQL procedure successfully completed.

Another example with SQL.
SQL> select 'a'data' from dual;
ERROR:
ORA-01756: quoted string not properly terminated


SQL> select 'a''data' from dual;


'A''DA
------
a'data

No comments:

Post a Comment