商品资料数据同步过程

USE [SABC]
GO
/****** Object:  StoredProcedure [dbo].[insertIntoGoods]  
  Script Date: 12/31/2013 10:23:09 ******/


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[insertIntoGoods]  
as 
declare @errcode integer
select @errcode = 7000        
   
declare @return integer
set @return=0

   --启动事务处理
declare @tran_point int
set @tran_point = @@trancount
if @tran_point = 0
  begin tran tran_UpdWmsData
else
  save tran tran_UpdWmsData
 
create table #abc(
    goodsid char(11) null default ''
    ,goodscode varchar(200) default ''
    ,goodsname varchar(300) default ''
    ,seqno int
    ,zt char(1) default 'N'
    ,entid char(11) default ''
)
if exists( select 1 from skzy..goodsdoc where zt = 'N')
--begin 
insert into #abc(entid,goodsid,goodscode,goodsname,seqno,zt)
select entid,goodsid,goodscode,goodsname,seqno,zt
from skzy..GOODSDOC
where zt = 'N'
order by seqno
 if @@error <> 0 or @@rowcount = 0
         begin
             set @return=11
         goto err_lab
         end
         
declare @goodsid char(11)  
declare @seqno integer 


declare pcurr cursor for select goodsid,seqno from #abc --group by seqno
open pcurr 
fetch next from pcurr into @goodsid,@seqno
while (@@fetch_status = 0)  
begin  
if exists(select 1 from sabc..GOODSDOC where GoodsId = @goodsid) 
begin 
update a set GoodsCode = b.GoodsCode,GoodsName = b.GoodsName
from SABC..GOODSDOC a
join #abc b on a.GoodsId = b.goodsid
where a.GoodsId = @goodsid and b.seqno = @seqno
if @@error <> 0
  begin
  set @return=12
  goto err_lab
  end
update skzy..GOODSDOC set zt ='Y' where seqno = @seqno
end
else
begin 
   insert into sabc..GOODSDOC(GoodsId,EntId,GoodsCode,GoodsName)
   select GoodsId,EntId,GoodsCode,GoodsName
   from #abc
   where goodsid = @goodsid and seqno = @seqno
   if @@error <> 0
         begin
             set @return=13
         goto err_lab
         end
   update skzy..GOODSDOC set zt ='Y' where seqno = @seqno
end
fetch next from pcurr into @goodsid,@seqno
end
close pcurr
deallocate pcurr 


ok_lab:
--删除临时表
   drop table #abc
    --结束事务处理
 commit_lab:
 if @tran_point = 0
  commit tran tran_UpdWmsData
 goto return_lab
 
 err_lab:
 if @return < 10000 
 set @return = cast(cast(@errcode as varchar(6)) + 
              right(cast(10000 + @return as varchar(5)),4) as int)
 
 rollback tran tran_UpdWmsData
 
 return_lab:
 return @return

你可能感兴趣的:(sql,server)