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 2008 Forums
 Transact-SQL (2008)
 how can to save the last values before updated

Author  Topic 

programer
Posting Yak Master

221 Posts

Posted - 2014-06-22 : 13:49:06
Hi,

Declare @ByName nvarchar(500)
Declare @Note nvarchar(4000)

SELECT Username from inserted inner join aspnet_Users
on inserted.UserId=aspnet_Users.UserId

SET @ByName=(SELECT Username from inserted inner join aspnet_Users
on inserted.UserId=aspnet_Users.UserId)

IF UPDATE(FirstName)
SET @Note='User details updated: First name '

BEGIN
INSERT INTO tbl_Audit (UserId, Type,Date,ByName,Note) VALUES ((SELECT UserId from inserted),'1',GETDATE(),@ByName,@Note)
END
END


I have FirstName: programer

Now I want to use store the last values before will updated.

If updated FirstName to programer2

then I need to get also programer and programer2

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-06-22 : 17:12:15
[code]INSERT dbo.tbl_Audit
(
UserID,
Type,
Date,
ByName,
Note
)
SELECT i.UserID,
'1',
GETDATE(),
i.Username,
'User details updated: First name (old) ' + d.Firstname + ' (new) ' + i.FirstName
FROM deleted AS d
INNER JOIN inserted AS i ON i.UserId = d.UserId;[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
   

- Advertisement -