Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Old Value in Update Trigger 2

Status
Not open for further replies.

hi2jenny

Programmer
Mar 30, 2005
14
US
I wonder how to use UPDATE trigger and get the old value of some column before update. I need to insert that value to a table. I created a trigger but it doesn't return any value. I have the print statement but nothing gets printed.

CREATE TRIGGER Test_Update ON Test FOR UPDATE
AS
DECLARE
@rowID int,
@date DATETIME

SELECT @rowID = AutoID, @date = Date
FROM Deleted

print 'rowID = ' + Cast(@rowID as nvarchar(20))
 
i belive that the old vaule is stored in the deleted table so...

select column from deleted
 
here is what i did and it worked

Code:
create table bob (
		idnumber int,
		n	int
)
create table bob2 (
		idnumber int,
		n	int
)
Code:
CREATE TRIGGER Test_Update ON bob FOR UPDATE
AS
    DECLARE
    @rowID int,
    @date int

    SELECT @rowID = idnumber, @date = n
    FROM Deleted
insert into bob2 select  @rowid, @date
Code:
insert into bob select 1, 5

update bob set n = 6 where idnumber = 1
select * from bob
select * from bob2
 
Not trying to be picky but that above trigger would not work if you had a multi-row update. You'd need to do something like this:

Code:
CREATE TRIGGER Test_Update ON bob FOR UPDATE
AS

insert into bob2 (idnumber, n)
SELECT idnumber, n
FROM Deleted

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top