Solved

sqlplus parameters containing single quotes

Posted on 2014-03-24
5
2,160 Views
Last Modified: 2014-03-25
Is there anyway to pass a parameter containing single quotes to sqlplus?

Using sqlplus, I  pass a parameter within a string that contains single quotes.
SQLPLUS interprets single quotes as a delimiter for multiple parameters.  

Example:

   DOS Batch File:

sqlplus userid/password  @test.sql   'SCHEMA1','SCHEMA2','SCHEMA3'

Open in new window


   TEST.SQL script


      DEFINE SCHEMAS="&1"
 
      PROMPT &&SCHEMAS
      
      prompt SCHEMAS

      select table_name
      from all_tables
      where owner in (&&SCHEMAS);

Open in new window


   Results:
     
     
SCHEMA1
      
      select table_name
      from all_tables
      where owner in (SCHEMAS1);
      
      ERROR at line 3:
      ORA-00904: "SCHEMAS1": invalid identifier

Open in new window

0
Comment
Question by:ciphersol
5 Comments
 
LVL 23

Expert Comment

by:David
Comment Utility
Traditionally the solution is to offset a delimited with an adjacent one, e.g., '' .  From 10g, however, try the quote mark shown following.

insert into testA (x) values (q'[yourstring]');

If necessary, the SET SQL_DELIMITER command might be useful to reset the single quote as another symbol.  Syntax is in the docs.
0
 
LVL 34

Accepted Solution

by:
johnsone earned 250 total points
Comment Utility
I'm not that familiar with DOS, but in a UNIX environment, if you change the command line to this:

sqlplus userid/password  @test.sql   "'SCHEMA1','SCHEMA2','SCHEMA3'"

It should work.  That is putting double quotes around the entire parameter.  The DOS shell is interpreting (and removing) the quotes, which is your problem.
0
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
Comment Utility
Try this.

SQL script:
DEFINE SCHEMAS="&1"

select table_name
from all_tables
where owner in (&1);      
where owner in (&&SCHEMAS);      

exit

Open in new window


Call it from DOS (Note: ALL single quotes, no double quotes):
sqlplususerid/password  @test.sql  '''SCHEMA1'',''SCHEMA2'',''SCHEMA3'''

Open in new window

0
 

Author Comment

by:ciphersol
Comment Utility
Interesting.  Neither solution worked but I combined the suggestions from johnsone and slightwv and it worked both in windows and on UNIX.

sqlplus userid/password  @test.sql  "'''SCHEMA1'',''SCHEMA2'',''SCHEMA3'''"

It may be hard to see but I put double quotes around what slightwv suggested and it worked.  

Results for    PROMPT &&SCHEMAS   in both UNIX and DOS:

'SCHEMA1','SCHEMA2','SCHEMA3'


Thank you for the help.
0
 
LVL 34

Expert Comment

by:johnsone
Comment Utility
It is all about getting the correct quotes in there so that the shell doesn't remove the ones you need.

I usually have to experiment with a few different ways of putting in the quotes before I find the magic one.

Glad you were able to get it working.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now