Solved

SQL raw queries versus stored proceedures

Posted on 2013-06-09
5
1,872 Views
Last Modified: 2013-06-25
What are the advantages/disadvantages of using stored procedures and raw queries?  What types of functionality is available for each?
0
Comment
Question by:Jack_son_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Assisted Solution

by:didnthaveaname
didnthaveaname earned 125 total points
ID: 39233009
The queries are more flexible, as you have complete control over every aspect of the query. Store procedures only give you as much control as the inputs allow.  The benefits of stored procedures are code re-use, execution plan re-use, and security ( you don't have people/program's directly interacting with the data - you can define how, what and who can interact with the data)
1
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 125 total points
ID: 39233534
Hi,

With the plan reuse you eliminate the compile time. Often this is small compared to the execution time. However I heard a story that compile time was 3 minutes, vs 10 seconds execution time.

So, generally, benefit of stored procedure is more control. Benefit of query is more flexibility.

HTH
  David
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 125 total points
ID: 39233908
this is an often discussed question, as a sample:
http://avishkarm.blogspot.com.au/2010/01/pros-and-cons-of-stored-procedures.html

and you may find this of interest:

Tech·Ed North America 2009
The Pros and Cons of Stored Procedures
Speakers: Stephen Forte, Adam Machanic, Jeffrey Palermo, Maciej Pilecki, Tobias Ternstrom, Michael Wang

http://channel9.msdn.com/Events/TechEd/NorthAmerica/2009/PAN67
video ~55 minutes
0
 
LVL 4

Accepted Solution

by:
BAKADY earned 125 total points
ID: 39234318
you have to ask first:
What are the differences between stored procedures and raw queries?
after that, like @PortletPaul posted, you need to understand "The Pros and Cons of Stored Procedures"...

for example, a really bad use of stored procedures:

USE MyDatabase;
GO
DECLARE @my_result int;
DECLARE @SomeNumericValue int;
SET @my_result = 0;
DECLARE my_cursor CURSOR FOR SELECT SomeNumericValue FROM SomeTable;
OPEN my_cursor;
FETCH NEXT FROM my_cursor INTO @SomeNumericValue;
WHILE @@FETCH_STATUS = 0
BEGIN
   SET @my_result = @my_result + @SomeNumericValue;
   FETCH NEXT FROM my_cursor INTO @SomeNumericValue;
END
CLOSE my_cursor;
DEALLOCATE my_cursor;

SELECT @my_result as my_result;
GO

in this case is better a raw query like :

SELECT SUM(SomeNumericValue) as my_result FROM SomeTable;

if you need to perform more that 1 query - like ADO, it handles multiple recordsets - and/or no just select commands, than you can use stored procedures.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39234541
:) good bad example I must say... LOL
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question