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 |
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 ONset QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[update_ts_pdu] ON [dbo].[tbl_pdu]AFTER INSERT,UPDATEASDECLARE @pointID intDECLARE curModifiedData CURSOR FORSELECT pointIDFROM INSERTEDOPEN curModifiedDataFETCH NEXT FROM curModifiedData INTO @pointIDWHILE (@@FETCH_STATUS <> -1)BEGINUPDATE tbl_pduSET updateTimestamp = getDate()WHERE pointID = @pointIDFETCH NEXT FROM curModifiedData INTO @pointIDENDCLOSE curModifiedDataDEALLOCATE 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 scriptcreate TRIGGER [dbo].[update_ts_pdu] ON [dbo].[tbl_pdu]AFTER INSERT,UPDATEASDECLARE @pointID intDECLARE curModifiedData CURSOR FORSELECT pointIDFROM INSERTEDOPEN curModifiedDataFETCH NEXT FROM curModifiedData INTO @pointIDWHILE @@FETCH_STATUS=0BEGINUPDATE tbl_pduSET updateTimestamp = getDate()WHERE pointID = @pointIDFETCH NEXT FROM curModifiedData INTO @pointIDENDCLOSE curModifiedDataDEALLOCATE curModifiedDataAlso 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.. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-01-14 : 02:25:03
|
why use cursor? go for set based solutionAlso its enough to do this only for updatesfor handling inserts. you can make a default constraint on updateTimestamp column in table with getdate() value as belowALTER TABLE [dbo].[tbl_pdu] ADD CONSTRAINT DF_tbl_pdu_updateTimestamp DEFAULT GETDATE() FOR updateTimestampset ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[update_ts_pdu] ON [dbo].[tbl_pdu]AFTER UPDATEASBEGINIF NOT UPDATE(updateTimestamp)BEGINUPDATE tSET t.updateTimestamp = getDate()FROM tbl_pdu tJOIN INSERTED iON i.pointID = t.pointIDENDEND |
 |
|
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. |
 |
|
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 exactlyoh you mean using a CASE..WHEN? |
 |
|
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? |
 |
|
|
|
|
|
|