Solved

sqlplus parameters containing single quotes

Posted on 2014-03-24
5
2,414 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle differnce between two timestamps 5 38
date show only hh:mm 2 39
Pivoting oracle table 9 74
Need to Understand Resolution to Oracle Error ORA-00600 2 45
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 take different types of Oracle backups using RMAN.
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.

733 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