Question

I'm trying to add a computed column to a SQL Server 2008 Express table.

The formula is:

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

But I'm getting the error:

Incorrect syntax near the keyword 'when'.

Était-ce utile?

La solution

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

See http://msdn.microsoft.com/en-us/library/ms181765.aspx for syntax.

Autres conseils

Have only 1 ELSE in your query:

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

else only belongs on the final, non-conditional clause:

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

Only one else can be in 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

Remove the Else's then try again.

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
SELECT
    CASE @TestVal
    WHEN 1 THEN 'First'
    WHEN 2 THEN 'Second'
    WHEN 3 THEN 'Third'
    ELSE 'Other'
END

Probably you can try changing it to case callrecord_contacttype when 1 then 'Routed voice' when 2 then 'Direct incoming voice' etc

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top