I am trying to insert records into my table named "CBM_Count_Daily" using trigger upon data insert in table named "Shipment_alloc_request".
Below is the trigger I have implemented in my DB.
But when a new record is inserted in the "Shipment_alloc_request" table, all the old records are also being inserted in my new table "CBM_Count_Daily" which should not be the case.
I hope you guys got my question. I want only the newly records created to be inserted in my query. Can someone please let me know or modify the trigger design above accordingly as to what i am missing
What I have tried:
create TRIGGER [dbo].[CBM_Out] ON [dbo].[shipment_alloc_request]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
insert into CBM_Count_Daily
(
Trans_Date,
Company,
Warehouse,
CBM_In,
CBM_Out,
Total_CBM,
Branch,
Group_Company
)
SELECT
SAR.DATE_TIME_STAMP,
SAR.COMPANY,
SH.WAREHOUSE,
NULL,
SUM(SAR.ALLOCATED_QTY*IUOM.LENGTH) AS CBM_Out,
NULL,
CASE when SH.warehouse IN ('C','D') THEN 'DIP' ELSE 'DIC'
END AS BRANCH,
CM.IP_ADDRESS
FROM SHIPMENT_ALLOC_REQUEST SAR
INNER JOIN ITEM_UNIT_OF_MEASURE IUOM ON SAR.ITEM = IUOM.ITEM
INNER JOIN SHIPMENT_HEADER SH ON SAR.INTERNAL_SHIPMENT_NUM = SH.INTERNAL_SHIPMENT_NUM
INNER JOIN COMPANY CM ON SAR.COMPANY = CM.COMPANY
GROUP BY SAR.DATE_TIME_STAMP,SAR.COMPANY,SH.warehouse,CM.IP_ADDRESS;
END