November 18, 2009 at 9:49 am
i have the following trigger created on a table, I need to know if it's possible and how I would do this. what i need to know is if a column is updated then fire the trigger, the insert part works fine as it stands now. but I don't want it to create the new record in the table the trigger is writing to if any column is updated on the record.
I hope this makes since.
here is the trigger statement
ALTER trigger [dbo].[badge_Print_history] on
[dbo].[ev_registrant]
for Insert, update
as
insert [dbo].[client_gcsaa_badge_print_history]
(
s10_reg_badge_print_date,
s10_reg_cst_key,
s10_reg_key,
s10_reg_evt_key,
s10_reg_add_user,
s10_reg_change_user,
s10_reg_change_date,
s10_reg_delete_flag,
s10_reg_ivd_key
)
Select
reg_badge_print_date,
reg_cst_key,
reg_key,
reg_evt_key,
reg_add_user,
reg_change_user,
reg_change_date,
reg_delete_flag,
reg_ivd_key
from Inserted[/i]
November 18, 2009 at 10:38 am
Alter the trigger to work only for Insert (remove update).
Change for Insert, update to for Insert.
You create another trigger for update if you want any other transaction to happen on update..
--Jus
November 18, 2009 at 12:01 pm
Do want to log the update if the data in the column does not change. For example:
Update Table
Set column = column
Will cause an update trigger to fire even if you use the If UPDATE(Column) syntax.
My recommendation would be something along these lines:
ALTER trigger [dbo].[badge_Print_history] on [dbo].[ev_registrant]
for Insert, update
AS
/*
This should only return rows that are updates.
*/
IF EXISTS(SELECT 1 FROM deleted AS D JOIN inserted I ON D.primary_key = I.primary_key)
BEGIN
INSERT [dbo].[client_gcsaa_badge_print_history]
(
s10_reg_badge_print_date,
s10_reg_cst_key,
s10_reg_key,
s10_reg_evt_key,
s10_reg_add_user,
s10_reg_change_user,
s10_reg_change_date,
s10_reg_delete_flag,
s10_reg_ivd_key
)
Select
reg_badge_print_date,
reg_cst_key,
reg_key,
reg_evt_key,
reg_add_user,
reg_change_user,
reg_change_date,
reg_delete_flag,
reg_ivd_key
from
Inserted AS I JOIN
deleted AS D ON
I.primary_key = D.primary_key AND
/*
This means the data in the desired column
changed.
*/
I.column_updated <> D.column_updated
END
ELSE -- insert
BEGIN
INSERT [dbo].[client_gcsaa_badge_print_history]
(
s10_reg_badge_print_date,
s10_reg_cst_key,
s10_reg_key,
s10_reg_evt_key,
s10_reg_add_user,
s10_reg_change_user,
s10_reg_change_date,
s10_reg_delete_flag,
s10_reg_ivd_key
)
Select
reg_badge_print_date,
reg_cst_key,
reg_key,
reg_evt_key,
reg_add_user,
reg_change_user,
reg_change_date,
reg_delete_flag,
reg_ivd_key
from
Inserted
END
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply