li.的博客
li.的首页 > li.的博客 > 浏览文章

sqlserver 字符串分割函数和数据记录循环操作

分类:sql server  人气:1587  评论:0  时间:2011-01-21 15:13

create function dbo.fn_split
(
@inputstr varchar(8000),
@seprator varchar(10)
)
returns @temp table (a varchar(200))
as
begin
declare @i int
set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex(@seprator, @inputstr)
while @i >= 1
begin
insert @temp values(left(@inputstr, @i - 1))
set @inputstr = substring(@inputstr, @i + 1, len(@inputstr) - @i)
set @i = charindex(@seprator, @inputstr)
end
if @inputstr <> '\'
insert @temp values(@inputstr)
return
end
go
select * from dbo.fn_split(‘10、20、30’,'、')

declare cursor_insert cursor for select col1 from [stable1]
declare @i nvarchar(200)
open   cursor_insert
fetch   cursor_insert   into   @i
while   @@fetch_status=0
begin
    --print   @i
insert into [table1]
select * from dbo.fn_split(@i,'、')
    fetch   cursor_insert   into   @i
end
close   cursor_insert
deallocate   cursor_insert

评论(0)
暂无评论
我来评论
(800字以内)