日期:2014-05-18 浏览次数:20542 次
-- with循环 m就是你要的数值 ; with a as( select n='liko', m=5000, m1 = 15200-5000 union all select n=a.n, m=(case when a.m1<5000 then a.m1 else 5000 end), m1=a.m1-5000 from a where a.m1>=0 ) select * from a
------解决方案--------------------
递归方法
DECLARE @_salary INT,@_limitSalary INT,@_n INT,@_i INT,@_reminder INT SET @_salary=152000 SET @_limitSalary=50000 SET @_n=@_salary/@_limitSalary SET @_reminder=@_salary%@_limitSalary IF (@_n<=1) SET @_n=1 ;WITH cte(empi,NAME,salary,n) AS ( SELECT 1,'liko',@_salary,@_n UNION ALL SELECT empi,NAME,CASE WHEN n>1 THEN @_limitSalary ELSE salary END,n-1 FROM cte WHERE n>0 UNION ALL SELECT empi,NAME,@_reminder,n-1 FROM cte WHERE n=0 ) SELECT * FROM cte WHERE salary<=50000