Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
Last Field updated
Last Field updated
Rate Topic
Display Mode
Topic Options
Author
Message
stricknyn
stricknyn
Posted Tuesday, January 29, 2008 2:25 PM
SSC Veteran
Group: General Forum Members
Last Login: Tuesday, April 30, 2013 12:32 PM
Points: 264,
Visits: 577
Hello everyone,
I have two tables, a product table and a product history table. I want the product history table to be updated with a new when a record when the product table is updated.
Sounds like I need a trigger. However I'm not sure how I would go about grabbing the last field updated. Note, there are no timestamps in the product table. The Product history table contains this with a relationship on product id. So for example:
1) I change the ProductName field from "TestA" to "TestB" (in Product Table)
2) Trigger fires (Product Table)
3) I want the field ProductName to be captured along with the initial value (TestA) and the new value (TestB) (I'll probably use 3 variables)
4) My insert into will insert a new record in the update history table with the following info: (modifieddate, fieldname (this is field that was updated), initial value, new value, user updated, product id)
The T-SQL for the inserts and update i got down, but what i'm trying to figure out if there is a way I can grab the record that just got updated along with the initial value, new value, and field name. Please note, that I don't want to add a modified date in the product table field to avoid the redundancy (its in the product history table) This is the method I'll end up using if I don't figure out how to do it the way I want to though.
Hope I'm making sense.
Thanks
Strick
Post #449089
Adam Haines
Adam Haines
Posted Tuesday, January 29, 2008 2:46 PM
SSCrazy
Group: General Forum Members
Last Login: 2 days ago @ 2:18 PM
Points: 2,278,
Visits: 2,999
For this you need two tiggers on that will add new products to the history table (after insert) and another to insert a new record when a product is updated. You can access the "old" data by using the deleted table. When an update occurs the new data is available via the inserted table and the data before update is available in the deleted table.
This link has a basic article on setting up triggers:
http://www.4guysfromrolla.com/webtech/091901-1.shtml
My blog:
http://jahaines.blogspot.com
Post #449099
Adam Haines
Adam Haines
Posted Tuesday, January 29, 2008 2:51 PM
SSCrazy
Group: General Forum Members
Last Login: 2 days ago @ 2:18 PM
Points: 2,278,
Visits: 2,999
As a side note, you can join the inserted and deleted tables on your key to get all the values need in a single query.
My blog:
http://jahaines.blogspot.com
Post #449101
stricknyn
stricknyn
Posted Thursday, January 31, 2008 7:37 AM
SSC Veteran
Group: General Forum Members
Last Login: Tuesday, April 30, 2013 12:32 PM
Points: 264,
Visits: 577
Thanks guys,
I'm going to test it out!
Strick
Post #449942
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.