--select top 1 * from jxcybb_new_define
--syyTest20091024_getPosData 120,'SPX028.A09ED'
alter procedure syyTest20091024_getPosData
(@yl int,@zpbh varchar(50))
as
begin
if(not exists(select * from atest20091024 where ylID=@yl and zpbh=@zpbh))
begin
insert into atest20091024(ylID,zpbh,qichu)
Select @yl,@zpbh,qckc from jxcybb_new_define
Where ny='2009-09-25' and yl=@yl and zpbh=@zpbh
create table #tmp (rq datetime,bill_bh varchar(100),operation nvarchar(100),insl int,outsl int)
insert into #tmp exec [proc_product_io] '2009-07-26','2009-10-20',@zpbh,@yl
--开始事务
BEGIN TRAN
--不显示计数信息
SET NOCOUNT ON
DECLARE @insl1 int,@outsl1 int
--声明游标
DECLARE CRMPSContact_cursor CURSOR FOR
select insl,outsl from #tmp
--打开游标
OPEN CRMPSContact_cursor
--取第一行的值给变量
FETCH CRMPSContact_cursor
INTO @insl1,@outsl1
--执行错误回滚
if @@error!=0
begin
rollback tran
return
end
--移动游标,其它所有行更新操作(当到结尾时退出)
WHILE @@FETCH_STATUS = 0
BEGIN
--执行语句,从第二行开始
if(not exists (select * from atest20091024 where ylID=@yl and zpbh=@zpbh))
begin
insert into atest20091024(ylID,zpbh,comeIn,comeOut)
select @yl,@zpbh,insl,outsl from #tmp
end
else
begin
--print @outsl1
update atest20091024 set comeIn=isnull(comeIn,0)+@insl1,comeOut=isnull(comeOut,0)+@outsl1
where zpbh=@zpbh and ylID=@yl
end
--游标移到下一行
FETCH NEXT FROM CRMPSContact_cursor
INTO @insl1,@outsl1
--执行错误回滚
if @@error!=0
begin
rollback tran
return
end
END
--关闭游标
CLOSE CRMPSContact_cursor
--释放游标
DEALLOCATE CRMPSContact_cursor
--提交所有变更
COMMIT TRAN
--恢复设置
SET NOCOUNT OFF
--if(not exists (select * from atest20091024 where ylID=@yl and zpbh=@zpbh))
--begin
-- insert into atest20091024(ylID,zpbh,comeIn,comeOut)
-- select @yl,@zpbh,insl,outsl from #tmp
--end
--else
--begin
--update atest20091024 set comeIn=isnull(comeIn,0)+b.insl,comeOut=isnull(comeOut,0)+b.outsl
--from atest20091024,#tmp b where atest20091024.zpbh=@zpbh
--end
--select distinct *
-- from atest20091024
--group by ylID,zpbh
drop table #tmp
end
end