字符串 str =  '11','12','113','141','151'数据表A
字段B
11
44
151求一条语句插入 字符串不在表中的数据表A结果
11
44
151
12
113
141
一条语句。求简单,求经典

解决方案 »

  1.   

    sqlserver字符串拆分(split)方法汇总
      

  2.   

    动态sql,结合insert into select 语句。
      

  3.   


    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)