gpt4 book ai didi

sql - 带走 EXISTS 但留在开关中

转载 作者:行者123 更新时间:2023-12-04 05:07:19 25 4
gpt4 key购买 nike

如何修改最终脚本以也有一个排除开关?我不想使用 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;

最佳答案

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

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

关于sql - 带走 EXISTS 但留在开关中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15370812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com