SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 copying values from one table to another
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

superblades
Starting Member

7 Posts

Posted - 01/03/2007 :  07:27:07  Show Profile  Reply with Quote
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

United Kingdom
22403 Posts

Posted - 01/03/2007 :  07:58:37  Show Profile  Reply with Quote
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 - 01/03/2007 :  10:24:04  Show Profile  Reply with Quote
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

United Kingdom
22403 Posts

Posted - 01/03/2007 :  13:44:37  Show Profile  Reply with Quote
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

Edited by - Kristen on 01/14/2012 08:31:07
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000