如何使用case语句将vales插入表中 [英] How to use case statement for inserting vales into table

查看:86
本文介绍了如何使用case语句将vales插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此查询插入但是语法错误...





 < span class =code-keyword> insert   into #tmp1 
select strcallDi,
case strcallDi = A
然后 strDescrip = ' Origi'
case strcallDi = b
然后 strDescrip = < span class =code-string>' Termi'
case < span class =code-keyword>当 strcallDi = c
然后 strDescrip = ' Inco'
case strcallDi = d
然后 strDescrip = ' Outg'
else
unknown
end
来自选择 distinct strcallDi From B) As A
其中 strcallDi 选择 strCallDirection 来自 dbo。#tmp1)





帮助

解决方案

你正在重复关键字案例



以下是如何使用 case



  SELECT   CASE   WHEN  ColumnA =  1   THEN  '  A'  WHEN  ColumnA =  2  那么 '   B'  ELSE  '  C'  END  





虽然如果您只检查单个值而不是多个表达式,则以下内容也有效。



  SELECT   CASE  ColumnA  WHEN   1  那么 '  A'  WHEN   2  那么 '  B'  ELSE  '  C'  END  


I am using this query to insert but getting syntax errors ...


insert into #tmp1
select strcallDi,
case when strcallDi=A
then strDescrip='Origi'
case when strcallDi=b
then strDescrip='Termi'
case when strcallDi=c
then strDescrip='Inco'
case when strcallDi=d
then strDescrip='Outg'
else
unknown
end
from (Select distinct strcallDi From B) As A
Where strcallDi not in (Select strCallDirection From dbo.#tmp1)



Help

解决方案

you're repeating the keyword case

The following is how you use case

SELECT CASE WHEN ColumnA = 1 THEN 'A' WHEN ColumnA = 2 THEN 'B' ELSE 'C' END



Although if you're only checking a single value and not multiple expressions the following is also valid.

SELECT CASE ColumnA WHEN 1 THEN 'A' WHEN 2 THEN 'B' ELSE 'C' END


这篇关于如何使用case语句将vales插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆