2014-01-08 21 views
0

我想將計算列添加到SQL Server 2008 Express表。計算列 - 關鍵字'附近時語法不正確'

其計算公式爲:

case when callrecord_contacttype=1 then 'Routed voice' 
else when callrecord_contacttype=2 then 'Direct incoming voice' 
else when callrecord_contacttype=3 then 'Direct outgoing voice' 
else when callrecord_contacttype=4 then 'Direct internal voice' 
else when callrecord_contacttype=5 then 'Routed callback' 
else when callrecord_contacttype=6 then 'Routed email' 
else when callrecord_contacttype=7 then 'Direct outgoing email' 
else when callrecord_contacttype=8 then 'Routed chat' else '' end 

但我得到的錯誤:

Incorrect syntax near the keyword 'when'.

回答

4

嘗試:

case callrecord_contacttype 
    when 1 then 'Routed voice' 
    when 2 then 'Direct incoming voice' 
    when 3 then 'Direct outgoing voice' 
    when 4 then 'Direct internal voice' 
    when 5 then 'Routed callback' 
    when 6 then 'Routed email' 
    when 7 then 'Direct outgoing email' 
    when 8 then 'Routed chat' 
    else '' 
end 

http://msdn.microsoft.com/en-us/library/ms181765.aspx語法。

3

只有1 ELSE在您的查詢:

case when callrecord_contacttype=1 then 'Routed voice' 
when callrecord_contacttype=2 then 'Direct incoming voice' 
when callrecord_contacttype=3 then 'Direct outgoing voice' 
when callrecord_contacttype=4 then 'Direct internal voice' 
when callrecord_contacttype=5 then 'Routed callback' 
when callrecord_contacttype=6 then 'Routed email' 
when callrecord_contacttype=7 then 'Direct outgoing email' 
when callrecord_contacttype=8 then 'Routed chat' 
else '' end 
2

else只屬於最終,非條件子句:

case when callrecord_contacttype=1 then 'Routed voice' 
    when callrecord_contacttype=2 then 'Direct incoming voice' 
    when callrecord_contacttype=3 then 'Direct outgoing voice' 
    when callrecord_contacttype=4 then 'Direct internal voice' 
    when callrecord_contacttype=5 then 'Routed callback' 
    when callrecord_contacttype=6 then 'Routed email' 
    when callrecord_contacttype=7 then 'Direct outgoing email' 
    when callrecord_contacttype=8 then 'Routed chat' 
    else '' end 
2

只有一個else可以在caseLook at examples

case when callrecord_contacttype=1 then 'Routed voice' 
    when callrecord_contacttype=2 then 'Direct incoming voice' 
    when callrecord_contacttype=3 then 'Direct outgoing voice' 
    when callrecord_contacttype=4 then 'Direct internal voice' 
    when callrecord_contacttype=5 then 'Routed callback' 
    when callrecord_contacttype=6 then 'Routed email' 
    when callrecord_contacttype=7 then 'Direct outgoing email' 
    when callrecord_contacttype=8 then 'Routed chat' 
else '' end 
1

刪除Else,然後再試一次。

case when callrecord_contacttype=1 then 'Routed voice' 
when callrecord_contacttype=2 then 'Direct incoming voice' 
when callrecord_contacttype=3 then 'Direct outgoing voice' 
when callrecord_contacttype=4 then 'Direct internal voice' 
when callrecord_contacttype=5 then 'Routed callback' 
when callrecord_contacttype=6 then 'Routed email' 
when callrecord_contacttype=7 then 'Direct outgoing email' 
when callrecord_contacttype=8 then 'Routed chat' else '' end 
1
SELECT 
    CASE @TestVal 
    WHEN 1 THEN 'First' 
    WHEN 2 THEN 'Second' 
    WHEN 3 THEN 'Third' 
    ELSE 'Other' 
END 

也許你可以嘗試將其更改爲 情況下callrecord_contacttype 1時,然後 '選路聲音' 2時,那麼 '直接傳入的聲音' 等

相關問題