Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TSQL:NULL在";在;条款_Sql_Sql Server_Tsql_Sql Server 2008 - Fatal编程技术网

TSQL:NULL在";在;条款

TSQL:NULL在";在;条款,sql,sql-server,tsql,sql-server-2008,Sql,Sql Server,Tsql,Sql Server 2008,我需要在以下情况下过滤记录: NotificationRead = 0 || NULL --> IF GetRead = 0 NotificationRead = 1 --> IF GetRead = 1 NotificationRead = 0 || 1 || NULL --> IF GetRead = NULL 以下是我对此使用的查询: DECLARE @GetRead BIT DECLARE @Query VARCHAR(20) SET @GetRead = NULL

我需要在以下情况下过滤记录:

NotificationRead = 0 || NULL --> IF GetRead = 0
NotificationRead = 1 --> IF GetRead = 1
NotificationRead = 0 || 1 || NULL --> IF GetRead = NULL
以下是我对此使用的查询:

DECLARE @GetRead BIT
DECLARE @Query VARCHAR(20)

SET @GetRead = NULL

IF @GetRead = 0 SET @Query = '0,NULL'
ELSE IF @GetRead = 1 SET @Query = '1'
ELSE SET @Query = '0,1,NULL'

SELECT * FROM vwNotifications  WHERE NotificationRead IN (@Query)
当我在
in
子句中提供
NULL
时,上述查询基本上失败。 我知道为什么要感谢你

但是,如果我采取该问题答案中建议的方法(使用
NotificationRead-in(@Query)或NotificationRead为NULL
),我会在不需要它们的情况下(例如
@GetRead=1


您能给我指出正确的方向吗?

不,问题是您提供的值是字符串

1 IN (1, 2) -- true.
1 IN ('1, 2') -- false.
请尝试以下方法:

SELECT *
FROM vwNotifications 
WHERE (@GetRead = 0 AND (NotificationRead = 0 OR NotificationRead IS NULL))
OR    (@GetRead = 1 AND NotificationRead = 1)
OR    (@GetRead IS NULL AND (NotificationRead IN (0, 1) OR 
                             NotificationRead IS NULL))

其他人可能有更好的方法,但我会将您的select移到每个“if”语句中,并进行相应的修改

IF @GetRead = 0
    SELECT * FROM vwNotifications WHERE NotificationRead IS NULL or NotificationRead = 0
ELSE IF @GetRead = 1 
    SELECT * FROM vwNotifications WHERE NotificationRead = 1
ELSE
    SELECT * FROM vwNotifications WHERE NotificationRead IS NULL or NotificationRead in 0, 1
注意:请不要在生产代码中实际使用
SELECT*
。命名您的列。

有几个选项:

DECLARE @T TABLE (ID INT IDENTITY(1,1), Notify INT)
DECLARE @GetRead BIT
SET @GetRead = 0

INSERT INTO @T
        ( Notify )
VALUES  ( 0  -- Notify - int
          )

INSERT INTO @T
        ( Notify )
VALUES  ( 1  -- Notify - int
          )

INSERT INTO @T
        ( Notify )
VALUES  ( NULL  -- Notify - int
          )

SELECT *
FROM @t t
WHERE @GetRead IS NULL 
    OR (@GetRead = 0 AND (t.Notify =0 OR t.Notify IS NULL))
    OR (@getRead = 1 AND t.Notify = @GetRead)

SELECT *
FROM @t t
WHERE @GetRead IS NULL 
    OR ISNULL(t.notify, 0) = @GetRead

您可以将SETANSI NULLS添加到查询的顶部。添加NULL后,可以在(NULL)子句中使用


您不能在
中的
中使用
NULL
,因为
NULL
s不能与
SQL Server
上下文中的其他值/变量进行比较。对于exmaple,以下语句将不返回任何内容:
SELECT*FROM[dbo].[TEST],其中[TestColumn]位于(NULL)
SET ANSI NULLS OFF
SELECT * ......
....
WHERE UserId IN (1, 2, NULL)