declare @a table(出口规格 varchar(30),原料重量 decimal(10,3),入口重量 decimal(10,3),出口重量 decimal(10,3))
insert @a select '0.47*1003*C',14.45,14,13.715
union all select '0.47*1003*C',14.33,13.96,13.55
union all select '0.47*1003*C',14.185,13.795,13.95
union all select '0.47*1003*C',14.335,13.93,13.665
union all select '0.5*1003*C',14.33,14.015,13.37
union all select '0.5*1003*C',17.98,17.6,17.37select * from @a现在想列出“出口规格=0.47*1003*C” 并且当求和后的出口重量=< 200 时停止。

解决方案 »

  1.   

    declare @a table(出口规格 varchar(30),原料重量 decimal(10,3),入口重量 decimal(10,3),出口重量 decimal(10,3))
    insert @a select '0.47*1003*C',14.45,14,200.715
    union all select '0.47*1003*C',14.33,13.96,40.55
    union all select '0.47*1003*C',14.185,13.795,60.95
    union all select '0.47*1003*C',14.335,13.93,20.665
    union all select '0.5*1003*C',14.33,14.015,13.37
    union all select '0.5*1003*C',17.98,17.6,17.37--select * from @adeclare @tmp table(序号 int identity(1,1),出口规格 varchar(30),原料重量 decimal(10,3),入口重量 decimal(10,3),出口重量 decimal(10,3))declare @Count int,
    @XH int,
    @HJ decimal(12,3)Set @HJ=0
    Set @XH=1insert @tmp
    select 出口规格,原料重量,入口重量,出口重量  from @a
    where 出口规格='0.47*1003*C'Set @Count=@@RowCountwhile @Count>0
    begin
    select @HJ=@HJ+出口重量 from @tmp where 序号=@XH
    if(@HJ>200)
    begin
    select @HJ=@HJ-出口重量 from @tmp where 序号=@XH
    Set @Count=1
    break
    end
    Set @XH=@XH+1
    Set @Count=@Count-1
    end
    select *from @tmp where 序号<=@XH