gpt4 book ai didi

sql-server - 在 CASE 语句中使用两个 = 符号?

转载 作者:行者123 更新时间:2023-12-02 06:26:51 24 4
gpt4 key购买 nike

我正在尝试使用依赖于变量的 case 语句来确定我的 WHERE 子句的一部分,但它不喜欢我正在做的事情。这是我的查询(为简单起见进行了修改)

DECLARE @SalesType VARCHAR(10)

SET @SalesType = 'Bulk'

SELECT CASE
WHEN fwsf.Customer_Cardlock_Customer = 'True'
THEN 'CFN'
ELSE 'Bulk'
END AS 'Prod Type'
FROM TABLE t
WHERE CASE
WHEN @SalesType = 'Bulk' then t.customer_type = 'False'
WHEN @SalesType = 'CFN' then t.customer_type = 'True'
End

换句话说,我想在 WHERE 子句中声明当@SalesType 是给定值时,选择具有另一个值的行。

编辑:为了其他人,我意识到我有另一种情况,我可能需要选择“全部”选项。下面的 Per Shawn,他用我验证过的以下内容更正了我最初提出的解决方案:

AND t.customer_type =
CASE @SalesType
WHEN 'Bulk' then 'False'
WHEN 'CFN' then 'True'
WHEN 'All' then t.customer_type
END
END

最佳答案

您可以将 WHERE 子句写得更简单一些:

where 
(@SalesType = 'Bulk' and t.customer_type = 'False')
OR
(@SalesType = 'CFN' and t.customer_type = 'True')
OR
@SalesType = 'All'

关于sql-server - 在 CASE 语句中使用两个 = 符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57485089/

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