分割再成批插入
declare @s varchar(8000),@sql nvarchar(4000)
set @s='1,12,1212,4545'
set @sql='insert into t(col) select '+replace(@s,',',' col union all select ')
exec(@sql)
測試
drop table #table
declare @s varchar(8000),@sql nvarchar(4000)
set @s='1,12,1212,4545,454'
create table #table (col int)
set @sql='insert into #table(col) select '+replace(@s,',',' col union all select ')
exec(@sql)
select * from #table
create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
--實現split功能 的函數
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@StrSeprate,@SourceSql)
while @i>=1
begin
insert @temp values(left(@SourceSql,@i-1))
set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)
set @i=charindex(@StrSeprate,@SourceSql)
end
if @SourceSql<>'\'
insert @temp values(@SourceSql)
return
end
用法:select * from dbo.f_split('ABC:BC:C:D:E',':')