[转]一个按批资冲减库存的存储过程模型
转自:资深菜鸟
 昨天应朋友之邀写了一个存储过程.贴在这里,有需要的朋友可以看一下.
昨天应朋友之邀写了一个存储过程.贴在这里,有需要的朋友可以看一下. 
 '模型',故名思义,纯理论性的,根据实际情况改造后可以使用.
'模型',故名思义,纯理论性的,根据实际情况改造后可以使用. 

 //如果数据库中存在此对象则删除
     //如果数据库中存在此对象则删除 
 if exists(select id from sysobjects where id=object_id('销售记帐'))
if exists(select id from sysobjects where id=object_id('销售记帐')) 
 drop procedure 销售记帐;
drop procedure 销售记帐; 

 //创建销售记帐存储过程
     //创建销售记帐存储过程 
 create procedure 销售记帐(
create procedure 销售记帐( 
 @销售单号       char(12)             //在审核单据时调用此存储过程,参数据销售单号
@销售单号       char(12)             //在审核单据时调用此存储过程,参数据销售单号 
 )as
)as 

 //定义变理
     //定义变理 
 declare @商品编码 char(7),
declare @商品编码 char(7), 
 @销售数量 decimal(16,4),
  @销售数量 decimal(16,4), 
 @仓库编码 varchar(4),
 @仓库编码 varchar(4), 
 @商品库存批次   varchar(12),
        @商品库存批次   varchar(12), 
 @未记账数量     decimal(16,4),
        @未记账数量     decimal(16,4), 
 @库存数量       decimal(16,4)
        @库存数量       decimal(16,4) 

 //定义游标,查询本单上的所有商品信息
     //定义游标,查询本单上的所有商品信息 
 set cursor_close_on_commit off
set cursor_close_on_commit off 

 declare cur_goods cursor for
declare cur_goods cursor for 
 select 主表.仓库编码,    //销售出库的仓库
select 主表.仓库编码,    //销售出库的仓库 
 从表.商品编码,          //商品编码
       从表.商品编码,          //商品编码 
 从表.销售数量           //销售数量
       从表.销售数量           //销售数量 
 from   主表,                      //主表
from   主表,                      //主表 
 从表                      //从表
       从表                      //从表 
 where  主表.销售单号 = 从表.销售单号 and
where  主表.销售单号 = 从表.销售单号 and 
 主表.销售单号 = @销售单号
       主表.销售单号 = @销售单号  
 
     

 //打开游标
     //打开游标 
 open cur_goods
open cur_goods 
 fetch cur_goods into @仓库编码,@商品编码,@销售数量
fetch cur_goods into @仓库编码,@商品编码,@销售数量  
 while @@fetch_status = 0
while @@fetch_status = 0 
 begin
begin 
 begin transaction
 begin transaction 

 select @未记账数量 = @销售数量
 select @未记账数量 = @销售数量

 if @销售数量>0
 if @销售数量>0 
 declare cur_storage cursor for
  declare cur_storage cursor for 
 select 库存批次,库存数量
  select 库存批次,库存数量 
 from 库存表
    from 库存表 
 where 仓库编码 =@仓库编码 and
   where 仓库编码 =@仓库编码 and 
 商品编码=@商品编码 and
                       商品编码=@商品编码 and 
 库存数量>0      //只取库存大于0的批次
                       库存数量>0      //只取库存大于0的批次 

 open cur_storage
 open cur_storage 
 fetch cur_storage into @商品库存批次,@库存数量
 fetch cur_storage into @商品库存批次,@库存数量 
 while @@fetch_status = 0 and @未记账数量<>0
 while @@fetch_status = 0 and @未记账数量<>0 
 begin
 begin   
 if @库存数量 >= @未记账数量
  if @库存数量 >= @未记账数量 
 begin
  begin 
 update 库存表
   update 库存表 
 set 库存数量 = 库存数量-@未记账数量
          set 库存数量 = 库存数量-@未记账数量 
 where 仓库编码 =@仓库编码 and
    where 仓库编码 =@仓库编码 and 
 商品编码=@商品编码
                        商品编码=@商品编码 
 if @@error != 0
   if @@error != 0 
 begin
   begin 
 rollback transaction
    rollback transaction 
 break
    break 
 end
   end 

 select @未记账数量 = @销售数量
   select @未记账数量 = @销售数量 
 end
  end 
 else
  else 
 begin
    begin 
 select @未记账数量 = @未记账数量-@库存数量
                        select @未记账数量 = @未记账数量-@库存数量 
 update 库存表
   update 库存表 
 set 库存数量 = 库存数量-@未记账数量
          set 库存数量 = 库存数量-@未记账数量 
 where 仓库编码 =@仓库编码 and
    where 仓库编码 =@仓库编码 and 
 商品编码=@商品编码
                        商品编码=@商品编码 
 if @@error != 0
   if @@error != 0 
 begin
   begin 
 rollback transaction
    rollback transaction 
 break
    break 
 end
   end 
 end
    end 
 fetch cur_storage into @商品库存批次,@库存数量
               fetch cur_storage into @商品库存批次,@库存数量 
 end
 end 
 close cur_storage
        close cur_storage 
 deallocate cur_storage
 deallocate cur_storage 
 end
end 
 close cur_goods
close cur_goods 
 deallocate cur_goods
deallocate cur_goods 
 return 0;
return 0; 

 
 昨天应朋友之邀写了一个存储过程.贴在这里,有需要的朋友可以看一下.
昨天应朋友之邀写了一个存储过程.贴在这里,有需要的朋友可以看一下.  '模型',故名思义,纯理论性的,根据实际情况改造后可以使用.
'模型',故名思义,纯理论性的,根据实际情况改造后可以使用. 
 //如果数据库中存在此对象则删除
     //如果数据库中存在此对象则删除  if exists(select id from sysobjects where id=object_id('销售记帐'))
if exists(select id from sysobjects where id=object_id('销售记帐'))  drop procedure 销售记帐;
drop procedure 销售记帐; 
 //创建销售记帐存储过程
     //创建销售记帐存储过程  create procedure 销售记帐(
create procedure 销售记帐(  @销售单号       char(12)             //在审核单据时调用此存储过程,参数据销售单号
@销售单号       char(12)             //在审核单据时调用此存储过程,参数据销售单号  )as
)as 
 //定义变理
     //定义变理  declare @商品编码 char(7),
declare @商品编码 char(7),  @销售数量 decimal(16,4),
  @销售数量 decimal(16,4),  @仓库编码 varchar(4),
 @仓库编码 varchar(4),  @商品库存批次   varchar(12),
        @商品库存批次   varchar(12),  @未记账数量     decimal(16,4),
        @未记账数量     decimal(16,4),  @库存数量       decimal(16,4)
        @库存数量       decimal(16,4) 
 //定义游标,查询本单上的所有商品信息
     //定义游标,查询本单上的所有商品信息  set cursor_close_on_commit off
set cursor_close_on_commit off 
 declare cur_goods cursor for
declare cur_goods cursor for  select 主表.仓库编码,    //销售出库的仓库
select 主表.仓库编码,    //销售出库的仓库  从表.商品编码,          //商品编码
       从表.商品编码,          //商品编码  从表.销售数量           //销售数量
       从表.销售数量           //销售数量  from   主表,                      //主表
from   主表,                      //主表  从表                      //从表
       从表                      //从表  where  主表.销售单号 = 从表.销售单号 and
where  主表.销售单号 = 从表.销售单号 and  主表.销售单号 = @销售单号
       主表.销售单号 = @销售单号   
     
 //打开游标
     //打开游标  open cur_goods
open cur_goods  fetch cur_goods into @仓库编码,@商品编码,@销售数量
fetch cur_goods into @仓库编码,@商品编码,@销售数量   while @@fetch_status = 0
while @@fetch_status = 0  begin
begin  begin transaction
 begin transaction 
 select @未记账数量 = @销售数量
 select @未记账数量 = @销售数量
 if @销售数量>0
 if @销售数量>0  declare cur_storage cursor for
  declare cur_storage cursor for  select 库存批次,库存数量
  select 库存批次,库存数量  from 库存表
    from 库存表  where 仓库编码 =@仓库编码 and
   where 仓库编码 =@仓库编码 and  商品编码=@商品编码 and
                       商品编码=@商品编码 and  库存数量>0      //只取库存大于0的批次
                       库存数量>0      //只取库存大于0的批次 
 open cur_storage
 open cur_storage  fetch cur_storage into @商品库存批次,@库存数量
 fetch cur_storage into @商品库存批次,@库存数量  while @@fetch_status = 0 and @未记账数量<>0
 while @@fetch_status = 0 and @未记账数量<>0  begin
 begin    if @库存数量 >= @未记账数量
  if @库存数量 >= @未记账数量  begin
  begin  update 库存表
   update 库存表  set 库存数量 = 库存数量-@未记账数量
          set 库存数量 = 库存数量-@未记账数量  where 仓库编码 =@仓库编码 and
    where 仓库编码 =@仓库编码 and  商品编码=@商品编码
                        商品编码=@商品编码  if @@error != 0
   if @@error != 0  begin
   begin  rollback transaction
    rollback transaction  break
    break  end
   end 
 select @未记账数量 = @销售数量
   select @未记账数量 = @销售数量  end
  end  else
  else  begin
    begin  select @未记账数量 = @未记账数量-@库存数量
                        select @未记账数量 = @未记账数量-@库存数量  update 库存表
   update 库存表  set 库存数量 = 库存数量-@未记账数量
          set 库存数量 = 库存数量-@未记账数量  where 仓库编码 =@仓库编码 and
    where 仓库编码 =@仓库编码 and  商品编码=@商品编码
                        商品编码=@商品编码  if @@error != 0
   if @@error != 0  begin
   begin  rollback transaction
    rollback transaction  break
    break  end
   end  end
    end  fetch cur_storage into @商品库存批次,@库存数量
               fetch cur_storage into @商品库存批次,@库存数量  end
 end  close cur_storage
        close cur_storage  deallocate cur_storage
 deallocate cur_storage  end
end  close cur_goods
close cur_goods  deallocate cur_goods
deallocate cur_goods  return 0;
return 0; 

 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号