日期:2014-05-17 浏览次数:20799 次
DECLARE @sql varchar(max)
DECLARE @sql varchar(max)
DECLARE @px int
DECLARE @n int
SET @n=0
SET @px=1
WHILE @px<=(SELECT max(px) FROM #13)
BEGIN
SELECT @sql='SELECT sspbh,sum(ndhsl)AS sl INTO #16 FROM #13 WHERE px between 1 and '+LTRIM(@px)+' GROUP BY sspbh--次数为1,2的总数'
WHILE @n < @px+5
BEGIN
SET @n =@n+1
SELECT @sql=@sql+'UPDATE a SET ndhsl=CASE
WHEN a.ndhsl<=b.cl THEN 0
WHEN ((a.ndhsl-b.cl)%a.ntpbzl)+(c.sl-d.sl)<a.ntpbzl
THEN a.ndhsl+(c.sl-d.sl)-b.cl ELSE ceiling((a.ndhsl-b.cl)/a.ntpbzl)*a.ntpbzl END
FROM #12 a,# '+LTRIM(@n)+ ' b,#15 c,#16 d
WHERE a.px= '+LTRIM(@px)+' AND a.sspbh=b.sspbh AND b.sspbh=c.sspbh AND a.sspbh=d.sspbh--- 更新2次的收货
SELECT a.sspbh,CASE WHEN b.ndhsl<=a.cl THEN (a.cl-b.ndhsl)
else (b.ntpbzl-abs(b.ndhsl-a.cl)%b.ntpbzl) END AS cl INTO # '+LTRIM(@px)+ ' FROM #' +LTRIM(@n)+ ' a,#13 b
WHERE a.sspbh=b.sspbh AND b.px='+LTRIM(@px)+' end ' ---更新为2的差量
End
SET @px=@px+5
exec (@sql)
END