[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Last Updated Trigger

Posted on 2003-10-28
2
Medium Priority
?
1,016 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
[X]
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
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 200 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

656 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