把用","分隔的字符串转化成Table的一列
create function dbo.ufn_ConvertToIntList
(
 @data varchar(7990)
)
returns @table Table(c int not null)
as
begin
    declare @str varchar(8000),@iter varchar(50)
            ,@beginPos int, @endPos int
    select @str= @data + ',', @beginPos = 1, @endPos = charindex(',', @str, @beginPos)
    while(@endPos > 0)
    begin
        set @iter = SubString(@str,@beginPos ,@endPos - @beginPos )
        if(@iter <> '')
        begin 
            insert into @table(c) values(Convert(int,@iter))
        end
        select @beginPos = @endPos + 1, @endPos = charindex(',', @str, @beginPos)
    end
    return
end
go

declare @data varchar(8000)
        ,@i int
    select @i = 1, @data ='1,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9'
while(@i < 20)
begin
    select * from dbo.ufn_ConvertToIntList(@data)
    set @i= @i +1
end
 
阅读更多
个人分类: SQL SERVER
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

把用","分隔的字符串转化成Table的一列

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭