Solved

SQL raw queries versus stored proceedures

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

839 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