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
 Transact-SQL (2000)
 Insert Trigger...

Author  Topic 

dhw
Constraint Violating Yak Guru

332 Posts

Posted - 2005-02-22 : 15:04:44
hello....

i have a need for an insert trigger...to explicitly update one of table columns regardless if a value has already been passed through during the actual insert operation.

the problem i am running into is that there is an primary key ID column, but it is not an Identity data type(i have done this before using the @@IDENTITY function to retrieve the current value)....so I need to figure out how, within my trigger, I can capture the pk id value for the row that is being INSERTed and then do my update based on that pk id.

thanks
-dw

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-02-22 : 17:21:29
You need to use the INSERTED table. Read BOL if you don't know what that is.

The @@IDENTITY doesn't work anyway, even if you had an identity column, because you could have inserted more than one row. You have to write a trigger to allow for all cases.

This code should do it:

update myTable
set
Col_1 = YourNewValue
from
myTable
join
inserted
on myTable.PK = inserted.PK


quote:
Originally posted by dhw

hello....

i have a need for an insert trigger...to explicitly update one of table columns regardless if a value has already been passed through during the actual insert operation.

the problem i am running into is that there is an primary key ID column, but it is not an Identity data type(i have done this before using the @@IDENTITY function to retrieve the current value)....so I need to figure out how, within my trigger, I can capture the pk id value for the row that is being INSERTed and then do my update based on that pk id.

thanks
-dw



Codo Ergo Sum
Go to Top of Page

dhw
Constraint Violating Yak Guru

332 Posts

Posted - 2005-02-22 : 21:27:25
Thanks! That worked great. I completely forgot about the INSERTED table.

- dw
Go to Top of Page
   

- Advertisement -