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.
| 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 UPDATEASDeclare @intFranchiseeId intDeclare @intUpdated intSet @intUpdated = isnull((Select count(*) From Updated),0)if @intUpdated > 0beginSELECT @intFranchiseeId = FranchiseeIdFROM UpdatedUpdate Franchisee Set DateUpdated = Getdate() Where FranchiseeId = @intFranchiseeIdend |
|
|
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 UPDATEASIF EXISTS (SELECT * FROM INSERTED)BEGINUPDATE fSET f.DateUpdated=GETDATE()FROM Franchisee fINNER JOIN INSERTED iON i.FranchiseeId=f.FranchiseeIdENDGO 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. |
 |
|
|
|
|
|