2013-03-29 52 views
1

我有SQL查询:如何选择结果与空集

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

1,2是我添加有动态参数。但是,如果我有空集:

select * from table where id in() 

那么这个查询调用异常:

ERROR at line 1: 
ORA-00936: missing expression 

我应该如何创建SQL空集

+1

你应该避免空集,毕竟如果设置为空,查询结果为空。在外面检查,如果设置不为空,则选择,如果为空,则跳过。 – Vesper

回答

3

您可以随时添加null到您设定的,所以当真实集合为空时,您将得不到语法错误:

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

vs

select * from table where id in (null) 

你的发电机会更简单,也因为您可以在您添加的每个项目前打印,,没有检查,如果它是第一个与否。

当然,因为您正在生成您的SQL,所以针对SQL Injection的标准注意事项适用:请勿让用户在您生成的SQL附近的任何地方输入内容。

0

试试这个

select * from table where id in (null)