April 1, 2010 at 2:26 pm
All,
I have the following trigger which is built to update date_time_mod whenever an update is fired on the table, however the trigger is updating the date_time_mod of the first row only when a mass update is fired. Is there anyway to get around to update the date_time_mod for all the rows effected by the mass update statement.
ALTER TRIGGER [dbo].[TRG_table_DT_MOD]
ON [dbo].
AFTER INSERT, UPDATE
NOT FOR REPLICATION
AS
BEGIN
SET NOCOUNT ON
IF TRIGGER_NESTLEVEL() > 1
RETURN
declare @table_id bigint
select @table_id=table_id from INSERTED
UPDATE table
SET DATE_TIME_MOD = GETDATE() WHERE table_id = @table_id
end
GO
below is the mass update statement which effects 200 rows but the date_time_mod is updated only for the first row.
update table
set user_id = user_id
Any suggestions and inputs would help.
Thanks
April 1, 2010 at 2:48 pm
Based on what I have read an I apologize becuase I don;t know that I have had direct expierence with this but it is my understanding that Sql server does not support row update triggeres so based on other forumns and other responces the popular answer seems to be that you would use cusor selected from inserted or deleted virtual table to see what records was updated
Dan
If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.
April 1, 2010 at 2:50 pm
HEre is a link to one of the threads on this subject.
http://www.sqlservercentral.com/Forums/Topic730493-338-1.aspx
Dan
If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.
April 1, 2010 at 2:53 pm
am-244616 - when you select into a variable from a table, the variable is going to hold the value from the last row selected. It might be the first row updated, the last, or something in between.
Dan - forget the cursor. Use the proper, set-based update statement instead:
UPDATE t
SET DATE_TIME_MOD = GETDATE()
-- make sure the first table in the from is the table being updated!
FROM "MyTable" t
JOIN inserted i
ON t.table_id = i.table_id
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 1, 2010 at 2:56 pm
Dan.Humphries (4/1/2010)
http://www.sqlservercentral.com/Forums/Topic730493-338-1.aspx%5B/quote%5D
This link is referring to the specific case of firing a trigger during a bulk insert and doesn't really apply to this question.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 1, 2010 at 3:10 pm
A trigger will still only fire once per statemnet though is that not correct?
Dan
If only I could snap my figures and have all the correct indexes apear and the buffer clean and.... Start day dream here.
April 1, 2010 at 3:13 pm
Dan.Humphries (4/1/2010)
A trigger will still only fire once per statemnet though is that not correct?
A trigger will fire only ONCE per DML statement. The problem is that the DML statement is updating > 1 row, but only one row is being updated by the trigger.
The inserted virtual table will contain the new contents of ALL the rows being updated by the one DML statement, and thus can be used to update the auditing field for ALL of the rows that were affected.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 2, 2010 at 4:16 am
am-244616 (4/1/2010)
declare @table_id bigintselect @table_id=table_id from INSERTED
This query only give one value for table_id irrespective of no of rows afftected in main table by inset/update
see this code to get this more clearer
create table tbl
( id int identity, name varchar(200) )
create table tr_tbl( output_id int )
create trigger tr on tbl AFter insert
as
begin
SET NOCOUNT ON
IF TRIGGER_NESTLEVEL() > 1
RETURN
declare @id bigint
select @id=id from INSERTED
insert into tr_tbl
select @id
end
GO
insert into tbl
select 'bhuvnesh'
union
select 'jack'
union
select 'max'
select * from tbl
select * from tr_tbl
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply