| Author |
Topic |
|
dupati1
Posting Yak Master
123 Posts |
Posted - 2005-10-07 : 17:14:46
|
I have the following temp tableVendorTemp______________VendorID|VendorName|VendorAddress|VendorPhone|VendorSpecialty|MoveItThe below are the live tablesVENDOR_______VendorID|VendorNameVendorContactInfo________________VendorID|VendorAddress|VendorPhoneVendorBusinessInfo___________________VendorID|VendorSpecialtyOK now i am trying to write this trigger...CREATE TRIGGER mytriggerON VendorTempFOR INSERT,UPDATEASIF UPDATE(MoveIt)=1BeginINSERT INTO Vendor(VendorID,VendorName)Select Ins.VendorID, Ins.VendorNameFROM Inserted InsINSERT INTO VendorContactInfo(VendorID,VendorAddress,VendorPhone)Select Ins.VendorID,Ins.VendorAddress,Ins.VendorPhoneFROM Inserted InsINSERT INTO VendorBusinessInfo(VendorID, VendorSpecialty)SELECT Ins.VendorID,Ins.VendorSpecialtyEndGo this is my first attempt at writing triggers.Sowasnt sure if it was correct. How can i adderror checking and roll back thing to it(if needed)and sometimes my VendorSpecialty field holds multiplevalues separated by vbCrLf...something like'Mechanical Chemical Electrical'how do i change my insert statement inside the triggerto get something likeVendorID | VendorSpecialty____________________________101 | Mechanical101 | Chemical101 | ElectricalThanks in advance. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-10-10 : 04:26:57
|
| maddy : I think dupati1 was using the "|" character to indicate column names, not that the data is in delimited format.Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-10 : 04:41:09
|
quote: and sometimes my VendorSpecialty field holds multiplevalues separated by vbCrLf...something like'Mechanical Chemical Electrical'how do i change my insert statement inside the triggerto get something likeVendorID | VendorSpecialty____________________________101 | Mechanical101 | Chemical101 | Electrical
So I thought he/she needs spliting it MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-10-10 : 06:32:52
|
| Ah, for that bit - yup, sorry about that.Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-10 : 06:44:08
|
quote: Originally posted by Kristen Ah, for that bit - yup, sorry about that.Kristen
No problem Kris MadhivananFailing to plan is Planning to fail |
 |
|
|
dupati1
Posting Yak Master
123 Posts |
Posted - 2005-10-10 : 08:35:20
|
| Thanks for the replies guys...but one question though...does my trigger look ok to you guys...because this was my first attempt to create one and i wrote it after reading BOL...how can i put some error checking and roll back if needed...Thanks so much |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-10-10 : 08:50:06
|
| hi you can try this .. CREATE TRIGGER mytriggerON VendorTempFOR INSERT,UPDATEASIF UPDATE(MoveIt)=1BeginBegin Tran Declare @ErrVendor int,@errVendorContinfo int,@errVendorBussinessInfoINSERT INTO Vendor(VendorID,VendorName)Select Ins.VendorID, Ins.VendorNameFROM Inserted InsSelect @ErrVendor= @@Error INSERT INTO VendorContactInfo(VendorID,VendorAddress,VendorPhone)Select Ins.VendorID,Ins.VendorAddress,Ins.VendorPhoneFROM Inserted InsSelect @errVendorContinfo= @@Error INSERT INTO VendorBusinessInfo(VendorID, VendorSpecialty)SELECT Ins.VendorID,Ins.VendorSpecialtySelect @errVendorBussinessInfo= @@Error if @@Error <> 0Begin if @errVendorBussinessInfo <> 0 print 'Error in Inserting Vendor Business info ' if @errVendorContinfo<> 0 print 'Error in Inserting Vendor Contact info ' if @ErrVendor<> 0 print 'Error in Inserting Vendor ' Rollback Tran End Else Commit Tran EndGofor the error checking .. i hope this helps you .. Complicated things can be done by simple thinking |
 |
|
|
dupati1
Posting Yak Master
123 Posts |
Posted - 2005-10-10 : 09:20:51
|
| Thank you chiragkhabaria,I will try it out... |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-10-10 : 09:33:56
|
if @@Error <> 0Begin ...is only going to react to the outcome of the third INSERT statement isn't it?The ROLLBACK is needed if any of the three error capture variables are non-zero.This:INSERT INTO VendorBusinessInfo(VendorID, VendorSpecialty)SELECT Ins.VendorID,Ins.VendorSpecialtyneedsFROM Inserted Insadding doesn't it?What is intended to happen on an INSERT UPDATE - as the values will already exist in the target tables won't they?Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2005-10-10 : 09:39:47
|
| opps .. My mistake........:-((... if @errVendorBussinessInfo <> 0 or @errVendorContinfo<> 0 or @ErrVendor<> 0 Begin if @errVendorBussinessInfo <> 0 print 'Error in Inserting Vendor Business info 'if @errVendorContinfo<> 0print 'Error in Inserting Vendor Contact info 'if @ErrVendor<> 0print 'Error in Inserting Vendor 'Rollback Tran End ElseCommit Tran EndI guess this should be right.. Thanks.. Kris.. :-)Complicated things can be done by simple thinking |
 |
|
|
dupati1
Posting Yak Master
123 Posts |
Posted - 2005-10-10 : 11:30:11
|
| Thank you all...you guys have been so helpful... |
 |
|
|
dupati1
Posting Yak Master
123 Posts |
Posted - 2005-10-10 : 11:45:31
|
| Guys, i have one question...Cant i just declare one error variable instead of three...Thanks |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-10-10 : 12:19:40
|
| Hmmmm ... well yes, but the moment you have examined @@ERROR its value is gone. So you need to do something useful with it straight away.DECLARE @strErrMsg varchar(1000)...INSERT INTO Vendor(VendorID,VendorName)Select Ins.VendorID, Ins.VendorNameFROM Inserted InsIF @@Error <> 0 SELECT @strErrMsg = COALESCE(@strErrMsg+', ', '') + 'Error in Vendor'INSERT INTO VendorContactInfo(VendorID,VendorAddress,VendorPhone)Select Ins.VendorID,Ins.VendorAddress,Ins.VendorPhoneFROM Inserted InsIF @@Error <> 0 SELECT @strErrMsg = COALESCE(@strErrMsg+', ', '') + 'Error in VendorContactInfo'...if @strErrMsg IS NOT NULLBegin print @strErrMsg -- Probably better to use RAISERROR hereRollback Tran End ElseCommit Tran EndKristen |
 |
|
|
dupati1
Posting Yak Master
123 Posts |
Posted - 2005-10-10 : 13:17:24
|
| Thanks Kristen,Thats Awesome. |
 |
|
|
dupati1
Posting Yak Master
123 Posts |
Posted - 2005-10-10 : 13:42:49
|
| Kristen,I am getting errors1. on line IF UPDATE(MoveIt)=1, it says incorrect syntax near '=' sign2. at the end, it says incorrect syntax near keyword END |
 |
|
|
dupati1
Posting Yak Master
123 Posts |
Posted - 2005-10-10 : 14:05:11
|
| ok fixed both the problems...thanks |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-10-10 : 14:43:22
|
"thanks"Who me? OK, I'll take a bow anyway! Kristen |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-10-12 : 02:45:24
|
>>Who me? OK, I'll take a bow anyway! No wonder. It is always you in this topic MadhivananFailing to plan is Planning to fail |
 |
|
|
|