Solved

SQL raw queries versus stored proceedures

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

Suggested Solutions

Title # Comments Views Activity
SQL Select Query help 1 38
Linked Server - SP with Param to VIew 7 24
Need to trim my database size 9 25
How to trim a value in SQL 2 27
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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
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 shrink a transaction log file down to a reasonable size.

733 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