几天都没有解决的问题,请教.谢谢
现有一表 "productstructuresEX "为产品结构表. 
 假如有两产品结构为:   
                            一个A1需要两个B三个C,一个B需要两个D; 
                            一个A2需要一个B两个C,一个B需要两个D     
 则在表里列示为:   
 autoid            cpspcode         cpscode         ipsquantity    
 1                              A1                              B                              2 
 2                              A1                              C                              3 
 3                              B                                 D                              2 
 4                              A2                              B                              1 
 5                              A2                              C                              2     
 现有一代码计算子件的需求数:   
 begin 
             declare   @i   int,@ret   varchar(8000) 
             set   @i   =   1 
             select   cpspcode,cpscode,ipsquantity   as   ipsquantity,@i   as   level   into   #t    
                                  from   productstructuresEX                                    
             while   @@rowcount <> 0 
             begin 
                         set   @i   =   @i   +   1 
                         print   @i 
                         insert   into   #t    
                         select    
                                     a.cpspcode,a.cpscode,a.ipsquantity*B.ipsquantity,@i    
                         from    
                                     productstructuresEX   a,#t   b    
                         where    
                                     a.cpspcode=b.cpscode   and   b.Level   =   @i-1 
             end    
             delete   t   from   #t   t   where   exists(select   1   from   #t   where   cpspcode=t.cpscode)       
             select   cpscode,sum(ipsquantity)   as   需求数   into   #b   from   #t   GROUP   BY   cpscode      
 end