Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Beth.

Asked: October 15, 2001 - 8:01 pm UTC

Last updated: October 02, 2009 - 7:47 am UTC

Version: 8.1.6

Viewed 10K+ times! This question is

You Asked

When you use a construct such as this:

select table_name
from all_tables
where table_name like 'XX\_%' escape '\';

must you have the SET ESCAPE option set to ON, or OFF, or the character. I am confused by the purpose of the SET ESCAPE option, as it seems that my statement above only works with SET ESCAPE OFF

and Tom said...

SET ESCAPE is a sqplus'ism and does not affect the behavior of this command.

It does affect the behavior of SQLPLUS however.

ESC[APE] {\| c|ON|OFF}
Defines the character you enter as the escape character. OFF undefines
the escape character. ON enables the escape character. ON changes the
value of c back to the default "\".

You can use the escape character before the substitution character (set
through SET DEFINE) to indicate that SQL*Plus should treat the substi-tution
character as an ordinary character rather than as a request for
variable substitution.


It is not in anyway related to the "escape" in the sqlplus like clause. What you are seeing is sqlplus pre-processing your query. It sees the "escape" from set escape on and assumes it is an escape character and removes it!

Consider:

ops$tkyte@ORA717DEV.US.ORACLE.COM> set escape on
ops$tkyte@ORA717DEV.US.ORACLE.COM> show escape
escape "\" (hex 5c)
ops$tkyte@ORA717DEV.US.ORACLE.COM> select '\' from dual;

'
-


ops$tkyte@ORA717DEV.US.ORACLE.COM> select '\\' from dual;

'
-
\

ops$tkyte@ORA717DEV.US.ORACLE.COM> select '\\\' from dual;

'
-
\

ops$tkyte@ORA717DEV.US.ORACLE.COM> select '\\\\' from dual;

'\
--
\\

But the SQL "escape" character is totally different and can be any character:


ops$tkyte@ORA717DEV.US.ORACLE.COM> create table t ( x varchar2(5) );

Table created.

ops$tkyte@ORA717DEV.US.ORACLE.COM> insert into t values ( 'X_X' );

1 row created.

ops$tkyte@ORA717DEV.US.ORACLE.COM> select * from t where x like '%#_%' escape '#';

X
-----
X_X

Rating

  (7 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, October 23, 2001 - 10:16 am UTC

Thanks!
-Beth

A reader, July 05, 2005 - 6:43 pm UTC


Helped me fix my insert

Jim, July 20, 2006 - 3:01 pm UTC

I was trying to insert part numbers into a temp table and the part numbers contained & symbols. I was able to add a \ before the &, then set commit on; and run my insert commands. Thanks for the help.

Tom Kyte
July 22, 2006 - 5:12 pm UTC

set define off
insert .....
commit;
set define on


and ONLY applies to sqlplus since SQLPLUS is the thing that sees "&" and does something special.

special characters while building insert statement

Ravi, September 28, 2009 - 4:58 pm UTC

Hi Tom,

I have a table with one of the columns as varchar2(4000) which stores html pages. This html text consists of &,'," commas etc. I wanted to exp/imp this table from prod to dev for my development/testing. Since i dont have exp privileges on this table(only have select previlege,cant get a DBA to do it, the process of creating a ticket etc would take a week) i wanted to construct INSERT INTO SELECT statements and run in my dev box from sqlplus. The problem is I am unable to construct the varchar2 coulumn in quotes as it has various special characters in the html text. I could escape & with set escape but I dont know what to do with single quotes, double quotes, commas etc. Your help is greatly appreciated. This table has about 10k records, not a big one.

Thanks
Tom Kyte
October 02, 2009 - 7:47 am UTC

you would issue a

SQL> set define off


in order to "escape" the & - & is just a special sqlplus character, not a special sql character.

and then

SQL> alter session set cursor_sharing=force;

before running the script generated by the code below.

the only other thing you have to "worry" about would be single quotes.

you would select:

ops$tkyte%ORA11GR1> create table emp as select replace( ename, 'A', 'A''' ) ename, empno, hiredate from scott.emp;

Table created.

ops$tkyte%ORA11GR1> drop table t;

Table dropped.

ops$tkyte%ORA11GR1> create table t as select replace( ename, 'A', 'A''' ) ename, empno, hiredate from scott.emp where 1=0;

Table created.

ops$tkyte%ORA11GR1>
ops$tkyte%ORA11GR1>
ops$tkyte%ORA11GR1> begin
  2          for x in (select ename, empno, hiredate from emp )
  3          loop
  4                  dbms_output.put_line( 'insert into t ( ename, empno, hiredate ) values (' );
  5                  dbms_output.put_line( '''' || replace( x.ename, '''', '''''' ) || ''', '  );
  6                  dbms_output.put_line( x.empno || ', ' );
  7                  dbms_output.put_line( 'to_date( ''' || to_char(x.hiredate,'yyyymmddhh24miss') || ''', ''yyyymmddhh24miss'' ) );' );
  8          end loop;
  9  end;
 10  /
insert into t ( ename, empno, hiredate ) values (
'SMITH',
7369,
to_date( '19801217000000', 'yyyymmddhh24miss' ) );
insert into t ( ename, empno, hiredate ) values (
'A''LLEN',
7499,
to_date( '19810220000000', 'yyyymmddhh24miss' ) );
insert into t ( ename, empno, hiredate ) values (
'WA''RD',
7521,
to_date( '19810222000000', 'yyyymmddhh24miss' ) );
insert into t ( ename, empno, hiredate ) values (
'JONES',
7566,
to_date( '19810402000000', 'yyyymmddhh24miss' ) );
....




special characters while building insert statement

Ravi, September 28, 2009 - 4:59 pm UTC

Hi Tom,

I have a table with one of the columns as varchar2(4000) which stores html pages. This html text consists of &,'," commas etc. I wanted to exp/imp this table from prod to dev for my development/testing. Since i dont have exp privileges on this table(only have select previlege,cant get a DBA to do it, the process of creating a ticket etc would take a week) i wanted to construct INSERT INTO SELECT statements and run in my dev box from sqlplus. The problem is I am unable to construct the varchar2 coulumn in quotes as it has various special characters in the html text. I could escape & with set escape but I dont know what to do with single quotes, double quotes, commas etc. Your help is greatly appreciated. This table has about 10k records, not a big one.

Thanks

A reader, October 02, 2009 - 8:00 am UTC


Use a CLOB

You are the best!

Ravi, October 02, 2009 - 11:17 am UTC