且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

具有WHERE子句和Null的SQL Server参数化查询

更新时间:2023-11-30 21:01:40

在SQL中,您无法比较空值,即,即使字段中的值为空,EngSerialNum = null始终求值为false.

In SQL you can't compare null values, i.e. EngSerialNum = null always evaluates to false, even if the value in the field is null.

您可以动态创建查询,以便使用is null来匹配空值,或者可以使用如下表达式:

Either you can create the query dynamically so that you use is null to match the null values, or you can use an expression like this:

((EngSerialNum is null and @EngSerialNo is null) or EngSerialNum = @EngSerialNo)