SQL Server触发器 插入商品明细时 库存随着更改

CREATE TRIGGER [t_i_his_trans_detailed ] ON [dbo].[his_trans_detailed]
FOR INSERT 
AS

declare @GoodsCode varchar(20) ,@ybbm  varchar(10)  ,@salenum  numeric(8,2)
Declare curs_sb_detail CURSOR FOR  
 select  GoodsCode,ybbm,salenum  from  inserted i 
 Open curs_sb_detail
    fetch curs_sb_detail into @GoodsCode,@ybbm,@salenum
              While @@FETCH_STATUS  = 0 
  begin
                           if  exists (select top 1 *  from goods_stock where GoodsCode=@GoodsCode and ybbm=@ybbm )
                                 begin
                                         update goods_stock  set  qty= qty - @salenum   where GoodsCode=@GoodsCode and ybbm=@ybbm
                                          if @@rowcount=0 or @@error<>0
                                             begin
                                                 close curs_sb_detail 
                                     DEALLOCATE curs_sb_detail
                                                  RAISERROR ('库存修改不成功',16, 1)
                                            end
                                 end
              fetch curs_sb_detail into @GoodsCode,@ybbm,@salenum
  end 
        close curs_sb_detail 
     DEALLOCATE curs_sb_detail  

posted on 2011-11-26 17:55  疯狂的石头陈  阅读(648)  评论(0编辑  收藏  举报

导航