Hi,
I need a help regarding SQL Triggers. I think I'm making a mistake that is why it is not executing well.
I'm developing an application which have PURCHASE, PURCHASEDETAILS, LOGS tables. What I need here is that when I make a purchase and when an entry is inserted or updated in PURCHASEDETAILS table a trigger should be fired and value of some fields of PURCHASEDETAILS shall be copied to LOGS table.
An image of the tables are shown below for a reference:
I need two triggers. One in PURCHASEDETAILS table to copy the ProductId, StockedQuantity, PurchaseId data to Logs table after insert.
Another trigger shall fire to update the stock based on the ProductId (as shown in the image). Unfortunately I'm not getting the desired result.
Can anyone help me?
Thank you.
CREATE TABLE [dbo].Suppliers ON [PRIMARY]
GO
CREATE TABLE [dbo].Category ON [PRIMARY]
GO
CREATE TABLE [dbo].Products ON [PRIMARY]
GO
ALTER TABLE [dbo].[Products] ADD CONSTRAINT [DF_Products_Discontinued] DEFAULT ((0)) FOR [Discontinued]
GO
ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [FK_Products_Category] FOREIGN KEY([Categoryid])
REFERENCES [dbo].[Category] ([Categoryid])
GO
ALTER TABLE [dbo].[Products] CHECK CONSTRAINT [FK_Products_Category]
GO
ALTER TABLE [dbo].[Products] WITH CHECK ADD CONSTRAINT [FK_Products_Suppliers] FOREIGN KEY([Supplierid])
REFERENCES [dbo].[Suppliers] ([Supplierid])
GO
ALTER TABLE [dbo].[Products] CHECK CONSTRAINT [FK_Products_Suppliers]
GO
CREATE TABLE [dbo].Purchase ON [PRIMARY]
GO
ALTER TABLE [dbo].[Purchase] WITH CHECK ADD CONSTRAINT [FK_Purchase_Suppliers] FOREIGN KEY([Supplierid])
REFERENCES [dbo].[Suppliers] ([Supplierid])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Purchase] CHECK CONSTRAINT [FK_Purchase_Suppliers]
GO
CREATE TABLE [dbo].PurchaseDetails ON [PRIMARY]
GO
ALTER TABLE [dbo].[PurchaseDetails] ADD CONSTRAINT [DF_PurchaseDetails_RejectedQty] DEFAULT ((0)) FOR [RejectedQty]
GO
ALTER TABLE [dbo].[PurchaseDetails] ADD CONSTRAINT [DF_PurchaseDetails_TaxPrice] DEFAULT ((0)) FOR [TaxAmt]
GO
ALTER TABLE [dbo].[PurchaseDetails] ADD CONSTRAINT [DF_PurchaseDetails_Discount] DEFAULT ((0)) FOR [Discount]
GO
ALTER TABLE [dbo].[PurchaseDetails] WITH CHECK ADD CONSTRAINT [FK_PurchaseDetails_Purchase] FOREIGN KEY([PurchaseId])
REFERENCES [dbo].[Purchase] ([PurchaseId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[PurchaseDetails] CHECK CONSTRAINT [FK_PurchaseDetails_Purchase]
GO
CREATE TABLE [dbo].InventoryLogs ON [PRIMARY]
GO
ALTER TABLE [dbo].[InventoryLogs] ADD CONSTRAINT [DF_PurchaseLogs_Stock] DEFAULT ((0)) FOR [Stock]
GO
INSERT STATEMENTS
CREATE PROCEDURE [dbo].[Purchase_Add]
(@purchasedate date,@supplierid bigint,@freightcharges decimal(18,3),@createdby varchar(50), @purchasedetails Add_PurchaseDetails Readonly)
AS
DECLARE @purchaseid as bigint;
BEGIN TRY
BEGIN TRANSACTION
INSERT INTO Purchase(PurchaseDate,Supplierid,FreightCharges,Createdby,Createdon)
VALUES(@purchasedate,@supplierid,@freightcharges,@createdby,getdate());
SET @purchaseid=IDENT_CURRENT('Purchase');
INSERT INTO PurchaseDetails
SELECT @purchaseid,Productid,OrderQty,ReceivedQty,RejectedQty,StockedQty,UnitPrice,TaxAmt,Discount
FROM @purchasedetails;
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT>
0
ROLLBACK TRANSACTION
TRIGGER
Unfortunately, this was not working
INSERT INTO InventoryLogs(ProductId,Movement,Quantity,TransactionId,Stock)
SELECT Productid,'IN',StockedQty,PurchaseId,StockedQty
FROM inserted i
WHERE Productid=i.Productid;