简体   繁体   English

在Java DB2 JDBC中:如何在WHERE子句中为SELECT语句使用null参数,其中值可以为null或不为null?

[英]In Java DB2 JDBC: How can one use null parameters in a WHERE clause for a SELECT statement where the value can be either null or not null?

For example I have the following select query for a PreparedStatement: 例如,我对PreparedStatement有以下选择查询:

"SELECT FOO FROM BAR WHERE FOOBAR=?" “选择FOO从哪里FOOBAR =?”

The parameter for FOOBAR can have a value and it could also be null. FOOBAR的参数可以有一个值,也可以为null。

Would the following code work? 以下代码是否有效?

if(<condition>) preparedStatement.setString(1, "<string value>");
else preparedStatement.setString(1, null);

If not, how should this be handled? 如果没有,应如何处理?

It won't work, null has no equivalence. 它不起作用,null没有等价。 If you're using a PreparedStatement, try this: 如果您正在使用PreparedStatement,请尝试以下操作:

if(<condition>) preparedStatement.setObject(1, "<string value>");
else preparedStatement.setNull(1, Types.VARCHAR);

It will send the null as an IN parameter. 它将null作为IN参数发送。

As per SQL standard, if a column has a NULL value, the search condition to check if it has NULL value is "column IS NULL". 根据SQL标准,如果列具有NULL值,则检查其是否具有NULL值的搜索条件是“列IS NULL”。 If column does not have null value you can have condition such as "column = 'blabla'. To make parameter markers work in both cases, change the statement as following: 如果列没有空值,则可以使用“column ='blabla'等条件。要使参数标记在两种情况下均有效,请按以下方式更改语句:

SELECT FOO FROM BAR WHERE COALESCE(FOOBAR, 'a') = ? "

and have the following code: 并具有以下代码:

if(<condition>) preparedStatement.setString(1, "<string value>");
else preparedStatement.setString(1, "a");

Whatever @Mark suggested did not work for me on Oracle. 无论@Mark建议什么都不适用于Oracle。

空值参数与列中的空值之间存在差异。

if(requiredValue == null){
 sql = "SELECT * FROM Table Where COLUMN is NULL"
}

You don't have to set the value in this case. 在这种情况下,您不必设置值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JDBC - 选择列为NULL的位置 - JDBC - select where column is NULL JDBC:如何在WHERE子句中处理不同数据库的IS NULL? - JDBC: How to handle IS NULL in WHERE clause for different databases? 如何使用 jdbc 在 java 中编写选择查询,需要检查 COLUMN 是否为 NULL? - HOW to write select query in java using jdbc where to need to check if COLUMN IS NULL? 当where子句条件为null时更新SQL - Update SQL when where clause condition can be null JDBC如何在带where子句的预准备语句中使用占位符? - JDBC How can I use a place holder in a prepared statement with a where clause? Select where 语句返回所有 null - Select where statement returning all null 如何在node.js中的IBM DB2 iSeries的sql select的where子句中使用固定长度的CCSID 37 char值 - How to use fixed length CCSID 37 char value in where clause of sql select for IBM DB2 iSeries in node.js 设置准备好的语句参数 where NOT NULL java - set prepared statement parameter where NOT NULL java 考虑 sql 中的可为空参数,可以使用 if 条件或使用条件 where 子句构建。 哪个更好用 - Consider nullable parameters in an sql, can be built up either with if conditions or using a conditional where clause. Which one is better to use 休眠-字段A OR B可以为null,但A或B之一不得为null - Hibernate - Either field A OR B can be null but one of A or B must not be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM