Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Querying a table with a "history" table

Author  Topic 

nlocklin
Yak Posting Veteran

69 Posts

Posted - 2006-10-06 : 15:21:25
I have two tables:

Table One
---------
RecordID
Column A
Column B
Column C

Table Two
---------
ChangeID
RecordID
ColumnChanged
OldValue
NewValue
DtChanged

Table Two is basically a change history table. Any time a value in a column in table One is changed, a record is inserted into table Two indicating the RecordID of the record that was changed, which column, what the old and new values are, and when it was changed.

I have been given the assignment of creating a view that will show the original values for the records in table One as they were inserted. I've managed to come up with something that works but it's very slow and I'm almost certain there's a better way. Can anyone help me find the most efficient solution?

Thanks in advance!

--
"It's not that I'm lazy... it's that I just don't care."

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-10-06 : 17:40:07
It might help if you posted the code you are having trouble with.

I doubt that you will get very good performacne, since the second table is an entity attribute value design. That almost always results in extremely poor query performance.




CODO ERGO SUM
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2006-10-07 : 01:13:19
Is a newly inserted record also inserted into table two? Or is it only inserted into table two after the original records is modified.
In other words, does table two hold a complete set of records including all the records from table one?

"Once you establish possibility, all that remains are desire and determination."
Go to Top of Page

nlocklin
Yak Posting Veteran

69 Posts

Posted - 2006-10-09 : 10:52:39
quote:
Originally posted by blindman
Is a newly inserted record also inserted into table two? Or is it only inserted into table two after the original records is modified.
In other words, does table two hold a complete set of records including all the records from table one?



Table two only contains the changes, not the initial insert.

--
"It's not that I'm lazy... it's that I just don't care."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-10-09 : 11:03:51
I guess most of us do this...but to make sure the trigger that fires does not impact any other OLTP performance, I whoulds never have an index on the history table.....so the table will scan

So....if you want to do reporting, make another table and index that


Also, you are taking the more complicated route...I just move the entire row to history, and not worrying about what column changed...you can figure that out later

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

nforakis
Starting Member

1 Post

Posted - 2006-10-17 : 09:06:40
I'm curious how you figured out which cloumn changed and how to just insert the changes into the history table?


quote:
Originally posted by nlocklin

I have two tables:

Table One
---------
RecordID
Column A
Column B
Column C

Table Two
---------
ChangeID
RecordID
ColumnChanged
OldValue
NewValue
DtChanged

Table Two is basically a change history table. Any time a value in a column in table One is changed, a record is inserted into table Two indicating the RecordID of the record that was changed, which column, what the old and new values are, and when it was changed.

I have been given the assignment of creating a view that will show the original values for the records in table One as they were inserted. I've managed to come up with something that works but it's very slow and I'm almost certain there's a better way. Can anyone help me find the most efficient solution?

Thanks in advance!

--
"It's not that I'm lazy... it's that I just don't care."

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-10-17 : 13:45:25
I think Audit Tables that record Column / Before / After data are a NIGHTMARE!

See: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=audit%20changes

Kristen
Go to Top of Page
   

- Advertisement -