"1、减库存表的库存,有多个工作站运行。如何安全可靠的修改记录。"<br>客户端提交先验证,提交数据加个事务。<br><br>2、不知道有没有理解错,<br> 你要实现库存的批次管理..? 还有按先进先出的方式出库。。?<br> 还有部分看得不是很明白。。<br><br>以下是我以前写的,实现批次和先进先出。。希望对你有帮助。<br>实现方式:存储过程(实现功能)+触发器调用<br><br>//出库算法<br>CREATE PROCEDURE OutWarehouse<br>@BIllName varchar(20),------------- 单据名称<br>@BillNo varchar(20),------------------ 单据编号<br>@AuditFlag varchar(10),-------------- 审核标志<br>@BillDate smallDateTime-------------单据日期<br>AS<br>begin<br> Declare @Warehouse varchar(50),--------仓库编号<br> @Material varchar(40),---------------物料编号<br> @Quantity Float,--------------------------数量<br> @BatchQuantity Float,-----------------批次数量<br> @Amount float,---------------------------金额<br> @BatchNo Varchar(30),----------- -批次<br> @instoctDate smallDatetime,-----入库日期<br> @ProductDate smalldatetime,----生产日期<br> @voidDate smalldatetime,----------失效日期<br> @OutProcedure varchar(20)------物料出库方式<br> DECLARE @Detail_Cursor CURSOR<br><br> if @AuditFlag<>'审核'<br> begin<br> Delete From CKKCCK_TMP Where Djlx=@BillName and Djbh=@BillNo<br> Return<br> end<br>---------------------------------------------------销售出库-----------------------------------------------------<br> if @BillName='销售出库' <br> begin<br> Set @Detail_Cursor=cursor for <br> Select ChCK,Wlbh,CkSl,CKSl*DJ,Pc,RkRq,ScRq,SxRq<br> from CKXSCK a,CKxsckMx b where a.Djbh=b.Djbh and a.Djbh=@BIllNo<br> end<br>----***************************领料单*************************************<br> else if @BillName='领料单'<br> begin<br> Set @Detail_Cursor=cursor for <br> Select CK,Wlbh,Sfsl,SfSl*Dj,Pc,RkRq,ScRq,SxRq<br> from CkLlD a,CkLlDMX b where a.Djbh=b.Djbh and a.Djbh=@BIllNo<br> end<br>----****************************其它出库*************************************<br> else if @BillName='其它出库'<br> begin<br> Set @Detail_Cursor=cursor for <br> Select CK,Wlbh,SfSl,SfSl*DJ,Pc,RkRq,ScRq,SxRq<br> from CKQTCK a,CKQTCKMX b where a.Djbh=b.Djbh and a.Djbh=@BIllNo<br> end<br>---------------------------------------------调拨-----------------------------------------------------------------<br> else if @BillName='仓库调拨入'<br> begin<br> Set @Detail_Cursor=cursor for <br> Select ZrCK,Wlbh,Sl*-1,Sl*DJ*-1,Pc,RkRq,ScRq,SxRq<br> from CkCkDb a,CkCkDbMX b where a.Djbh=b.Djbh and a.Djbh=@BIllNo<br> end<br> else if @BillName='仓库调拨出'<br> begin<br> Set @Detail_Cursor=cursor for <br> Select ZcCK,Wlbh,Sl,Sl*DJ,Pc,RkRq,ScRq,SxRq<br> from CkCkDb a,CkCkDbMX b where a.Djbh=b.Djbh and a.Djbh=@BIllNo <br> end <br> else<br> Return<br>--------------------------------------------End--------------------------------------------------------------------<br><br> OPEN @Detail_Cursor<br> <br> FETCH NEXT FROM @Detail_Cursor<br> Into @warehouse,@Material,@Quantity,@Amount,@BatchNo,@instoctDate,@ProductDate,@voidDate<br><br> WHILE @@FETCH_STATUS = 0<br> BEGIN<br> update JCWLZL Set Zjcksj=@BillDate where Wlbh=@Material -------------------------------------出库时更新物料表最近出库日期<br> Select @OutProcedure=CkFs from Jcwlzl where Wlbh=@Material<br> If @OutProcedure='正常' or @OutProcedure='手工选择' <br> begin<br> Insert into CKKCCK_TMP(DjLx,Djbh,CKbh,Wlbh,CKSl,CKJe,Pc,RkRq,ScRq,SxRq)<br> Values(@BillName,@BillNO,@warehouse,@Material,@Quantity,@Amount,@BatchNo,@instoctDate,@ProductDate,@voidDate)<br> end<br> else if @OutProcedure= '先进先出' or @OutProcedure= '后进先出' ----------------------------物料出库方式为先进先出<br> begin<br> Declare @BatchCur Cursor <br><br> if @OutProcedure= '先进先出'<br> begin<br> Set @BatchCur=cursor for <br> Select Sl,PC,RkRq,ScRq,SxRq from CKKCXL <br> Where CKBH=@Warehouse and Wlbh=@Material and Sl<>0 order by ScRq Asc<br> end<br> else if @OutProcedure= '后进先出'<br> begin<br> Set @BatchCur=cursor for <br> Select Sl,PC,RkRq,ScRq,SxRq from CKKCXL <br> Where CKBH=@Warehouse and Wlbh=@Material and Sl<>0 order by ScRq Desc<br> end<br><br> open @BatchCur<br><br> fetch next from @BatchCur<br> Into @BatchQuantity,@BatchNo,@InstoctDate,@ProductDate,@voidDate<br><br> while @@FETCH_STATUS = 0 and @Quantity>0<br> begin<br><br> if @Quantity>@BatchQuantity <br> set @Quantity=@Quantity-@BatchQuantity<br> else <br> begin<br> Set @BatchQuantity=@Quantity<br> Set @Quantity=0<br> end<br><br> Insert into CKKCCK_TMP(DjLx,Djbh,CKbh,Wlbh,CKSl,CKJe,Pc,RkRq,ScRq,SxRq)<br> Values(@BillName,@BillNO,@warehouse,@Material,@BatchQuantity,@Amount,@BatchNo,@instoctDate,@ProductDate,@voidDate) <br> <br> fetch next from @BatchCur<br> Into @BatchQuantity,@BatchNo,@InstoctDate,@ProductDate,@voidDate<br> end <br> if @Quantity>0 <br> begin<br> Insert into CKKCCK_TMP(DjLx,Djbh,CKbh,Wlbh,CKSl,CKJe,Pc,RkRq,ScRq,SxRq)<br> Values(@BillName,@BillNO,@warehouse,@Material,@Quantity,@Amount,@BatchNo,@instoctDate,@ProductDate,@voidDate) <br><br> end<br> close @BatchCur<br> deallocate @BatchCur <br> end<br> else<br> Return<br><br> --set @Quantity=@Quantity*@Flag<br> --exec UpdateWarehouse @warehouse,@Material,@Quantity,@Amount,@BatchNo,@instoctDate,@ProductDate,@voidDate<br> FETCH NEXT FROM @Detail_Cursor<br> Into @warehouse,@Material,@Quantity,@Amount,@BatchNo,@instoctDate,@ProductDate,@voidDate <br> END<br><br> CLOSE @Detail_Cursor<br> DEALLOCATE @Detail_Cursor <br><br>end<br>GO<br><br><br><br>//更新库存<br>CREATE PROCEDURE UpdateWarehouse<br>@warehouse varchar(50),-------------仓库编号<br>@material varchar(40),------------------ 物料编号<br>@quantity float,----------------------------- 数量<br>@Amount float,----------------------------- 金额<br>@BatchNo Varchar(30),---------------- 批次<br>@instoctDate smallDatetime,---------入库日期<br>@ProductDate smalldatetime,--------生产日期<br>@voidDate smalldatetime--------------失效日期<br>As<br>Set @BatchNo=isNull(@BatchNo,'')<br>if exists(Select 1 from CKKCXl Where Ckbh=@warehouse and Wlbh=@material and Pc=@BatchNo)<br>begin<br> UPDATE CKKCXl <br> SET SL=SL+@quantity,Je=Je+@Amount<br> WHERE Ckbh=@warehouse and Wlbh=@material and Pc=@BatchNo<br>end<br>else<br>begin<br> Insert Into CKKCXl(ckbh,Wlbh,Sl,Je,Pc,RkRq,ScRq,SxRq)<br> Values(@Warehouse,@material,@quantity,@Amount,@BatchNo,@InstoctDate,@ProductDate,@VoidDate)<br>end<br>GO<br><br><br>这个部分代码是触发器引用<br>CREATE TRIGGER XsAudit ON [dbo].[CKXSCK] <br>FOR UPDATE<br>AS<br> Declare @BillNo varchar(20),@AuditFlag varchar(10),@BillDate SmallDateTime<br> Select @BillNo=DJbh,@AuditFlag=Shbz,@BillDate=DjRq from inserted<br> if Update(Shbz)<br> begin<br> if (Select ChBs from inserted )=1<br> exec OutWarehouse '销售出库',@BillNo,@AuditFlag,@BillDate<br> else<br> exec inWarehouse '销售退货',@BillNo,@AuditFlag,@BillDate<br> end