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


Create a trigger on a column


Create a trigger on a column

Author
Message
Stephen crocker
Stephen crocker
SSC Veteran
SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)

Group: General Forum Members
Points: 218 Visits: 324
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]
[/i]
Jus
Jus
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 275
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
Jack Corbett
  Jack Corbett
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19172 Visits: 14900
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

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
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