Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Last Updated Trigger

Posted on 2003-10-28
2
1,010 Views
Last Modified: 2008-01-16
Hello, I have scripted a trigger and it works, but I am concerned about the overhead of doing it this way. Ideally I would like to do away with the Cursor, but am not sure it can be done for what I am trying to accomplish.  The point of this trigger is to update the dateTime field "lastTouched" to reflect any time information is added or modified.

CREATE TRIGGER update_row_timestamp ON dbo.tableName
AFTER INSERT,UPDATE
AS
 
DECLARE @rowID  int
 
DECLARE curModifiedData CURSOR FOR
SELECT rowID
FROM INSERTED
 
OPEN curModifiedData
FETCH NEXT FROM curModifiedData INTO @rowID
WHILE (@@FETCH_STATUS <> -1)
BEGIN
 UPDATE tableName
 SET lastTouched = getDate()
 WHERE rowID = @rowID
 FETCH NEXT FROM curModifiedData INTO @rowID
END
 
CLOSE curModifiedData
DEALLOCATE curModifiedData

In case you need to know, lastTouched is a dateTime, rowID is a primary key on the table.

Thanks for giving this a once over!
0
Comment
Question by:3gg
2 Comments
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9633506
this is whati do in my triggers:


UPDATE a
   SET LastUpdated=GETDATE()
   FROM MyTable a
      INNER JOIN INSERTED b ON a.ID=b.ID
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 50 total points
ID: 9633517
no you don't need a cursor...

CREATE TRIGGER update_row_timestamp ON dbo.tableName
AFTER INSERT,UPDATE
AS
 
DECLARE @rowID  int
 
 UPDATE tableName
 SET lastTouched = getDate()
 From TableName as A, Inserted as I
 on A.Rowid=I.Rowid

Return

go
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question