Solved

sqlplus parameters containing single quotes

Posted on 2014-03-24
5
2,198 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
ID: 39951386
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
ID: 39951433
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
ID: 39952279
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
ID: 39954188
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
ID: 39954219
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

911 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

15 Experts available now in Live!

Get 1:1 Help Now