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
 General SQL Server Forums
 New to SQL Server Programming
 trigger after update

Author  Topic 

Msandlana
Starting Member

33 Posts

Posted - 2008-01-08 : 09:50:05
Hi Everyone I want to update the date after You update franchiseeId and the following code does not work

CREAT TRIGGER [dbo].[trgFranchisee]
ON [dbo].[Franchisee]
AFTER UPDATE
AS

Declare @intFranchiseeId int
Declare @intUpdated int

Set @intUpdated = isnull((Select count(*) From Updated),0)

if @intUpdated > 0
begin

SELECT @intFranchiseeId = FranchiseeId
FROM Updated

Update Franchisee Set DateUpdated = Getdate() Where FranchiseeId = @intFranchiseeId
end

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-08 : 10:03:00
Do like this:-

CREATE TRIGGER [dbo].[trgFranchisee]
ON [dbo].[Franchisee]
AFTER UPDATE
AS

IF EXISTS (SELECT * FROM INSERTED)
BEGIN
UPDATE f
SET f.DateUpdated=GETDATE()
FROM Franchisee f
INNER JOIN INSERTED i
ON i.FranchiseeId=f.FranchiseeId
END
GO



You dont have updated table.Instead update is done internally as insert followed by delete so that you get old values in DELETED and new values in INSERTED. Also, its better to define default constraint on DateUpdated as GETDATE() rather than writing a trigger as performance with trgiggers will be much slower.
Go to Top of Page
   

- Advertisement -