SQL Case 语句在 where 子句中指定条件?
我有以下查询:
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'.
How will I get this to work?说如果参数为“Active”,则返回符合以下条件的记录?
I have the the following query:
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
This returns
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
How would I get this to work? saying if the parameter is 'Active' then return records with the following criteria?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以用另一种方式来做:
You could do it an alternate way:
为什么不添加一个 OR 条件呢?喜欢
Why don't you add an OR condition? like
您不能在 CASE 中使用比较表达式,就像
我会考虑使用此选项将比较更改为普通表达式
或者这样,您可以在
SIGN()
表达式上有一个计算列You can't have an comparison expression in a CASE like that
I'd consider this option which change the comparison to a normal expression
Or this, and you can have a computed column on the
SIGN()
expression