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 |
|
flchico
Starting Member
46 Posts |
Posted - 2010-09-08 : 17:14:41
|
| Hi there,I need to create an Insert Trigger to do the following:Create an extra record with the same information except one field, which is not the Primary Key (Primary key is an identity key field). For simplicity if code = 600 then we need to add an extra record and the code in the new extra record will be 610, the rest of the information will be the same.Ex:pk name code1 Mark 5002 Tina 600-- This triggers to create an extra record with code 6103 Tina 6104 Ray 4005 Kay 600-- This triggers to create an extra record with code 6106 Kay 610How can I do this in the insert trigger?Thanks! |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
flchico
Starting Member
46 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
flchico
Starting Member
46 Posts |
Posted - 2010-09-08 : 18:34:12
|
| Wouldn't I get recursive inserts b/c we are inserting into the same table in the insert trigger? Well, that would only happen if we didn't change the code (leaving the code = 600) but since we change it to 610 for the newly inserted record when the trigger fires then it's gonna look for code = 600 so it stops. Maybe I didn't explain myself well. Thanks again! Simple solution :). |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-09-08 : 18:57:48
|
| My solution should do what you want. The inserted trigger table will contains all rows to be inserted, regardless of the code. The query I posted will duplicate the 600 code ones but use 610 instead. So you'll get everything you inserted plus 610 rows if any 600 rows were found.Give it a try and see if it solves your issues. If it doesn't, please post sample data and DDL for your table so that I can test.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
|
|
|
|
|