回帖时怎么把自己写的sql代码用sql code回复:字体颜色不变,格式不变.我把代码复制到上面格式和颜色都变了.用插入源代码sql也不行??在线等

解决方案 »

  1.   


    use demo
    go
    create table arrays
    (
    aid int not null identity primary key,
    array varchar(7999) not null
    )
    go
    --add data to table
    insert into arrays values('')
    insert into arrays values('10')
    insert into arrays values('20,30,40')
    insert into arrays values('-1,-2,-3')
    go
    --create function 
    create function function2(@arr as varchar(7999))
    returns @t table(pos int not null,value int not null)
    as
    begin
    declare @end as int,@start as int,@pos as int
    --在@arr中搜索','并返回起始位置..start是从那个位置搜索,,如果是0或是负数就从@arr的开头搜索
    select @arr=@arr+',',@pos=1,@start=1,@end=CHARINDEX(',',@arr,@start)
    while @end>1
    begin
    insert into @t values(@pos,SUBSTRING(@arr,@start,@end-@start))
    select @pos=@pos+1,
    @start=@end+1,@end=CHARINDEX(',',@arr,@start)
    end
    return
    end
    go
    --test
    select * from function2('200,400,300')
    go