IIF在SQL Server 2012中遇到麻烦

时间:2016-05-11 22:36:52

标签: sql sql-server

我尝试在SQL Server 2012管理工作室中使用IIF(连接到SQL Server 2012)。我可以在Access中生成我需要的东西,但SQL Server中的语法正在绊倒我。甚至像

那样简单
SELECT IIF(dbo.GL_Register.Credit_Or_Debit = C,'true','false')
from dbo.GL_Register

应该工作但会带来以下错误:"列表函数参数出错:' ='未识别。无法解析查询文本。

在Access中这很容易做到。我究竟做错了什么?谢谢。

更新了,使用"等等#34;而不是等于。然后它说" FROM"是个问题:

SELECT dbo.GL_Account.Account_Code, dbo.GL_Register.Date,     
SUM(IIF(dbo.GL_Register.Credit_Or_Debit like 'C',-  
dbo.GL_Register.Amount,dbo.GL_Register.Amount) AS Expr1
FROM dbo.GL_Register INNER JOIN
dbo.GL_Account ON dbo.GL_Register.Account_Id = dbo.GL_Account.Account_Id
GROUP BY dbo.GL_Account.Account_Code, dbo.GL_Register.Date
HAVING (dbo.GL_Account.Account_Code = N'11000')

像魅力一样工作的Access SQL是:

SELECT dbo_GL_Account.Account_Code, dbo_GL_Register.Date,    
Sum(IIf([Credit_Or_Debit]="C",-[Amount],[Amount])) AS Amount2
FROM dbo_GL_Register INNER JOIN dbo_GL_Account ON dbo_GL_Register.Account_Id   
= dbo_GL_Account.Account_Id
GROUP BY dbo_GL_Account.Account_Code, dbo_GL_Register.Date
HAVING (((dbo_GL_Account.Account_Code)="11000"));

2 个答案:

答案 0 :(得分:2)

使用

SELECT case when dbo.GL_Register.Credit_Or_Debit = 'C' 
            then 'true' 
            else 'false'
       end
from dbo.GL_Register

答案 1 :(得分:1)

语法错误是因为“C”

附近缺少引号

我运行了类似的查询并且工作正常。

SELECT IIF(dbo.GL_Register.Credit_Or_Debit = 'C','true','false')
from dbo.GL_Register