-- ================================================
-- Template generated from Template Explorer using:
-- Create Trigger (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- See additional Create Trigger templates for more
-- examples of different Trigger statements.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE TRIGGER DZX_MaterialAppVouchs_iQuantity
ON MaterialAppVouchs
AFTER INSERT ,UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT *
FROM inserted
LEFT JOIN (SELECT CINVCODE,cFree1,iQuantity,INUM FROM CurrentStock WHERE iQuantity <> '0' AND INUM <> '0') AS XCL
ON inserted.cInvCode = XCL.cInvCode AND inserted.cFree1 = XCL.cFree1 AND inserted.iNum = XCL.iNum
WHERE XCL.iQuantity IS NOT NULL)
BEGIN
-- ִ
UPDATE MaterialAppVouchs SET MaterialAppVouchs.iQuantity = XCL.iQuantity
FROM MaterialAppVouchs
LEFT JOIN (SELECT CINVCODE,cFree1,iQuantity,INUM FROM CurrentStock WHERE iQuantity <> '0' AND INUM <> '0') AS XCL
ON MaterialAppVouchs.cInvCode = XCL.cInvCode AND MaterialAppVouchs.cFree1 = XCL.cFree1 AND MaterialAppVouchs.iNum = XCL.iNum
left JOIN inserted ON MaterialAppVouchs.AutoID = inserted.AutoID
left JOIN deleted ON MaterialAppVouchs.AutoID = deleted.AutoID
WHERE 1 = 1
AND (inserted.AutoID <> '0' OR deleted.AutoID <> '0')
AND XCL.iQuantity IS NOT NULL
END
ELSE
BEGIN
UPDATE MaterialAppVouchs SET iQuantity = MaterialAppVouchs.iNum * cInvDefine13
FROM MaterialAppVouchs
LEFT JOIN (SELECT CINVCODE, cInvDefine13 FROM Inventory WHERE cInvDefine13 <> '1') AS HSL
ON MaterialAppVouchs.cInvCode = HSL.cInvCode
left JOIN inserted ON MaterialAppVouchs.AutoID = inserted.AutoID
left JOIN deleted ON MaterialAppVouchs.AutoID = deleted.AutoID
WHERE 1 = 1
AND (inserted.AutoID <> '0' OR deleted.AutoID <> '0')
AND HSL.cInvCode IS NOT NULL
END
END
GO