2

如何修改最终脚本以使其也具有排除开关?我不想EXISTS在最终脚本中使用:

SELECT MyField = 'x' INTO #Data
UNION SELECT MyField = 'y'
UNION SELECT MyField = 'z'
UNION SELECT MyField = 'j'
UNION SELECT MyField = 'q'
UNION SELECT MyField = NULL;

SELECT MyField = 'j' INTO #Exclusions
UNION  SELECT MyField = 'q'
UNION SELECT MyField = NULL;


DECLARE @ShowAll TINYINT = 1; -->>exceptions "switch" is turned ON so all data returned
SELECT *
FROM   #Data d
WHERE  @ShowAll = 1 OR 
       (
       EXISTS
         (
         SELECT d.MyField 
         EXCEPT
         SELECT MyField FROM #Exclusions
         )
       );

--can I simplify and get rid of the EXISTS but still include a switch based on @ShowAll ? 
SELECT *
FROM   #Data d
       EXCEPT
       SELECT MyField FROM #Exclusions;
4

2 回答 2

3
SELECT *
FROM   #Data d
       EXCEPT
       SELECT MyField FROM #Exclusions WHERE @ShowAll <> 1;

http://sqlfiddle.com/#!6/d41d8/3125

于 2013-03-12T19:57:01.310 回答
0

将其包装在存储过程中并将开关作为选项传递。

于 2013-03-12T19:53:46.697 回答