IF OBJECT_ID (‘skBuyProduct_Materiel_iMaterialType‘,‘TR‘) IS NOT NULL
DROP TRIGGER skBuyProduct_Materiel_iMaterialType;
GO
CREATE TRIGGER skBuyProduct_Materiel_iMaterialType ON skBuyProduct_Materiel
AFTER INSERT ,UPDATE
AS
DECLARE @ID INTEGER --物料加工管理_产品_物料 ID 此ID与skBuyProduct_PM中的ID_ProduceMateriel 相同
DECLARE @cBillCode_BuyProductRequisition VARCHAR(200)
DECLARE @cCode_Product VARCHAR(200)
SELECT @ID = ID FROM inserted
IF NOT EXISTS(
SELECT 1 FROM skBuyProductRequisition_Materiel mm INNER JOIN
(
SELECT t.iMaterialType,t.ID_Material, d.ID_Main, d.ID_Produce, d.ID_ProduceMateriel, n.ID_RequisitionDetail, n.cBillCode_BuyProductRequisition,
CASE WHEN t.iMaterialType = 0 THEN 6 WHEN t.iMaterialType = 1 THEN 4 WHEN t.iMaterialType = 2 THEN 1 END iMaterialType_sq
FROM skBuyProduct_Materiel t INNER JOIN skBuyProduct_PM d ON t.id = d.ID_ProduceMateriel
INNER JOIN skRequisition_BuyRelation n ON d.ID_Main = n.ID_BuyProduct AND d.ID_Produce = n.ID_BuyDetail
WHERE t.id = @ID
)aa ON mm.ID_Main = aa.ID_RequisitionDetail AND mm.ID_Material = aa.ID_Material AND mm.iType = aa.iMaterialType_sq
)
BEGIN
SELECT @cBillCode_BuyProductRequisition = n.cBillCode_BuyProductRequisition ,@cCode_Product= p.cBillCode
FROM skBuyProduct_Materiel t INNER JOIN skBuyProduct_PM d ON t.id = d.ID_ProduceMateriel
INNER JOIN skBuyProduct p ON d.ID_Main = p.id
INNER JOIN skRequisition_BuyRelation n ON d.ID_Main = n.ID_BuyProduct AND d.ID_Produce = n.ID_BuyDetail
WHERE t.id = 159794
RAISERROR ( ‘触发器:插入物料加工管理_产品_物料表(skBuyProduct_Materiel)时,物料类型和申请单的类型匹配不上,请找系统管理员报告这个错误!加工单号:("%s");申请单号:("%s")‘, 16, 1,@cCode_Product,@cBillCode_BuyProductRequisition);
ROLLBACK TRANSACTION;
RETURN
END