SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Avoid trigger inserting duplicate records


Avoid trigger inserting duplicate records

Author
Message
randheerb
randheerb
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 47

Hi
I have created a trigger when a value is inserted to the main table dbo.MVF_Transport_Register, it updtes the dbo.MVF_Transport_Acc table with that record. Also when a value is changed on the main table dbo.MVF_Transport_Register it updates the particular field value accordingly on the other dbo.MVF_Transport_Acc.. Problem I have is my trigger inserts duplicte record on to the dbo.MVF_Transport_Acc.
Just want to check how can I amend this trigger so it checks dbo.MVF_Transport_Acc to see if the record exsists before inserting the record. If the record exsists it shouldnt insert duplicate records. Can some one pelase check my trigger an advice how to sort this thanks in advance big help
Primary key is Order_No for both tables


ALTER TRIGGER [dbo].[UpdateTransport]
ON [dbo].[MVF_Transport_Register]
AFTER UPDATE
AS
BEGIN
UPDATE c SET c.Record_Status = i.Record_status
FROM MVF_SYSTEMS.dbo.MVF_Transport_Acc AS c
INNER JOIN inserted AS i
ON c.Order_No = i.Order_No
AND c.Record_Status <> i.Record_Status
WHERE i.Record_Status IN ( 'CONFIRMED', 'AMENDED')
AND i.Price > 0;

IF @@ROWCOUNT = 0
BEGIN

INSERT MVF_SYSTEMS.dbo.MVF_Transport_Acc
([Order_No], [Record_Status], [Notes], [Transport_Supplier],
[Surcharge], [Vendor_No], [Pallets], [Amend_Pallets],[Price],
[Input_Date], [Amend_Price], [Transport_Invoice_No],[Transport_Job_No],
[Amend_Date], [Report_Price], [Return_Price], [Report_Date])
SELECT i.[Order_No], i.[Record_Status], i.[Notes], i.[Transport_Supplier],
i.[Surcharge], i.[Vendor_No], i.[Pallets], i.[Amend_Pallets],
i.[Price], i.[Input_Date], i.[Amend_Price], i.[Transport_Invoice_No],
i.[Transport_Job_No], i.[Amend_Date], i.[Report_Price],
i.[Return_Price], i.[Report_Date]
FROM inserted AS i
WHERE i.Record_Status IN ( 'CONFIRMED', 'AMENDED')AND i.Price > 0;
END
END


Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)SSC Guru (194K reputation)

Group: General Forum Members
Points: 194915 Visits: 23765
What is the primary key on table MVF_Transport_Acc?


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
randheerb
randheerb
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 47
Hi ,
I have added the quotes as you suggested, I havent set a primary key on the second table (dbo.MVF_Transport_Acc) this would be the reason why it is inserting duplicate records, I will try this
thanks for your reply
ZZartin
ZZartin
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22297 Visits: 15937
I'd be willing to bet this is the problem.

AND c.Record_Status <> i.Record_Status 


Anytime the record gets updated but the status doesn't change the update will not run since the status in the second table will already match, so that row count will be zero and it'll run the insert.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search