参考:--N要连继,
select top 8000 identity(int,1,1) as N into numtab from
(select top 100 id=1 from sysobjects) as a,
(select top 100 id=1 from sysobjects) as b,
(select top 100 id=1 from sysobjects) as c---------------------------------------------------------------------
declare @a table (id int,string varchar(8000))
insert @a select 1 ,'a,b,c,sd,dfsdfg'
union select 2, 'a,n,sdf,we,t'
union select 3, 's,df,df'select a.*,b.*,id,substring(','+string+',',N+1,charindex(',',','+string+',',N+1)-(N+1))
from @a a,numtab b
where substring(','+string+',',N,8000) like ',_%'
order by id,N
select top 8000 identity(int,1,1) as N into numtab from
(select top 100 id=1 from sysobjects) as a,
(select top 100 id=1 from sysobjects) as b,
(select top 100 id=1 from sysobjects) as c---------------------------------------------------------------------
declare @a table (id int,string varchar(8000))
insert @a select 1 ,'a,b,c,sd,dfsdfg'
union select 2, 'a,n,sdf,we,t'
union select 3, 's,df,df'select a.*,b.*,id,substring(','+string+',',N+1,charindex(',',','+string+',',N+1)-(N+1))
from @a a,numtab b
where substring(','+string+',',N,8000) like ',_%'
order by id,N
create function getstrofindex (@str varchar(8000),@index int =0)
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @start int
declare @next int
declare @location int
select @start =1
select @next =1
select @location = charindex(' ',@str,@start)
while (@location <>0 and @index > @next )
begin
select @start = @location +1
select @location = charindex(' ',@str,@start)
select @next =@next +1
end
if @location =0 select @location =len(@str)+1
select @str_return = substring(@str,@start,@location -@start)
if (@index <> @next ) select @str_return = ''
return @str_return
end
go
select dbo.getstrofindex('fdsd ds sddsdfs dfd',3)
1,
--N要连继,
select top 8000 identity(int,1,1) as N into numtab from
(select top 100 id=1 from sysobjects) as a,
(select top 100 id=1 from sysobjects) as b,
(select top 100 id=1 from sysobjects) as c
2,declare @a table (id int,string varchar(8000))
insert @a select 1 ,'a b c sd dfsdfg'select a.*,b.*,id,substring(' '+string+' ',N+1,charindex(' ',' '+string+' ',N+1)-(N+1))
from @a a,numtab b
where substring(' '+string+' ',N,8000) like ' _%'
order by id,N
--创建一个用空格来分拆字符串的函数
create function fsplit(@str varchar(8000))
returns @re table(istr varchar(50))
as
begin
declare @i int
set @i=charindex(' ',@str)+1
while @i>1
begin
insert into @re values(left(@str,@i-2))
select @str=substring(@str,@i,8000)
,@i=charindex(' ',@str)+1
end
if @str<>'' insert into @re values(@str)
return
end
go--调用这个自定义函数来完成字符串分拆
declare @d varchar(50)
select @d='fdsd ds sddsdfs dfd'
select * from dbo.fsplit(@d)
set @string='fdsd ds sddsdfs dfd'
set @string='select '''+replace(@string,' ','''as str union all select ''')+''''
exec(@string)
不就可以了?
你要得到:create table #结果 (a varchar(1000))declare @string varchar(8000)
set @string='fdsd ds sddsdfs dfd'
set @string='insert #结果 select '''+replace(@string,' ','''as str union all select ''')+''''
exec(@string)select * from #结果