【发布时间】:2016-10-20 18:41:41
【问题描述】:
你能教我在我的情况下我做错了什么吗? :)
我正在选择发票和还款。
错误:
消息 4145,第 15 级,状态 1,第 10 行
在预期条件的上下文中指定的非布尔类型表达式,靠近“结束”。
代码:
select
I.subject1, R.subject2
from
dbo.invoice I
left join
dbo.repayments R on I.subject1 = R.subject2
where
case
when R.subject2 is not null and R.remains_due > 0
then R.remains_due
when R.subject2 is not null and R.remains_due = 0
then I.remains_due
when R.subject2 is not null and R.due_date <= GETDATE()
then R.due_date
when R.subject2 is null and I.due_date <= GETDATE()
then I.due_date
end
【问题讨论】:
-
在
WHERE子句中使用AND/OR而不是case表达式通常会更好。 -
你的 case 表达式返回一个值,现在你需要将它与某物进行比较。
-
我需要“case”,因为“then”对我很重要。你说的比较?你能告诉我更多吗?
-
您可能根本不需要“then”...是的,您的 case 表达式返回一个值。该怎么办?
-
那我如何告诉程序在没有“then”的情况下获取特定属性?
标签: sql sql-server tsql