Solved

SQL raw queries versus stored proceedures

Posted on 2013-06-09
5
1,538 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_
5 Comments
 
LVL 8

Assisted Solution

by:didnthaveaname
didnthaveaname earned 125 total points
Comment Utility
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
Comment Utility
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 48

Assisted Solution

by:PortletPaul
PortletPaul earned 125 total points
Comment Utility
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
Comment Utility
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 48

Expert Comment

by:PortletPaul
Comment Utility
:) good bad example I must say... LOL
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now