Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sqlplus parameters containing single quotes

Posted on 2014-03-24
5
Medium Priority
?
2,927 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 1000 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 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

609 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