创建存储过程:
其中tingting1为数据库名称,tingting为存储过程名称,instore_cursor和outstore_cursor为两个出库和入库对应的两个游标
use tingting1
if exists(
select name
from sysobjects
where name='tingting' and type='P'
)
drop procedure tingting
GO
create procedure tingting
as
create table #result
(编号  int,
 部门  char(10),
 入时间 datetime,
 出时间 datetime,
 数量 int)
DECLARE instore_cursor SCROLL CURSOR
for
SELECT * from instore
declare outstore_cursor scroll cursor
for 
select * from outstore
open outstore_cursor
open instore_cursor
declare @inid int
declare @indepart char(10)
declare @intime datetime
declare @innum int
declare @outid int
declare @outdepart char(10)
declare @outtime datetime
declare @outnum int
FETCH FIRST FROM instore_cursor into @inid,@indepart,@intime,@innum
if(@@FETCH_STATUS=0)
BEGIN 
FETCH FIRST FROM outstore_cursor into @outid,@outdepart,@outtime,@outnum
while(@@FETCH_STATUS=0)
begin
tt1:
if(@inid=@outid and @indepart=@outdepart and @innum>0 and @outnum>0 and @intime<=@outtime)
begin
if(@innum<@outnum) 
begin
insert into #result values (@inid,@indepart,@intime,@outtime,@innum)
set @outnum=@outnum-@innum
fetch next from instore_cursor into @inid,@indepart,@intime,@innum
if(@@FETCH_STATUS=0)
BEGIN
goto tt1
END
else
begin
break;
end
end
else
begin
insert into #result values (@inid,@indepart,@intime,@outtime,@outnum)
set @innum=@innum-@outnum
FETCH NEXT FROM outstore_cursor into @outid,@outdepart,@outtime,@outnum
continue;
end
end
end
if(@innum>0)
begin
set @outtime=dateadd(dd,1,@outtime)
insert into #result values(@inid,@indepart,@intime,@outtime,@innum)
end
end
deallocate outstore_cursor
deallocate instore_cursor
select * from #result
order by 出时间
GO

解决方案 »

  1.   

    执行:execute tingting
    执行结果:
    1001 计算机部   2007-05-25 00:00:00.000 2007-05-26 00:00:00.000 3
    1001 计算机部   2007-05-25 00:00:00.000 2007-05-27 00:00:00.000 2
    1001 计算机部   2007-05-25 00:00:00.000 2007-05-29 00:00:00.000 2
    1001 计算机部   2007-05-28 00:00:00.000 2007-05-29 00:00:00.000 10
    1001 计算机部   2007-05-29 00:00:00.000 2007-05-29 00:00:00.000 1
    1001 计算机部   2007-05-29 00:00:00.000 2007-05-30 00:00:00.000 4
      

  2.   

    --生成测试数据
    create table [in](编号 int,部门 varchar(10),日期 datetime,数量 int)
    insert into [in] select 1001,'计算机部','2007-05-25',7
    insert into [in] select 1001,'计算机部','2007-05-28',10
    insert into [in] select 1001,'计算机部','2007-05-29',5
    create table out(编号 int,部门 varchar(10),日期 datetime,数量 int)
    insert into out  select 1001,'计算机部','2007-05-26',3
    insert into out  select 1001,'计算机部','2007-05-27',2
    insert into out  select 1001,'计算机部','2007-05-29',13
    go--建一个连续ID的中间表,所需要的行数可根据[in]/[out]表的最大数量值来限定
    set rowcount 100
    select identity(int,1,1) as num into # from sysobjects,syscolumns
    set rowcount 0--展开in表中的记录
    select identity(int,1,1) as RID,0 as ID,编号,部门,日期,1 as num
    into #IN from [IN] a,# b where a.数量>=b.NUM--展开out表中的记录
    select identity(int,1,1) as RID,0 as ID,编号,部门,日期,1 as num
    into #OUT from OUT a,# b where a.数量>=b.NUM--更新序列得到参考值
    update t set ID=(select count(*) from #IN  where 编号=t.编号 and 部门=t.部门 and RID<=t.RID) from #IN t
    update t set ID=(select count(*) from #OUT where 编号=t.编号 and 部门=t.部门 and RID<=t.RID) from #OUT t--查询临时表得到最终所需要的结果
    select
        i.编号,i.部门,i.日期 as 入日期,o.日期 as 出日期,count(*) as 数量
    from
        #IN i
    left join
        #OUT o
    on
        i.编号=o.编号 and i.部门=o.部门 and i.ID=o.ID
    group by
        i.编号,i.部门,i.日期,o.日期
    order by
        i.编号,i.部门,i.日期,isnull(o.日期,'2009-01-01')
    go--查询输出的结果集如下
    /*                                       
    编号  部门       入日期                  出日期                  数量
    ----- ---------- ----------------------- ----------------------- ---------
    1001  计算机部   2007-05-25 00:00:00.000 2007-05-26 00:00:00.000 3
    1001  计算机部   2007-05-25 00:00:00.000 2007-05-27 00:00:00.000 2
    1001  计算机部   2007-05-25 00:00:00.000 2007-05-29 00:00:00.000 2
    1001  计算机部   2007-05-28 00:00:00.000 2007-05-29 00:00:00.000 10
    1001  计算机部   2007-05-29 00:00:00.000 2007-05-29 00:00:00.000 1
    1001  计算机部   2007-05-29 00:00:00.000 NULL                    4
    */--清除测试环境
    drop table #,#IN,#OUT
    drop table [IN],OUT
    go