//Write a trigger in sql server
-- =============================================
CREATE TRIGGER [dbo].[trigUpdateDissection_Cat_Vendor]
ON [dbo].[tblProductDissection]
AFTER INSERT,DELETE
AS
BEGIN
DECLARE
@VendorId bigint,
@DissectionId bigint,
@CategoryId bigint,
@ProductId bigint
IF (SELECT Count(*) FROM INSERTED) > 0
BEGIN
SELECT
@ProductId=tblProductDissection.ProductId,
@DissectionId=tblProductDissection.DissectionId
FROM INSERTED tblProductDissection
insert into dbo.tblDissection_Cat_Vendor
(
VendorId,
DissectionId,
CategoryId
)
SELECT VendorId ,@DissectionId,CategoryId from tblProduct where tblProduct.productId=@ProductId
END
--End inserted
IF (SELECT Count(*) FROM DELETED) > 0
BEGIN
SELECT
@ProductId=tblProductDissection.ProductId,
@DissectionId=tblProductDissection.DissectionId
FROM DELETED tblProductDissection
SELECT @VendorId=VendorId from tblProduct where tblProduct.productId=@ProductId
SELECT @CategoryId=CategoryId from tblProduct where tblProduct.productId=@ProductId
DELETE from dbo.tblDissection_Cat_Vendor
WHERE DissectionId=@DissectionId and
CategoryId=@CategoryId and VendorId=@VendorId
END
END
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment