Author |
Topic |
JeffS23
Posting Yak Master
212 Posts |
Posted - 2008-01-08 : 12:18:35
|
I have a table called OLTSUPDATES. I want a trigger that would update two tables (DMAIN & CONTACTS). This is a ONE(dmain) to MANY(contacts) relation based on the DONOR_ID field.Currently a trigger in DBO.OLTSUPDATES updates my DMAIN table with no problem. ----------------USE YourDatabaseGoesHereGoCREATE trigger trigger_DmainUpdate On dbo.OltsUpdates FOR INSERT AS BEGIN Insert DMAIN (CaseNo, DFName, DLName,DDate,DoB,Hosp_id,Sex,Race,DCause,ReferralType)Select CaseNo, DFName, DLName, DDate, DoB, Hosp_id, Sex, Race, DCause, ReferralTypeFrom InsertedEND----------------However, in DMAIN there is an autonumber (DONOR_ID) that is created upon new insert and because of the one-to-many relation with CONTACTS table, now I want a trigger to insert a new record in DBO.CONTACTS, that will update data from DBO.OLTSUPDATES but also include the newly created autonumber.I was thinking. Should I create another trigger to update my OLTSUPDATE table with the new autonumber, then have the trigger below update the CONTACTS table. Is there a simpler way for this?CREATE trigger trigger_OLTSInsertToContact On bt1.dbo.OltsUpdatesFOR INSERTASBEGINInsert CONTACTS (CaseNo, ConFName, ConLName, ConStreet, ConStreet2, ConCity, ConState, ConZip, ConHomePH, Relationship)Select CaseNo, ConFName, ConLName, ConStreet, ConStreet2, ConCity, ConState, ConZip, ConHomePH, Relationship From InsertedEND |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-08 : 12:28:02
|
[code]Cant you do both inserts inside same trigger?USE YourDatabaseGoesHereGoCREATE trigger trigger_DmainUpdate On dbo.OltsUpdatesFOR INSERTASBEGINDECLARE @DONOR_ID intInsert DMAIN (CaseNo, DFName, DLName,DDate,DoB,Hosp_id,Sex,Race,DCause,ReferralType)Select CaseNo, DFName, DLName,DDate,DoB,Hosp_id,Sex,Race,DCause,ReferralTypeFrom InsertedSET @DONOR_ID=SCOPE_IDENTITY()Insert CONTACTS (fields)Select @DONOR_ID,other fieldsFrom InsertedEND[/code] |
 |
|
|
|
|