Solved

SQL raw queries versus stored proceedures

Posted on 2013-06-09
5
1,582 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
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 48

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 48

Expert Comment

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

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

947 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

19 Experts available now in Live!

Get 1:1 Help Now