2012-11-20 38 views
3

我來自MySQL後臺,請在這裏忍受我。使用MSSQL Server 2008,我正在編寫一個查詢,其中包含IFF語句。我收到以下錯誤。IFF聲明錯誤'='附近的語法不正確

"Msg 102, Level 15, State 1, Line 8 
Incorrect syntax near '='." 

我的查詢看起來是這樣的:

SELECT 
    a.[storeno], 
    a.[Description], 
    a.[transactiondate], 
    a.[amount] AS 'pos', 
    b.[amount] AS 'ecc', 
    a.[amount] - b.[amount] AS 'difference', 
    IIF(a.amount = b.amount,'BALANCED','UNBALANCED') AS 'result', 
    GETDATE() 
FROM 
    [POS_REPORT].[dbo].[Txn_Daily_Totals] a 
LEFT JOIN 
    [POS_REPORT].[dbo].[SAP_FI_INBOUND_DAILY_TOTALS] b ON 
    a.[storeno] + 
    a.[transactiondate] = 
    b.[storeno] + 
    b.[transactiondate] 

我已經嘗試了很多不同的方式,並不能得到這個權利。我已使用

IF 
BEGIN 
END 
ELSE 
END 

和案例。所有給我不同的錯誤。請幫幫我。

+0

可能重複的[你如何在SQL SELECT中執行IF ... THEN?](http://stackoverflow.com/questions/63447/how-do-you-perform-an-if-then- in-an-sql-select) – hims056

回答

3

你可以寫這樣的:的

case when a.amount = b.amount then 'BALANCED' else 'UNBALANCED' end as result 

代替

IIF(a.amount = b.amount,'BALANCED','UNBALANCED') AS 'result' 
+0

謝謝你,完美的工作。 – Skyboard

2

IIF沒有在SQL Server 2008中實現的。它是在2012版的新功能。

相關問題