0

我最近发布了一个关于如何在 SQL Server Reporting Services 中处理 NULL 值的问题。该问题位于处理 SQL Server Reporting Services 中的 NULL 值

应用WHERE @MyParam=MyCol OR @MyParam IS NULL面临一个问题,如果我选择单个 NON-NULL 值(从下拉列表中并在 SQL Server 中传递它),这也会带来 NULL 值记录。它应该只带来非 NULL 值的记录。

如果选择了 NON-NULL 记录,应该如何避免 NULL 记录?

4

1 回答 1

0

您正在寻找的是异或(XOR):

-- Exclusive OR is an either/or combination which, 
-- using => as "is defined as" symbol, can be shown as:
-- 
-- x XOR y => (x AND (NOT y)) OR ((NOT x) AND y)

-- x => @MyParam = MyCol  
-- y => @MyParam IS NULL



-- DECLARE @MyParam NVARCHAR(32) = 'MC1,MC2'; -- returns two lines
-- DECLARE @MyParam NVARCHAR(32) = 'MC1';     -- returns one line
   DECLARE @MyParam NVARCHAR(32) =  NULL;     -- returns one line


DECLARE @MyParamXML XML = N'<root><r>' + replace(@MyParam,',','</r><r>') + '</r></root>'

;WITH   SOExample AS
(
    SELECT cast('MC1' AS NVARCHAR(12)) AS MyCol, 'Line 1'    AS MyCol2 UNION ALL
    SELECT      'MC2'                          , 'Line 2'              UNION ALL
    SELECT       NULL                          , 'Line NULL'
)
SELECT * from SOExample 
WHERE
       (@MyParam IS NULL     AND ISNULL(MyCol,'<NULL Value>') = '<NULL Value>') 
    OR (@MyParam IS NOT NULL AND 
        SOExample.MyCol IN 
            (SELECT SSRSlist.MyParams.value('.','NVARCHAR(32)') as MyParam 
             FROM   @MyParamXML.nodes('//root/r') as SSRSlist(MyParams)))
于 2012-10-30T10:00:28.817 回答