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 2000 Forums
 SQL Server Development (2000)
 Triggers - Change data of inserted or updated cols

Author  Topic 

Pakl
Starting Member

10 Posts

Posted - 2005-06-09 : 05:43:11
Hi!

I have a question about triggers in sql server. Sporadically I want to change a value of an updated or inserted column in a trigger. As there is no rowid in sql server ¿ I have to know the where clause to update the field.

Unfortunately I have to simulate an identity field by using triggers in lots of tables and I dont want to code every one trigger by hand. Not that I am that lazy but it would be very difficult (and unnecessary dangerous) to administer. So I wonder if there is any way to do this programmatically. Beside this specific problem I would like to know if anyone has a best practise solution to do updates of inserted or updated columns in a trigger. (maybe dynamically providing the where clause)...

I guess lots of people do some kind of data manipulation in triggers, so this should be a standard procedure !?

Thank you very much for any comments or suggestions...

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-06-09 : 06:07:34
well trigger fires after or instead of the insert/delete/update and you can't pass variables to it.
new values will be in the inserted pseudo-table, deleted in deleted pseudo-table.
you can do your operations on the main table by joining it to those pseudo-tables.

other than that i don't get what you mean exactly.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Pakl
Starting Member

10 Posts

Posted - 2005-06-09 : 07:48:10
okay... i'll try again...

I have a normal 'after' insert trigger....
now I want to update a few fields of the inserted rows in the table.
as you say by joining over those pseudo tables.
but I have to know the where-clause (pks) of the table to do this.

I want to have a dynamic solution for multiple tables... (i just want to create the triggers by a stored procedure --- and the triggers will all be the same).

so maybe it is possible to create the where-clause dynamically in the sp which creates the trigger!?

I hope this explains it better otherwise I've to fight the flow
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-06-09 : 08:09:41
the PK's will be in psedo table inserted inside the trigger.
have you read about it in create trigger in Book onLine = sql server help?
so you want to create a same trigger on all of the tables that does a different
thing on each table based on the where part of the statement???

i don't think you can do that. or i'm not understanding you correctly.
can you give an example of exactly what you want?

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Pakl
Starting Member

10 Posts

Posted - 2005-06-09 : 08:31:45
Okay.

Example

TestTable 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]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[TestTable]
GO

CREATE TABLE [dbo].[TestTable] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[TextField] [char] (10) COLLATE Latin1_General_CI_AS NULL ,
[NumericField] [int] NULL
) ON [PRIMARY]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE TRIGGER TESTTable_INSERT ON dbo.TestTable
FOR INSERT
AS

declare @ID int
declare Cur cursor local for SELECT ID FROM inserted
open Cur
fetch Cur into @ID

while (@@fetch_status = 0)
begin

UPDATE dbo.TestTable SET NumericField = NumericField+1 WHERE [ID] = @ID
fetch Cur into @ID
end

close Cur

GO
SET QUOTED_IDENTIFIER OFF
GO
SET 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 one



CREATE PROCEDURE dbo.CreateTrigger @Table varchar(255), @Field varchar(255) AS

declare @createtrigger varchar(3000)
SET @createtrigger=
'CREATE TRIGGER #TABLE#_INSERT ON dbo.#TABLE#
FOR INSERT
AS

declare @ID int
declare Cur cursor local for SELECT ID FROM inserted
open Cur
fetch Cur into @ID

while (@@fetch_status = 0)
begin

UPDATE dbo.#TABLE# SET #FIELD# = #FIELD#+1 WHERE [ID] = @ID
fetch Cur into @ID
end

close 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.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-06-09 : 09:09:32
for updating that column use this:

declare @i int
set @i = 0
UPDATE t1
SET @i = Id = @i + 1
from MyTable t1 join inserted i on i.id = t1.id

you don't need a cursor.

if you don't know the id's then you can't generalize....

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Pakl
Starting Member

10 Posts

Posted - 2005-06-09 : 09:16:35
quote:
Originally posted by spirit1

for updating that column use this:

declare @i int
set @i = 0
UPDATE t1
SET @i = Id = @i + 1
from MyTable t1 join inserted i on i.id = t1.id

you don't need a cursor.


yes I see
quote:

if you don't know the id's then you can't generalize....

Go with the flow & have fun! Else fight the flow


No, thats impossible. There has to be a way to do this better than comparing per hand over the pks....

I am trying to do this with the following select
quote:

select distinct a.COLUMN_NAME, a.TABLE_NAME from information_schema.columns as a inner join information_schema.constraint_column_usage as b ON a.COLUMN_NAME = b.COLUMN_NAME
INNER JOIN information_schema.table_constraints as c ON b.CONSTRAINT_NAME = c.CONSTRAINT_NAME
WHERE c.CONSTRAINT_TYPE = 'PRIMARY KEY' and a.TABLE_NAME ='TestTable'



I could use this to generate the trigger .....
But don't bother, I just wanted to know if there is any best practise for updating values in insert or update triggers...

thanks for your effort
Go to Top of Page
   

- Advertisement -