SQL选择查询w / Null参数

时间:2016-04-05 10:10:41

标签: c# sql select

我在2个类似的SQL Select Querys'上有错误,我的假设是我错误地使用了NULL。有人看了看,告诉我哪里出错了。

string CountHolidayQuery = "SELECT StartDate, EndDate FROM [Holiday] WHERE PayrollNo = @PayrollNo AND Authorised IS NOT Null";

string CountRequestedQuery = "SELECT StartDate, EndDate FROM [Holiday] WHERE PayrollNo = @PayrollNo AND Authorised IS Null";

4 个答案:

答案 0 :(得分:4)

Authorised = Null无效,应为Authorised IS NullAuthorised IS NOT Null

答案 1 :(得分:4)

在SQL中,Null并不像值那样对待,实际上是一种特殊情况,其语法为Is NullIs Not Null,例如

string CountHolidayQuery = "SELECT StartDate, EndDate FROM [Holiday] WHERE PayrollNo = @PayrollNo AND Autorised Is Not Null";

以及

string CountRequestedQuery = "SELECT StartDate, EndDate FROM [Holiday] WHERE PayrollNo = @PayrollNo AND Authorised Is Null";

在第一个声明中,Authorised不应该为空,它输入错误,而是读取Autorised

答案 2 :(得分:2)

应该是" IS NOT null",缺少查询IS

SELECT StartDate, EndDate FROM [Holiday] WHERE PayrollNo = @PayrollNo AND Autorised IS NOT Null";

第二个查询Authorised = Null不是有效的sql语法。

答案 3 :(得分:1)

我认为您错过了关键字

所以声明如下:

string CountHolidayQuery = "SELECT StartDate, EndDate FROM [Holiday] WHERE PayrollNo = @PayrollNo AND Autorised is NOT Null";