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
はい、それはデータを返します。私はまた別のテーブルの前にこのようなトリガーを作成しています。それはうまく動作しています。それは動作しません –