Jump to content

Recommended Posts

Posted

Please excuse my basic knowledge of SQL.

I have two tables on a MS SQL2000 sp4 database

Table name - Details

id

forename

Surname

dob

email

Table name - DetailsHistory

id

forename

Surname

dob

email

When somebody changes (Update) a row in Details, I want to create a trigger to : Before the change is made copy the current row to the DetailsHistory table

here is my code

CREATE TRIGGER savetohistory 
on [dbo].[Details] for update
as
BEGIN
INSERT into DetailsHistory select * from Details where id = Details.id
END;

but this copies ALL the rows not just the current row :(

Taggs


Posted

You want to select from the DELETED table, to copy out the row(s) to be updated.

This will ignore deletes and inserts, only fire for updates.

CREATE TRIGGER savetohistory 
on [dbo].[Details] for update
as
BEGIN
INSERT into DetailsHistory select * from DELETED
END;

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...