当前位置: 代码迷 >> Sql Server >> 求一条插入语句,高手!该怎么解决
  详细解决方案

求一条插入语句,高手!该怎么解决

热度:11   发布时间:2016-04-27 13:07:12.0
求一条插入语句,高手!

字符串 str = '11','12','113','141','151'

数据表A
字段B
11
44
151

求一条语句插入 字符串不在表中的数据

表A结果
11
44
151
12
113
141


一条语句。求简单,求经典

------解决方案--------------------
sqlserver字符串拆分(split)方法汇总
------解决方案--------------------
动态sql,结合insert into select 语句。
------解决方案--------------------
SQL code
declare @s varchar(100)set @s = '11,12,113,141,151' -- 去除单引号;with t as( select   vt=(case when charindex(',',@s)>0 then substring(@s, charindex(',',@s)+1, len(@s)) else '' end),   st=(case when charindex(',',@s)>0 then left(@s, charindex(',',@s)-1) else @s end)  union all select   vt=(case when charindex(',',t.vt)>0 then substring(t.vt, charindex(',',t.vt)+1, len(t.vt)) else '' end),   st=(case when charindex(',',t.vt)>0 then left(t.vt, charindex(',',t.vt)-1) else t.vt end)  from t where len(t.vt)>0)insert into 表A(字段B)select st from t where t.st not in(select 字段B from 表A)
  相关解决方案