hudi
hudi

Reputation: 16555

How to select result with empty set

I have sql query:

select * from table where id in (1,2)

1,2 are parameters which I add there dynamically. But what If I have empty set:

select * from table where id in ()

then this query call exception:

ERROR at line 1:
ORA-00936: missing expression

how should I create sql with empty set

Upvotes: 3

Views: 3040

Answers (2)

Harshil
Harshil

Reputation: 411

Try this

 select * from table where id in (null)

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727077

You can always add null to your set, so when the real set is empty, you get no syntax error:

select * from table where id in (null,1,2)

vs

select * from table where id in (null)

Your generator would be simpler, too, because you can print a , in front of every item that you add, without checking if it's the first one or not.

Of course since you are generating your SQL, the standard precautions against SQL Injection apply: do not let user input anywhere near the SQL that you generate.

Upvotes: 5

Related Questions