Solved

sqlplus parameters containing single quotes

Posted on 2014-03-24
5
2,594 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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.
1
 
LVL 35

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 77

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

1
 

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.
1
 
LVL 35

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.
1

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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 recover a database from a user managed backup

688 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