你要做什么?看上去感觉不需要用循环,row_number()应该能够处理你的问题

解决方案 »

  1.   

    http://blog.csdn.net/wufeng4552/article/details/4534365
      

  2.   

    create table #table1(
    os_no varchar(100) null,
    pn_po varchar(500) null
    )
    create table #last(
    os_no varchar(100) null,
    pn_po varchar(500) null
    )
    create table #linshi(
    os_no varchar(100) null,
    pn_po varchar(500) null
    )insert into  #table1
    select 'SOBZ111121LOG01','PO-138490' union
    select 'SOBZ130819ZAL01','PO-1006818/PO-1006820/PO-1006823/PO-1006825' union
    select 'SOBZ130517FRE01','PO-1004245/PO-1004248/PO-1004426/PO-1004661' select * From #table1declare @i int,@max int
    set @i=1
    select @max=max(LEN(pn_po)) from #table1
    while @i<@max
    begin insert into #last
    select * from #table1 where CHARINDEX('/',pn_po)=0
    delete #table1 where CHARINDEX('/',pn_po)=0 insert into #last
    select os_no,substring(pn_po,1,CHARINDEX('/',pn_po)-1) from #table1 insert into #linshi
    select os_no,substring(pn_po,CHARINDEX('/',pn_po)+1,LEN(pn_po)) from #table1 truncate table #table1
    insert into #table1
    select * from #linshi
    truncate table #linshi
    select @i=@i+1
    endselect * from #last
    结果: