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
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 trigger - update field after insert

Author  Topic 

pupp
Starting Member

1 Post

Posted - 2010-01-13 : 20:46:59
i am attempting to update each insert with the current time stamp (getDate()). unfortunately i am very new to triggers and haven't wrapped my head around every as of yet. after some time reading and googling, i was able to come up with a trigger that does what I am looking for. However, it works great when 1 table is being inserted but not when several are. I think I have problems with the CURSOR and @@FETCH_STATUS. My trigger is:


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[update_ts_pdu] ON [dbo].[tbl_pdu]
AFTER INSERT,UPDATE
AS

DECLARE @pointID int

DECLARE curModifiedData CURSOR FOR
SELECT pointID
FROM INSERTED

OPEN curModifiedData
FETCH NEXT FROM curModifiedData INTO @pointID
WHILE (@@FETCH_STATUS <> -1)
BEGIN
UPDATE tbl_pdu
SET updateTimestamp = getDate()
WHERE pointID = @pointID
FETCH NEXT FROM curModifiedData INTO @pointID
END

CLOSE curModifiedData
DEALLOCATE curModifiedData



I need this trigger to work on multiple tables. Is there a better way to accomplish what I am looking for? Thanks!

gangadhara.ms
Aged Yak Warrior

549 Posts

Posted - 2010-01-14 : 01:49:43
Hi Pupp,

Please use this script to update updatetimestamp to getdate.
There is small change in script


create TRIGGER [dbo].[update_ts_pdu] ON [dbo].[tbl_pdu]
AFTER INSERT,UPDATE
AS

DECLARE @pointID int

DECLARE curModifiedData CURSOR FOR
SELECT pointID
FROM INSERTED

OPEN curModifiedData
FETCH NEXT FROM curModifiedData INTO @pointID
WHILE @@FETCH_STATUS=0
BEGIN
UPDATE tbl_pdu
SET updateTimestamp = getDate()
WHERE pointID = @pointID
FETCH NEXT FROM curModifiedData INTO @pointID
END

CLOSE curModifiedData
DEALLOCATE curModifiedData


Also in order to work on multiple tables you will have to attach the same trigger on multiple tables, as trigger will work on particular table attached.

Hope this will help you out..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 02:25:03
why use cursor? go for set based solution
Also its enough to do this only for updates
for handling inserts. you can make a default constraint on updateTimestamp column in table with getdate() value as below

ALTER TABLE [dbo].[tbl_pdu] ADD CONSTRAINT DF_tbl_pdu_updateTimestamp DEFAULT GETDATE() FOR updateTimestamp


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[update_ts_pdu] ON [dbo].[tbl_pdu]
AFTER UPDATE
AS

BEGIN

IF NOT UPDATE(updateTimestamp)
BEGIN
UPDATE t
SET t.updateTimestamp = getDate()
FROM tbl_pdu t
JOIN INSERTED i
ON i.pointID = t.pointID
END

END

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-14 : 03:16:30
"IF NOT UPDATE(updateTimestamp)"

That's an interesting idea - allow the application to set the updateTimestamp if it wants to?

Although is there some risk that the App only sets it on some of the rows in the update set? ... there I am being pedantic again

We stuff the date in via a trigger on all Inserts / Updates, but actually the SProc probably does it two most of the time. This would save some time, and TLog space.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-14 : 05:05:24
quote:
Originally posted by Kristen

"IF NOT UPDATE(updateTimestamp)"

That's an interesting idea - allow the application to set the updateTimestamp if it wants to?

Although is there some risk that the App only sets it on some of the rows in the update set? ... there I am being pedantic again

We stuff the date in via a trigger on all Inserts / Updates, but actually the SProc probably does it two most of the time. This would save some time, and TLog space.


Yup exactly

oh you mean using a CASE..WHEN?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-01-14 : 06:58:09
"oh you mean using a CASE..WHEN?"

Sorry, not sure which aspect you are referring to?
Go to Top of Page
   

- Advertisement -