Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why (and when to) use stored procedures? [duplicate]

Possible Duplicate:
What are the pros and cons to keeping SQL in Stored Procs versus Code

What would be appropriate scenario when stored procedures should be used?

I stumbled upon implementation where almost whole data manipulation was handled by store procedures, even simplest form of INSERT/DELETE statements were wrapped and used via SP's.

So, what's the rationale for using stored procedures in general?

Sorry for such a beginners question..

like image 263
mr.b Avatar asked Dec 28 '22 13:12

mr.b


2 Answers

Outwith the reasons @Tom has already pointed out which are probably the most important (Speed/Security) I would also say that another good reason to use Stored Procedures is code re-use. If you find yourself writing the same SQL all over the place its usually a sign that it should be a stored procedure. Also, another good reason is it allows not only developers, but DBA's the ability to change/add new procedures if required.

like image 151
James Avatar answered Jan 14 '23 14:01

James


Two reasons I know of:

  1. Security

The stored procedures are secure from attacks such as SQL injection attacks

  1. Speed

Stored procedures are sometimes precompiled which makes execution faster.

like image 29
Tom Gullen Avatar answered Jan 14 '23 13:01

Tom Gullen