Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Last Field updated Expand / Collapse
Author
Message
Posted Tuesday, January 29, 2008 2:25 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, February 9, 2014 10:16 PM
Points: 265, Visits: 587
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
Posted Tuesday, January 29, 2008 2:46 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 11:02 AM
Points: 2,278, Visits: 3,065
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
Posted Tuesday, January 29, 2008 2:51 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 11:02 AM
Points: 2,278, Visits: 3,065
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
Posted Thursday, January 31, 2008 7:37 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, February 9, 2014 10:16 PM
Points: 265, Visits: 587
Thanks guys,

I'm going to test it out!

Strick



Post #449942
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse