Create Table tb(tid int,sl int,tdate datetime) Insert into tb Select 1,10,'2009-11-9' union all Select 1,5,'2009-11-10' union all Select 1,100,'2009-11-11' Ò»ÖÖÉÌÆ·³ö¿â£¬Èç¹û³ö¿âÊýÁ¿Ð¡ÓÚ10£¬Ö±½Ó´Ó×îСµÄÈÕÆÚ¼õµô£¬ÆäËûµÄ²»±ä£»¼ÙÈç³ö¿âÊýÁ¿(sl)Ϊ20£¬Ê×ÏȳöµÄÊÇ×îСµÄÈÕÆÚ£¬×îСµÄÈÕÆÚÊýÁ¿²»¹»Íùºó¼õÉÙµÚ¶þ¸öÈÕÆÚ¶ÔÓ¦µÄÊýÁ¿£¬ÒÔ´ËÀàÍÆ¡£
Create Table tb(tid int,sl int,tdate datetime) Insert into tb Select 1,10, '2009-11-9 ' union all Select 1,5, '2009-11-10 ' union all Select 1,100, '2009-11-11 '
declare @sl int set @sl=13 -- ³ö¿âÁ¿ while @sl > 0 update tb set @sl=@sl-sl, sl=(case when @sl> =0 then 0 else abs(@sl) end) where tdate=(select min(tdate) &n