1

Possible Duplicate:
How to handle a single quote in Oracle SQL

My Senario: I want to save the query(not result set) in the database. I am using Java as the front end; My Table(Querytab) has the following Fields:

sno VARCHAR2(1024)
QUERY VARCHAR2(4000)

The problem is when I insert a query which has conditions e.g insert into querytab values('100','select * from querytab where sno ='100'');

ERROR at line 1: ORA-00933: SQL command not properly ended

Problem is: The Queryvalue gets terminated when it finds the ' How to solve this. Thanks in advance.

2
  • @Mat : This is not duplicate of what you are saying... He want to store the query... Commented Jan 21, 2012 at 9:08
  • 2
    The problem is the same. You need to escape your strings properly. (And if you're doing this from code, you should be using prepared statements and bind variables.) Commented Jan 21, 2012 at 9:09

3 Answers 3

2

To insert single quotes, you need to escape them. Escaping is done by simply duplicating them:

insert into querytab 
(sno, query)
values
('100','select * from querytab where sno = ''100'' ');

This is explained in the manual:

http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements003.htm#i42617

Sign up to request clarification or add additional context in comments.

Comments

1

Use PreparedStatement to escape the string.

Something like this:

sql="insert into querytab values (?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,"100");
ps.setString(2,"select * from querytab where sno ='100'");
ps.executeUpdate();

Comments

0

try this,

insert into querytab values('100','select * from querytab where sno =100');

1 Comment

You're storing a different string. That's not the string that the OP wants to store.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.