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


Last Field updated


Last Field updated

Author
Message
stricknyn
stricknyn
Mr or Mrs. 500
Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)

Group: General Forum Members
Points: 564 Visits: 690
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



Adam Haines
Adam Haines
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3316 Visits: 3135
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
Adam Haines
Adam Haines
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3316 Visits: 3135
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
stricknyn
stricknyn
Mr or Mrs. 500
Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)

Group: General Forum Members
Points: 564 Visits: 690
Thanks guys,

I'm going to test it out!

Strick



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