?
Solved

Track Field Change SQL 2008

Posted on 2014-08-12
2
Medium Priority
?
271 Views
Last Modified: 2014-08-12
This one should be easy. I've done a fair bit of dabbling in SQL with standard statements (select, insert...), views, and even a stored proc or 2, but now I need a bit more.

What I need is to watch a field in a table, any time that changes write an insert into another table with that effective date.

Our front office system doesn't track a Vehicle's cost center effective date, so I thought I could use a trigger to do it myself. The destination table is stupid simple 2 fields, Unit and a datetime stamp. I just have never done this before and don't know where to start.  I find database triggers under Programmability in Management studio, but when I right click I don't get an option for new.

We are on SQL 2005.
0
Comment
Question by:bhieb
2 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 40256295
Aircode so take with a grain of salt...

CREATE TRIGGER ON SourceTable
AFTER INSERT, UPDATE
AS
BEGIN

INSERT INTO DestinationTable (Unit, DateTimeStamp)
SELECT I.Unit, GETDATE()
FROM Inserted AS I
LEFT OUTER JOIN deleted AS D
   ON I.SourceTableKey = D.SourceTableKey
WHERE D.SourceTableKey IS NULL   -- An insert was made so no deleted table exists
   OR I.Unit <> D.Unit                         -- An update was made so just check that the specific column was changed

END

Open in new window

0
 

Author Comment

by:bhieb
ID: 40256848
Thanks. I had to tweak it some but works great.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question