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 |
SQLIsTheDevil
Posting Yak Master
177 Posts |
Posted - 2008-11-06 : 17:01:06
|
I'd like to update a column after an insert using a trigger. It'll look like this:Create Trigger TR_SomeNameON SomeTableAfter INSERTASIF Exists (Select * From Inserted)BEGIN--Update SomeColumn from SomeTableENDGOMy question is since this is an AFTER INSERT, can I still update the column from the Inserted table? That is the whole point of this exercise. If I must use ON INSERT to modify the contents of the insert table, how would I go about updating a column? Thank you very much. |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-11-06 : 18:16:46
|
SQL Server knows: Create Trigger TR_SomeNameON SomeTableAfter INSERT / INSTEAD OF INSERT(AFTER is the default, if FOR is the only keyword specified: FOR INSERT is same to AFTER INSERT and FOR AFTER INSERT)and ON INSERT is not knownAFTER INSERT: use information from INSERTED to make, for example, your additional updates on SomeTableINSTEAD OF INSERT: Specifies that the trigger is executed instead of the triggering SQL statement, thus overriding the actions of the triggering statements. But INSERTED (and DELETED when updating or deleting) are filled with information like it is using AFTER INSERT trigger.Webfred No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-11-06 : 18:35:27
|
[code]Update MyTableset MyColumn = <some value>where MyTable.PKColumn in ( select inserted.PKColumn from inserted )[/code]CODO ERGO SUM |
 |
|
SQLIsTheDevil
Posting Yak Master
177 Posts |
Posted - 2008-11-10 : 14:34:10
|
Thank you very much for the help, for I got it to work. |
 |
|
|
|
|