Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does stored procedure help eliminates SQL injection / What are the benefits of stored procedured over normal SQL statement in apps?

I'm pretty new to SQL world. Here are my questions:

  • What are the benefits of stored procedured over normal SQL statement in applications?
  • Does stored procedure help eliminates SQL injection?
  • In Microsoft SQL Server it is called stored procedure. How about in Oracle, MySQL, DB2, etc.?

Thanks for your explanation.

like image 630
natch3z Avatar asked Jul 23 '09 19:07

natch3z


People also ask

Does stored procedures prevent SQL injection?

Stored procedures only directly prevent SQL injection if you call them in a paramerized way. If you still have a string in your app with the procedure name and concatenate parameters from user input to that string in your code you'll have still have trouble.

What are the advantages of stored procedure and stored procedure?

Executable code is automatically cached and shared among users. This lowers memory requirements and invocation overhead. By grouping SQL statements, a stored procedure allows them to be executed with a single call. This minimizes the use of slow networks, reduces network traffic, and improves round-trip response time.

What is the purpose of stored procedure?

A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it.

What is a stored procedure and what are its advantages and disadvantages?

A Stored Procedure can be used as a modular programming which means create once, store and call for several times whenever it is required. This supports faster execution. It also reduces network traffic and provides better security to the data.


1 Answers

Stored procedures only directly prevent SQL injection if you call them in a paramerized way. If you still have a string in your app with the procedure name and concatenate parameters from user input to that string in your code you'll have still have trouble.

However, when used exclusively, stored procedures let you add some additional protection by making it possible for you to disable permissions to everything but the EXEC command. Aside from this, parameterized queries/prepared statements are normally cached by the server, and so are just like a stored procedure in nearly every respect.

In spite of this, stored procedures have two big advantages for larger enterprises:

  • They allow you to define an application interface for the database, so that the system can be shared between multiple applications without having to duplicate logic in those applications.
  • They move the sql code to the db, where you can easily have an experienced DBA tune, update, and otherwise maintain it, rather than application developers who often don't know exactly what they're doing with database code.

Of course, these advantages aren't without cost:

  • It's harder to track changes in source control
  • The database code is far separated from the code that uses it
  • Developer tools for managing many stored procedures are less than ideal (if you've ever open the stored procedures folder in management studio to find 200 procedures for a database, you know what I'm talking about here).
like image 160
Joel Coehoorn Avatar answered Oct 05 '22 23:10

Joel Coehoorn