Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arguments for/against Business Logic in stored procedures [closed]

What are the arguments for and against business logic in stored procedures?

like image 543
Chris Burgess Avatar asked Jan 27 '09 18:01

Chris Burgess


People also ask

Why shouldn't we put all business logic in stored procedure?

Cons of holding all the business logic on stored procedures in web application: against: Good SQL knowledge can be hard to find in many locations. Good SQL coders can be expensive. All application developers will need to be able to either change it or request a quick change.

Should you have business logic in stored procedures?

The current draft of CommonChannelArchitecture states the following: "The default stance in designing an application should be that business logic is held in the application code, NOT in database stored procedures. Only move business logic into StoredProcedures as performance needs required. "

Why you should not use stored procedures?

Stored procedures promote bad development practices, in particular they require you to violate DRY (Don't Repeat Yourself), since you have to type out the list of fields in your database table half a dozen times or more at least. This is a massive pain if you need to add a single column to your database table.


3 Answers

Against stored procedures: business logic in programming space

I place a high value on the power of expression, and I don't find the SQL space to be all that expressive. Use the best tools you have on hand for the most appropriate tasks. Fiddling with logic and higher order concepts is best done at the highest level. Consequently, storage and mass data manipulation is best done at the server level, probably in stored procedures.

But it depends. If you have multiple applications interacting with one storage mechanism and you want to make sure it maintains its integrity and workflow, then you should offload all of the logic into the database server. Or, be prepared to manage concurrent development in multiple applications.

like image 182
Mark Canlas Avatar answered Sep 22 '22 19:09

Mark Canlas


I am thoroughly against it. One of the biggest reasons is the first reason earino stated - it lives in one place. You can not integrate it into source control very easily. It is next to impossible to have two devs working on a stored proc at the same time.

My other main complaint is that SQL is just not very good at representing complex logic. You have no concept of scope, code tends to be copy-pasted because there is a less ability to reuse code (as opposed to an OO language).

You have to give developers access to the database to develop there. In many organizations I have worked at the data people are in a different world than the devs, with different permissions, etc. Keeping the devs out of the database in these cases would be harder.

like image 24
Nick Van Brunt Avatar answered Sep 19 '22 19:09

Nick Van Brunt


I'm of the school of thought that says that as long as business logic:

  • lives in one place
  • where it is properly documented
  • proper access is provided through services that can be loosely coupled
  • through a published abstracted interface

I don't care if the logic lives in a stored procedure, in a J2EE middle tier, in a clips expert system, or wherever. No matter where you store our business logic the "law of conservation of misery" is going to guarantee that someone will say it was the wrong idea because component/repository X needs to be swapped out for technology/method Y.

like image 23
earino Avatar answered Sep 22 '22 19:09

earino