Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Main Store procedure to execute several store procedures or Trigger? Expand / Collapse
Author
Message
Posted Friday, September 11, 2009 6:28 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 8, 2014 12:50 PM
Points: 5, Visits: 125
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!
Post #786775
Posted Friday, September 11, 2009 11:41 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, February 9, 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)

Post #786827
Posted Saturday, September 12, 2009 4:50 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:11 PM
Points: 35,546, Visits: 32,141
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #786924
Posted Saturday, September 12, 2009 4:51 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:11 PM
Points: 35,546, Visits: 32,141
You might also want to lookup what OUTPUT does for you, as well.

--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #786925
Posted Saturday, September 12, 2009 10:23 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, November 20, 2014 7:53 PM
Points: 9,928, Visits: 11,194
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
Post #786946
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse