存储过程:
CREATE PROCEDURE [packIntoBox]
(@total int, @volume int)
WITH
EXECUTE AS CALLER
AS
declare @n int,@left int;
declare @tmp table (id int, v int)--tmp是保存结果的临时表
set @n=@total/@volume; --n是能装满的箱子数量
set @left=@total%@volume; --left是最后未装满一箱中的重量
--如果最后一箱重量大于零,把这一箱加入到结果中
if @left>0
insert into @tmp values(@n+1,@left)
--将n个装满的箱子加入到结果中
while @n>0
begin
insert into @tmp values(@n,@volume);
set @n=@n-1
end
--打印结果
select * from @tmp order by id
测试:
exec packIntoBox 100,30
结果:
id v
1 30
2 30
3 30
4 10