0
看到下面我的觸發器不工作。我已經測試了很多次,觸發器正在工作,但不是光標。我得到來自inserted
值,但它不執行 WHILE (@@FETCH_STATUS = 0)
而@@ FETCH_STATUS = 0不起作用
ALTER TRIGGER [dbo].[TRG_InsertStockMasterDetail]
ON [dbo].[PrdBallMRRConsumption]
AFTER INSERT AS
Begin
Declare @IssueTypeID Int, @IssueID bigint, @IssueDate dateTime, @SupplierID int, @DepartmentID int,
@CompanyID int, @CreateBy int, @CreateDate Datetime, @CreatePC nvarchar(50), @IsDeleted bit,
---------------------------------------------------------------------------------------------------------------------------------------
@ItemID int , @UOMID int, @LotID int, @Qty Decimal(18,6),
@UnitPrice Decimal(18,6), @ItemTypeID int, [email protected] int, @GradeID int,
---------------------------------------------------------------------------------------------------------------------------------------
@ReceiveValue Decimal(18,6), @ReceiveQty Decimal(18, 6), @IssueQty Decimal(18,6), @IssueValue Decimal(18,6), @StockID bigint,
@CurrentStock numeric(18, 6), @CurrentRate numeric(18, 10), @CurrentStockValue numeric(18, 6),
@IsStockUpdate bit, @IsCostingUpdate bit
----/// Declare Cursour ///----
--select BallConsumptionID, YarnCountID, SupplierID, DepartmentID, LotID, LengthM, LengthYds, UnitPrice, Amount, Qty, Unit, Remarks, CompanyID, CreateBy, CreateOn, CreatePc, UpdateBy, UpdateOn, UpdatePc, IsDeleted, DeleteBy, DeleteOn, DeletePc from PrdBallMRRConsumption
DECLARE Issue_Insert CURSOR FOR
Select M.TransactionTypeID,D.BallConsumptionID,M.BalMRRDate,D.SupplierID,D.DepartmentID,D.CompanyID,D.CreateBy,D.CreateOn,D.CreatePc,D.IsDeleted,
D.YarnCountID ItemID, D.Unit UnitID,D.LotID,D.Qty,D.UnitPrice,(ISNULL(D.Qty,0) * ISNULL(D.UnitPrice,0)) AS ReceiveValue
from Inserted D --PrdBallMRRConsumption
INNER JOIN PrdBallMRRDetail S ON D.BallConsumptionID=S.BallConsumptionID
INNER JOIN PrdBallMRRMaster M ON S.BalMRRID=M.BalMRRID
OPEN Issue_Insert
FETCH NEXT FROM Issue_Insert
INTO @IssueTypeID ,@IssueID ,@IssueDate,@SupplierID ,@DepartmentID , @CompanyID,@CreateBy, @CreateDate,@CreatePC,@IsDeleted,
-------
@ItemID , @UOMID, @LotID, @Qty, @UnitPrice, @ReceiveValue
------
----/// Loop Cursour ///----
WHILE (@@FETCH_STATUS = 0)
BEGIN
Select @ItemTypeID = ItemTypeID from CmnItemMaster where [email protected]
select @IsStockUpdate=IsStockUpdate,@IsCostingUpdate=IsCostingUpdate
from CmnTransactionTypeERPIntigration
Where [email protected] AND [email protected]
IF(@IsStockUpdate=1)
BEGIN
Select @ReceiveQty =0, @CurrentStock =0, @CurrentRate =0, @CurrentStockValue=0
IF(ISNULL(@ItemID,'') <> '')
Begin
--1 For Random , 2 For FIFO, 3 For LIFO
IF((SELECT TOP 1 IntegrationType FROM InvIntegrationType
Where (ISNULL(DepartmentID,0)=0 OR [email protected])
and [email protected])='AVG')
BEGIN
Update InvStockMaster Set ReceiveQty = Isnull(ReceiveQty,0) - @Qty, ReceiveValue = Isnull(ReceiveValue,0) - @ReceiveValue,
LastReceiveDate= Case When Isnull(LastReceiveDate,@IssueDate)<[email protected] Then @IssueDate Else LastReceiveDate End
Where Isnull(CompanyID,0)=Isnull(@CompanyID,0)
And Isnull(DepartmentID,0) = Isnull(@DepartmentID,0)
And Isnull(ItemID,0) = Isnull(@ItemID,0)
And Isnull(UOMID,0) = Isnull(@UOMID,0)
And Isnull(SupplierID,0) = Isnull(@SupplierID,0)
--AND ISNULL(BatchID,0)=ISNULL(@BatchID,0)
AND ISNULL(LotID,0)=ISNULL(@LotID,0)
--AND ISNULL(GradeID,0)=ISNULL(@GradeID,0)
Select @StockID = StockID from InvStockMaster
Where Isnull(CompanyID,0)=Isnull(@CompanyID,0)
And Isnull(DepartmentID,0) = Isnull(@DepartmentID,0)
And Isnull(ItemID,0) = Isnull(@ItemID,0)
And Isnull(UOMID,0) = Isnull(@UOMID,0)
And Isnull(SupplierID,0) = Isnull(@SupplierID,0)
--AND ISNULL(BatchID,0)=ISNULL(@BatchID,0)
AND ISNULL(LotID,0)=ISNULL(@LotID,0)
--AND ISNULL(GradeID,0)=ISNULL(@GradeID,0)
INSERT INTO InvStockDetail
(StockID, TransactionID, TransactionTypeID, StockDate, ItemID, ItemTypeID, LotID, SupplierID, DepartmentID, ReceiveQty, ReceiveRate, ReceiveValue, UOMID, CompanyID, CreateBy, CreateOn, CreatePc, IsDeleted)
Values (@StockID, @IssueID, @IssueTypeID, @IssueDate, @ItemID, @ItemTypeID, @LotID, @SupplierID, @DepartmentID, @Qty, @UnitPrice, @ReceiveValue, @UOMID, @CompanyID, @Createby, @CreateDate, @CreatePC, @IsDeleted)
----// For Average Cost //----
--Begin
IF(@IsCostingUpdate=1)
BEGIN
Update InvStockMaster Set CurrentRate = CurrentValue/CurrentStock
Where Isnull(CurrentValue,0) <> 0 And Isnull(CurrentStock,0)<>0
AND Isnull(CompanyID,0)=Isnull(@CompanyID,0)
And Isnull(DepartmentID,0) = Isnull(@DepartmentID,0)
And Isnull(ItemID,0) = Isnull(@ItemID,0)
And Isnull(UOMID,0) = Isnull(@UOMID,0)
And Isnull(SupplierID,0) = Isnull(@SupplierID,0)
--AND ISNULL(BatchID,0)=ISNULL(@BatchID,0)
AND ISNULL(LotID,0)=ISNULL(@LotID,0)
--AND ISNULL(GradeID,0)=ISNULL(@GradeID,0)
END
END
--Comments
END
END
FETCH NEXT FROM Issue_Insert
INTO @IssueTypeID , @IssueID , @IssueDate , @SupplierID , @DepartmentID ,
@CompanyID , @CreateBy , @CreateDate , @CreatePC, @IsDeleted ,
-------------------------------------------------------------------------------------------------------
@ItemID , @UOMID , @LotID , @Qty , @UnitPrice , @ReceiveValue
[email protected], @GradeID ,
End
CLOSE Issue_Insert
DEALLOCATE Issue_Insert
End
此位是否返回任何行:'select * from Inserted D INNER JOIN PrdBallMRRDetail S ON D.BallConsumptionID = S.BallConsumptionID INNER JOIN PrdBallMRRMaster M ON S.BalMRRID = M.BalMRRID' – Jaco
是的,它返回數據。我還在另一個表格中創建了這樣的觸發器。這工作正常。但它不起作用 –