我试图根据我的查询从表中检索一些记录,但它显示了一个错误
消息102,级别15,状态1,行2
'>

select vd.LedgerId,(CreditAmt-DebitAmt) AS NET, CASE NET
WHEN NET > 0 THEN 'Debit' WHEN NET < 0 THEN 'Credit' ELSE 'Nil'End
from dbo.vdebit vd INNER JOIN dbo.vCredit vc  ON vd.LedgerId=vc.LedgerId

最佳答案

多亏了wikser和trey,我遇到了一些语法问题,他们引起了我的注意,之后我的查询仍然有问题。
Issue was-net是我的列的别名,当我在case语句中检查它时,它无法找到同一列。
错误

select vd.LedgerId,(CreditAmt-DebitAmt) AS NET,
CASE
   WHEN NET > 0 THEN 'Debit'
   WHEN NET < 0 THEN 'Credit'
   ELSE 'Nil'
End
AS NetVal
from dbo.vdebit vd INNER JOIN dbo.vCredit vc  ON vd.LedgerId=vc.LedgerId

修正
select vd.LedgerId,(CreditAmt-DebitAmt) AS NET,
CASE
   WHEN (CreditAmt-DebitAmt) > 0 THEN 'Debit'
   WHEN (CreditAmt-DebitAmt) < 0 THEN 'Credit'
   ELSE 'Nil'
End
AS NetVal
from dbo.vdebit vd INNER JOIN dbo.vCredit vc  ON vd.LedgerId=vc.LedgerId

关于sql-server-2005 - 如何在SQL中使用CASE,显示语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2703254/

10-12 05:23