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
 SQL Server Development (2000)
 copying values from one table to another

Author  Topic 

superblades
Starting Member

7 Posts

Posted - 2007-01-03 : 07:27:07
Hi Again,

im wondering if its possible to copy an old value to another table for audit purposes when its changed.

e.g.

under tbl_reqs there is a field called fldstatus

when this field is changed i want it to copy what the field was before and insert it into my tbl_oldstatus table as well as the date it was entered and the corresponsing record number.

e.g.

before:-

tbl_reqs

reqnumber - status

123456 - ordered

after:-

reqnumber - status

123456 - completed


tbloldstatus

reqnumber - old status - change date

123456 - ordered - 03/01/2007


etc.

Once again i havent got a clue how to do this as im still a newbie!

any help is most appreciated

Regards

Brendan Tate

Kristen
Test

22859 Posts

Posted - 2007-01-03 : 07:58:37
You could use a trigger for this - whenever a record is changed the Trigger could copy the Old record (or the New record if you prefer) to an "Audit table"

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

Kristen
Go to Top of Page

superblades
Starting Member

7 Posts

Posted - 2007-01-03 : 10:24:04
Thanks Kristen ive had a look at the code you have created on that FAQ site but i dont understand what it does ive already got the old status table made, what syntax etc would i need in the trigger?

Sorry :)

Brendan
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-01-03 : 13:44:37
Just to be sure we're on the right wavelength the relevant code you need is here:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=170215

This will insert into your Audit Table the original values from all the columns in the source table - plus two additional leading columns for the Type of the action (Update or Delete) and the current Date/Time. You can change the SELECT list to only insert the columns you need.

If that isn't clear then I reckon you need to read up on Triggers and possibly some more basic syntax stuff on INSERT.

Kristen
Go to Top of Page
   

- Advertisement -