Solved

Last Updated Trigger

Posted on 2003-10-28
2
1,011 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 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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