September 11, 2009 at 6:28 pm
I have a main table Broker which has an identity field as primary key, once inserted, is supposed to insert relevant data on several other tables by accessing the @@Identity. I have created store procedures for all the other tables.
Initially I was planning on just creating triggers to insert data in the child tables, now I am not sure if I should use a trigger to execute a store procedure, or a Main Parent SP to execute all the child sp's
If I use One Main sp to execute all the child sp's, do I need to call ALL the parameters of the child sp's in the main sp?
If I use a trigger to execute a store procedure? How do I configure the parameters.
Any help will be much appreciated.
Thanks!
September 11, 2009 at 11:41 pm
Well, it really depends on how complicated your data is and what kind of work your triggers do.
I, personally, have been avoiding triggers in the past 12 years. I like to keep my code and logic plain simple and "procedural"... It might look somewhat crippled, but as a tech-leader I have the luxury of choosing between readability and sophistication level of a solution.
As for your question: If you DO have triggers already - go ahead and use them, as otherwise you'll bump into more problems than you might think of.
If you do not have any triggers yet, I recommend you split your code into several SPs and call them from one main SP. It is best solution because:
1. Code is broken into logoically different pieces in an elegant way
2. You might wanna change one SP without changing tons of lines of code.
3. All the reasons for using procedural coding apply here... event the name 'procedural' is a good hint
If I use One Main sp to execute all the child sp's, do I need to call ALL the parameters of the child sp's in the main sp?
Just like any other SP, you may or may not pass all parameters. You may leave NULLs or DEFAULT keyword, if they apply
If I use a trigger to execute a store procedure? How do I configure the parameters
In order to do that you should learn, first, about what INSERTED and DELETED logical tables are and take your parameters from there, but then again - It's a trigger based solution, even if a trigger fires SP...
September 12, 2009 at 4:50 pm
I don't have a very good answer for you but I can tell you that using @@IDENTITY, especially if triggers come into play, is a form of "Death by SQL". Use SCOPE_IDENTITY() instead. See Books Online for why.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 12, 2009 at 4:51 pm
You might also want to lookup what OUTPUT does for you, as well.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 12, 2009 at 10:23 pm
Jeff Moden (9/12/2009)
You might also want to lookup what OUTPUT does for you, as well.
Using the OUTPUT clause of the data modification statement is much to be preferred in many cases.
Both SCOPE_IDENTITY() and @@IDENTITY are unreliable if a parallel plan was used in the statement that affected the IDENTITY column. I am unsure about whether IDENT_CURRENT is likewise unreliable in these circumstances.
All four options (OUTPUT clause, @@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT) have their applications, and all are well documented.
When using anything except the OUTPUT clause, I am careful to add an explicit MAXDOP(1).
See this confirmed bug for details. Both 2005 and 2008 are affected, and there are no plans to fix it before at least R2.
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply