2014-03-14 67 views
0

這裏是我的代碼:爲什麼SQL Server無法正確處理代碼?

Select 
    cname, 
    case 
     WHEN cid < 35 then 'Failed' 
     WHEN cid > 35 AND < 50 then 'Below Average' 
     WHEN cid > 50 AND < 60 then 'Average' 
     WHEN cid > 60 AND < 70 then 'Good' 
     WHEN cid > 70 AND < 85 then 'Distinction' 
     WHEN cid > 85 then 'Outstanding' 
    end as Report 
from cus 

我不知道什麼是錯誤的與上面的代碼。它沒有得到正確執行。

它顯示的結果是這樣的。

消息102,級別15,狀態1,行3
附近有語法錯誤<「。

我可以知道我犯了什麼錯誤,以及如何克服它嗎?

回答

2

的情況下更改爲

Select cname, case 
WHEN cid < 35 then 'Failed' 
When cid >35 and cid<50 then 'Below Average' 
WHEN cid >50 and cid<60 then 'Average' 
WHEN cid >60 and cid<70 then 'Good' 
WHEN cid >70 and cid<85 then 'Distinction' 
WHEN cid >85 then 'Outstanding' end as Report from cus 

你忘了第二個條件指定cid

作爲一方不是,如果它是50是什麼情況?

您可能想要將其更改爲包容性/排他性cluases。像

Select cname, case 
WHEN cid <= 35 then 'Failed' 
When cid >35 and cid<=50 then 'Below Average' 
WHEN cid >50 and cid<=60 then 'Average' 
WHEN cid >60 and cid<=70 then 'Good' 
WHEN cid >70 and cid<=85 then 'Distinction' 
WHEN cid >85 then 'Outstanding' end as Report from cus 
+0

謝謝astander – NandhagaM

0

東西試試這個

Select 
    cname, 
    case 
     WHEN cid < 35 then 'Failed' 
     WHEN cid > 35 AND cid < 50 then 'Below Average' 
     WHEN cid > 50 AND cid < 60 then 'Average' 
     WHEN cid > 60 AND cid < 70 then 'Good' 
     WHEN cid > 70 AND cid < 85 then 'Distinction' 
     WHEN cid > 85 then 'Outstanding' 
    end as Report 
from cus 
相關問題