毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> 数据库 >> 正文

MSSQL数据如何循环

更新时间:2013-12-26:  来源:毕业论文

表结构如下:
os_no         pn_po
------------------------------------------------------------
SOBZ111121LOG01 PO-138490
SOBZ130819ZAL01 PO-1006818/PO-1006820/PO-1006823/PO-1006825
SOBZ130517FRE01 PO-1004245/PO-1004248/PO-1004426/PO-1004661
------------------------------------------------------------
查询结果格式如下:
so_no           po
------------------------------------------------------------
SOBZ130517FRE01 PO-1004245
SOBZ130517FRE01 PO-1004248
SOBZ130517FRE01 PO-1004426
SOBZ130517FRE01 PO-1004661
SOBZ111121LOG01 PO-138490
SOBZ130819ZAL01 PO-1006818
............... ..........
--------------------------------------------------------------
declare @num int
declare @i int
declare @k int
declare @f int
declare @temp table(po varchar(20),so_no varchar(20))
set @i=0
set @f=0
declare @s varchar(400)
declare @po varchar(20)
declare @so_no varchar(20)

select @num=len(pn_po)-len(replace(pn_po,'/','')),@s=pn_po,@k=charindex('/',pn_po),@so_no=os_no from mf_pos_z
where os_no='SOBZ130819ZAL01' or os_no='SOBZ130517FRE01' or os_no='SOBZ111121LOG01'
--这儿如何加一个大循环,因为下面小循环中有给变量赋值的语句,不能用游标啊
    while(@i<=@num)
      begin
        if(@k>1)
          begin
           set @po=left(@s,@k-1)
           set @s=substring(@s,@k+1,len(@s)-@k+1)
           insert into @temp(po,so_no)
           values(@po,@so_no)
          end
        else
        begin
         insert into @temp(po,so_no)
         values(@s,@so_no)
        end
         set @i=@i+1
     end
 
select * from @temp

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' unionselect 'SOBZ130819ZAL01','PO-1006818/PO-1006820/PO-1006823/PO-1006825' unionselect 'SOBZ130517FRE01','PO-1004245/PO-1004248/PO-1004426/PO-1004661'   select * From #table1   declare @i int,@max intset @i=1 select @max=max(LEN(pn_po)) from #table1 while @i<@maxbegin      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 end  select * from #last

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。