trigger to determine if any column changed

Is there a way to write a trigger to determine when any column in a record has changed and then update a "edit_datetime" column?
LVL 1
HLRosenbergerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
If you do not need to know if a column was updated, you can create a trigger that occurs instead of an update or after an update (but prior to the change committing). See this link: http://msdn.microsoft.com/en-us/library/ms189799.aspx
If you need to know a specific column changed you can use the UPDATE ( <column name> ) syntax. http://msdn.microsoft.com/en-us/library/ms187326.aspx
0
Scott PletcherSenior DBACommented:
Sure.  You'd probably want to generate the code using sys.columns rather than writing it by hand, but the general structure would look like this:


CREATE TRIGGER <trigger_name>
ON dbo.<table_name>
AFTER UPDATE
AS
SET NOCOUNT ON;
UPDATE tn
SET edit_datetime = GETDATE()
FROM inserted i
INNER JOIN deleted d ON
    d.key_col = i.key_col
INNER JOIN dbo.<table_name> tn ON
    tn.key_col = i.key_col
WHERE
    ISNULL(i.data_col1, '~~') <> ISNULL(d.data_col1, '~~') OR
    ISNULL(i.data_col2, '~~') <> ISNULL(d.data_col2, '~~') OR
    ISNULL(i.data_col3, -999) <> ISNULL(d.data_col3, -999) --OR ...other columns

GO
0
Randy Knight, MCMPrincipal ConsultantCommented:
Instead of rolling you own code, check out the SQL Server change tracking feature.  

http://msdn.microsoft.com/en-us/library/bb933875.aspx
0
HLRosenbergerAuthor Commented:
I went with this:  http://www.codeproject.com/Articles/21068/Audit-Trail-Generator-for-Microsoft-SQL

works great.   The SQL Server change tracking feature, I believe, tracks based on the logged in SQL user account;  I need to track based on my own "users" table; I was able to modify this  "Audit-Trail-Generator-for-Microsoft-SQL " to do this.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HLRosenbergerAuthor Commented:
This is the solution I went with.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.