--字符串分割表函数
declare @str varchar(1000)
declare @split varchar(10)
declare @i int;
declare @count int;
declare @ChildStr varchar(1000);
declare @splitStr varchar(1000);
declare @Index int;
declare @table as table (rowId int,splitStr varchar(1000))
declare @rowID int;
set @str='aaa;bbb;ccc;d;';
set @split=';';
set @i=1;
set @count=LEN(@str);
set @ChildStr=@str;
set @rowID=1;
while @i<=@count
begin
set @Index= charindex(@split,@ChildStr);
if @Index >0
begin
set @splitStr=SUBSTRING(@ChildStr,0,@Index);
insert @table
select @rowID,@splitStr
set @ChildStr=SUBSTRING(@ChildStr,@Index+1,LEN(@ChildStr));
set @rowID=@rowID+1;
set @i=@i+1;
end
else if LEN(@ChildStr)>0
begin
insert @table
select @rowID,@ChildStr
--select * from @table
return;
end
else
begin
--select * from @table
return;
end
end