读书人

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

发布时间: 2012-05-01 12:48:58 作者: rapoo

求一条插入语句,高手!

字符串 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) 

读书人网 >SQL Server

热点推荐