ALTER TRIGGER [dbo].[Create_Component_Demand]
ON [dbo].[tblFinished_Goods]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF EXISTS(SELECT * FROM INSERTED)
BEGIN
INSERT INTO tblPlan_Component_Demand
(Pro_Ord,Mat_no,Pro_qty,Comp_no,Comp_qty)
SELECT
Pro_Ord,
Mat_no,
Pro_qty,
(SELECT dbo.tblBOM.Comp_no
FROM dbo.tblBOM INNER JOIN
dbo.tblFinished_Goods
ON dbo.tblBOM.Mat_no = dbo.tblFinished_Goods.Mat_no
WHERE (dbo.tblFinished_Goods.Pro_Ord
IN (SELECT Pro_Ord FROM INSERTED))
AND (dbo.tblFinished_Goods.Mat_no
IN (SELECT Mat_no FROM INSERTED))),
(SELECT dbo.tblBOM.Comp_qty
FROM dbo.tblBOM INNER JOIN
dbo.tblFinished_Goods
ON dbo.tblBOM.Mat_no = dbo.tblFinished_Goods.Mat_no
WHERE (dbo.tblFinished_Goods.Pro_Ord
IN (SELECT Pro_Ord FROM INSERTED))
AND (dbo.tblFinished_Goods.Mat_no
IN (SELECT Mat_no FROM INSERTED))
AND (dbo.tblBOM.Comp_no
IN (SELECT dbo.tblBOM.Comp_no
FROM dbo.tblBOM INNER JOIN
dbo.tblFinished_Goods
ON dbo.tblBOM.Mat_no = dbo.tblFinished_Goods.Mat_no
WHERE (dbo.tblFinished_Goods.Pro_Ord
IN (SELECT Pro_Ord FROM INSERTED))
AND (dbo.tblFinished_Goods.Mat_no
IN (SELECT Mat_no FROM INSERTED)))))
FROM INSERTED
END
END