|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册账号
×
第一次写触发器,有些错误,不知道在哪,求大神给看看,在线急等,谢谢!
进货单审核后自动生成材料出库单
--采购进货单审核触发生成材料出库单,弃审时删除对应的材料出库单
CREATE TRIGGER Add_Other_Out_Vouch1 ON ST_RDRecord
FOR UPDATE
AS
IF UPDATE(VoucherState)--审核状态
begin tran
BEGIN
DECLARE @Bln int --判断是否已经增加了材料出库单
DECLARE @MaxVouch INT
--ST_RDRecord表变量声明
DECLARE @Systime char(19) --系统时间
DECLARE @NewAutoID INT
DECLARE @PurchaseArrival_ID uniqueidentifier --进货单ID
DECLARE @RDRecord_ID uniqueidentifier --出库单表头ID
DECLARE @ID nvarchar(30) --ID号(进货单编码)
DECLARE @code nvarchar(30) --单据编号
DECLARE @voucherstate uniqueidentifier --审核状态
DECLARE @accountstate uniqueidentifier --核算状态
DECLARE @AccountingPeriod int --当前月
DECLARE @AccountingYear int --当前年
DECLARE @idbusiType uniqueidentifier --直接领料
DECLARE @idvoucherType uniqueidentifier --材料出库单
DECLARE @idrdStyle uniqueidentifier --材料出库
DECLARE @IdMarketingOrgan uniqueidentifier
--ST_RDRecord_b表变量声明
DECLARE @b_code int --行号
DECLARE @quantity decimal(28,14) --数量
DECLARE @price decimal(28,14) --单价
DECLARE @amount decimal(28,14) --金额
DECLARE @idrDRecordDTO uniqueidentifier --ST_RDRecord.id
DECLARE @idinventory uniqueidentifier --存货编码
DECLARE @idunit uniqueidentifier --单位
DECLARE @idwarehouse uniqueidentifier --库位
--ST_RDRecord赋值
SET @PurchaseArrival_ID =(SELECT ID FROM PU_PurchaseArrival) --进货单ID
SET @Systime =(SELECT getdate())
SET @ID =(SELECT code from PU_PurchaseArrival) --ID号(进货单编码)
SET @code =(SELECT code=@ID)
SET @RDRecord_ID =(SELECT ID FROM ST_RDRecord WHERE code=@ID)
SET @voucherstate =(SELECT id from eap_EnumItem where Name='未审核')
SET @accountstate =(SELECT id from eap_EnumItem where Name='未核算')
SET @AccountingPeriod =(SELECT DATEPART(month, GETDATE()))
SET @AccountingYear =(SELECT DATEPART(year, GETDATE()))
SET @idvoucherType =(SELECT id from SM_VoucherType where Name='材料出库单')
SET @idbusiType =(SELECT id from AA_BusiType where Name='直接领料')
SET @idrdStyle =(SELECT id from AA_RDStyle where Name='材料出库')
SET @IdMarketingOrgan =(SELECT id from AA_MarketingOrgan where disabled=0 )
END
--ST_RDRecord插入
--SET @Bln=(select count(*) from ST_RDRecord where code=@ID)
--IF ((@Bln<1)--如果不存在材料出库单,开始增加操作
--删除已有表单
DELETE FROM ST_RDRecord_b WHERE idrDRecordDTO=@RDRecord_ID
DELETE FROM ST_RDRecord WHERE ID=@RDRecord_ID
begin
insert into ST_RDRecord(ID,code,voucherstate,accountstate,AccountingPeriod,AccountingYear,idvoucherType,idbusiType,idrdStyle,IdMarketingOrgan,printtime,amount,RdDirectionFlagm,IsCostAccount,IsMergedFlow,IsAutoGenerate,VoucherDate,IsCarriedForwardOut,IsCarriedForwardIn,IsModifiedCode,SequenceNumber,VoucherDate,madedate,CreatedTime,updated,maker,updatedby,VoucherYear,VoucherPeriod)values(NewID(),@code,@voucherstate,@accountstate,@AccountingPeriod,@AccountingYear,@idvoucherType,@idbusiType,@idrdStyle,@IdMarketingOrgan,0,0,0,0,0,0,0,0,0,0,@Systime,@Systime,@Systime,@Systime,'demo','demo',@AccountingYear,@AccountingPeriod)
SET @idrDRecordDTO=(select id from PU_PurchaseArrival where code=@code)
DECLARE PU_PurchaseArrival_b_Cursor CURSOR
FOR
SELECT code,quantity,OrigDiscountPrice,OrigTaxAmount,idinventory,idunit,idwarehouse FROM PU_PurchaseArrival_b WHERE idpurchaseArrivalDTO=@PurchaseArrival_ID
SET @MaxVouch=0
OPEN PU_PurchaseArrival_b_Cursor
FETCH NEXT FROM PU_PurchaseArrival_b_Cursor
INTO @b_code,@quantity,@price,@amount,@idinventory,@idunit,@idwarehouse
WHILE @@FETCH_STATUS = 0
BEGIN
set @NewAutoID=@NewAutoID+1
insert into ST_RDRecord_b(id,code,quantity,BaseQuantity,price,BasePrice,amount,ismanualcost,IsCostAccounted,taxflag,createdtime,sequencenumber,updated,updatedby,idrDRecordDTO,idinventory,idunit,idbaseUnit,idwarehouse,IsPromotionPresent,ispresent) values (NewID(),@b_code,@quantity,@quantity,@amount,0,0,0,0,@Systime,0,@Systime,'demo',@RDRecord_ID,@idinventory,@idunit,@idunit,@idwarehouse,0,0)
set @MaxVouch=@MaxVouch+1
FETCH NEXT FROM PU_PurchaseArrival_b_Cursor
INTO @b_code,@quantity,@price,@amount,@idinventory,@idunit,@idwarehouse
CLOSE PU_PurchaseArrival_b_Cursor
DEALLOCATE PU_PurchaseArrival_b_Cursor
END
END
ElSE
BEGIN
DELETE FROM ST_RDRecord_b WHERE idrDRecordDTO=@RDRecord_ID
DELETE FROM ST_RDRecord WHERE ID=@RDRecord_ID
END
commit tran
END
|
|