Okay.ExampleTestTable with InsertTrigger (sorry don't have northwind installed right now)if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TESTTable_INSERT]') and OBJECTPROPERTY(id, N'IsTrigger') = 1)drop trigger [dbo].[TESTTable_INSERT]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[TestTable]GOCREATE TABLE [dbo].[TestTable] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [TextField] [char] (10) COLLATE Latin1_General_CI_AS NULL , [NumericField] [int] NULL ) ON [PRIMARY]GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOCREATE TRIGGER TESTTable_INSERT ON dbo.TestTable FOR INSERTASdeclare @ID intdeclare Cur cursor local for SELECT ID FROM insertedopen Curfetch Cur into @IDwhile (@@fetch_status = 0)begin UPDATE dbo.TestTable SET NumericField = NumericField+1 WHERE [ID] = @ID fetch Cur into @IDendclose CurGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO
So every value inserted in the NumericField Field is incremented by one....Problem: This (not exactly this...) is necessary in about 40 tables... so i write a stored procedure which is creating the trigger..like this oneCREATE PROCEDURE dbo.CreateTrigger @Table varchar(255), @Field varchar(255) ASdeclare @createtrigger varchar(3000)SET @createtrigger='CREATE TRIGGER #TABLE#_INSERT ON dbo.#TABLE# FOR INSERTASdeclare @ID intdeclare Cur cursor local for SELECT ID FROM insertedopen Curfetch Cur into @IDwhile (@@fetch_status = 0)begin UPDATE dbo.#TABLE# SET #FIELD# = #FIELD#+1 WHERE [ID] = @ID fetch Cur into @IDendclose Cur'SET @createtrigger = REPLACE (@createtrigger,'#TABLE#', @Table)SET @createtrigger = REPLACE (@createtrigger,'#FIELD#', @Field)exec (@createtrigger)GO
works quite fine if the table has a pk called ID but if not I have to know the WHERE clause to build the trigger .....If there would be a rowID this would be unnecessary as there would be a possibility to link the inserted rows to the actual rows in the table...hope this clears things up.