|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, November 01, 2012 10:02 AM
Points: 5,
Visits: 107
|
|
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!
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, February 09, 2010 2:41 AM
Points: 140,
Visits: 1,019
|
|
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...
Tal Ben Yosef www.TalBenYosef.com (visit my LinkedIn profile)
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,893,
Visits: 26,771
|
|
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 "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 9:57 PM
Points: 32,893,
Visits: 26,771
|
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 10:33 AM
Points: 10,989,
Visits: 10,529
|
|
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 SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|