2014-01-08 5 views
0

Я пытаюсь добавить вычисленный столбец в таблицу SQL Server 2008 Express.Вычисленный столбец - Неправильный синтаксис рядом с ключевым словом 'when'

Формула:

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

Try:

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 может быть в case. Look 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

Удалите Чужая затем повторите попытку.

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 затем «Прямой входящий голос» и т.д.

Смежные вопросы