请教各位我传到存储过程中一个字符串为 ,aa,tt,dd,
我想把它在存储过程里面存成个临时表 #temp
格式为:
列名
aa
tt
dd
请教各位帮忙
------解决方案--------------------
create proc p_split
@s varchar(200)
as
create table #(col varchar(20))
declare @s1 varchar(1000)
set @s1= ' '
select @s1= 'select '+replace( ' ' ' '+stuff(@s,1,1, ' ')+ ' ' ' ', ', ', ' ' ' union all select ' ' ')
insert # exec(@s1)
select * from # where len(col)> 0
drop table #
go
exec p_split ',aa,tt,dd, '
drop proc p_split
col
--------------------
aa
tt
dd
(所影响的行数为 3 行)
------解决方案--------------------
create proc p_split
@sp varchar(200)
as
declare @s varchar(200)
declare @tem varchar(200)
declare @s1 varchar(20)
declare @i int
declare @sql nvarchar(200)
set @[email protected]
set @i=charindex( ', ',@s)
set @sql= 'create table #t( '
while @i> 0
begin
set @s1=substring(@s,1,@i-1)
set @tem=substring(@s,@i+1,datalength(@s))
set @[email protected]
set @[email protected][email protected]+ ' varchar(20) '
set @i=charindex( ', ',@s)
if @i> 0
set @[email protected]+ ', '
end
set @[email protected]+ ') '+ 'select * from #t '
exec (@sql)
go
exec p_split 'aa,tt,dd, '
drop proc p_split