gpt4 book ai didi

SQL Case 语句在 where 子句中指定条件?

转载 作者:行者123 更新时间:2023-12-01 08:17:49 25 4
gpt4 key购买 nike

我有以下查询:

    SELECT * 
FROM dbo.tblOrders o
WHERE o.OrderId IN (SELECT [Value] FROM [dbo].[udf_GenerateVarcharTableFromStringList](@OrderId, ','))
AND
@ActiveInactive =
CASE
WHEN 'Active' THEN (o.[orderactivedate] > o.[orderinactivedate])
WHEN 'Inactive' THEN (o.[orderactivedate] < o.[orderinactivedate])
END

这返回
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.

我将如何让它发挥作用?说如果参数是“事件”然后返回具有以下条件的记录?

最佳答案

你可以用另一种方式做到这一点:

SELECT * 
FROM dbo.tblOrders o
WHERE o.OrderId IN (SELECT [Value] FROM [dbo].[udf_GenerateVarcharTableFromStringList](@OrderId, ','))
AND ((@ActiveInactive = 'Active' AND o.[orderactivedate] > o.[orderinactivedate])
OR (@ActiveInactive = 'Inactive' AND o.[orderactivedate] < o.[orderinactivedate]))

关于SQL Case 语句在 where 子句中指定条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5939225/

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