Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Friday, July 03, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Using Stored Procedures in SQL
12/28/2004 8:13:57 AM

Most of us, the database programmers, have used Stored Procedures. May be not all of us knows about why we use them. This article is for those who have used/never used stored procedures, and are yet to understand why everyone suggests using them in your Database. 

Stored Procedures – What are they? 

Stored procedure is a set of pre-defined Transact-SQL statements, used to perform a specific task. There can be multiple statements in a stored procedure, and all the multiple statements are clubbed in to one database object.  

How to create a stored procedure? 

Creating a stored procedure is as easy as running the “Create Procedure” statement followed by the SQL script. You can run your Create Procedure statement from the SQL Query Analyzer, or can use the New Procedure menu item in the Enterprise Manager. 

The simplest skeleton of a stored procedure. 

CREATE PROC procedure_name
    [ { @parameter data_type }
    ]
AS sql_statement  

Check the basic building blocks of a stored procedure. 

A stored procedure includes

1.      A CREATE PROC (CREATE PROCEDURE) statement;

2.      The procedure name;

3.      The parameter list

4.      And the SQL statements.

Even though there are numerous other options available while we define a stored procedure, I kept it simple, just to give you a basic idea about creating stored procedures.

 Advantages! 

Almost every database Guru that you will meet, will suggest using stored procedures. For you, it will seem as if most of them blindly believes in stored procedures. But there are reasons for this. This is what I am trying to explore in this article. 

1. Performance 

All the SQL statements, that you send to your database server passes through a series of actions, called execution. These are the steps that your SQL statement passes through before the data is returned to the client. 

    User sends request to execute the Stored Procedure. SQL Server checks for syntax errors. Identifies and checks the aliases in the FROM clause. Creates a query plan. Compiles the query and. Executes the query plan and return the requested data.

 See, lots of things are happening inside that we didn’t knew about. Now, the crucial question. Does a stored procedure bypass all these?

 In a way, yes. The previous versions of SQL Server stored the compiled execution plan in system tables, making them partially pre-compiled. This improved performance, because the Server did not have to compile the stored procedure each and every time it is called. 

In later versions of SQL Server, there were a large number of changes in statement processing. Now, the stored procedure is stored in a procedure cache when it is called, making subsequent calls faster. 

2. Security 

Stored procedures provide significant benefits when it comes to security. By using a stored procedure, you can grant permissions to certain users to access data, reducing the immense coding that you need to do in your client applications. This is one of the best ways to control access to your data. 

3. Modifications/Maintenance 

If you use stored procedures for database access, any change in the database can be reflected on to the client application without much effort. This is because you know exactly where the data is accessed from, and you also know exactly where you need to alter. This means no scuba diving in to thousands of lines of source code to identify areas where you need to alter and no headache of re-deploying the client application. 

4. Minimal processing at the client. 

When creating a client/server application, normally it was the client who took care of the integrity of data that went in to the database. Managing Primary Keys, Foreign keys, cascaded deletion everything was done by the client, and the database server just had to store data given by the client. 

Well friends, things have changed. Stored procedures help you write batch of SQL statements, which helps you manage the transactions, constraints etc. A little data aware code has to be written in to the client application, making it a thin-client application. These applications will be concerned more about displaying data in the way the user needs them and they know little about the database.

Take another scenario. You have a database with millions of rows and hundreds of tables. You need to do some calculations before updating each and every record. If you are fetching the complete data to the client, and is asking the client machine to process the data completely, then think about the overhead it creates. But when the client can execute a store procedure, where you have done the calculations prior to updating the records, you have a client, that doesn’t need to know about the calculations. This also reduces the amount of computing happening in the client, and the server takes care of tedious calculations. 

5. Network traffic 

Client applications always have to request/send data from the database server. These data are sent as packets, and travel through the network to the server.  

To explain how stored procedures can help reduce network traffic, let us see another scenario, where a request for data is send from the client. The request is sent as an SQL statement, and here it is. 

SELECT dbo.Tbl_Tablename.fieldID,
    dbo.Tbl_Tablename.fieldName,
    dbo.Tbl_Tablename.Title,
    dbo.TBl_otherTableName.fieldID,
    dbo.Tbl_Tablename.Published,
    dbo.Tbl_Tablename.Updated,
    dbo.Tbl_Tablename.SomeText,
    dbo.Tbl_Tablename.TransactionDate,
    dbo.Tbl_Tablename.Approved,
    dbo.Tbl_Tablename.ApprovedBy,
    dbo.Tbl_Tablename.ApprovalID
FROM
dbo.Tbl_Tablename
LEFT OUTER JOIN
            dbo.TBl_otherTableName on dbo.Tbl_Tablename.fieldID=dbo.TBl_otherTableName.ID
Where
            DateDiff ( wk, dbo.Tbl_Tablename.TransactionDate, getdate()) <= 1
            and dbo.Tbl_Tablename.Approved = 0 

518 Characters travel through the network, and when there are 20 client applications using this stored procedure 20 times a day, the number of characters passing through the network for just this request will be 2,07,200!  

You see the difference now. If it was a stored procedure, lets call it SP_fetchSomething, there are only 6800 characters in the network for the request. A saving of 2,004,00!

 As you have seen the five major points that I use to explain why I used a stored procedure, I hope you will also elect to intelligently use this awesome technology in your next database design.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 7/1/2009 1:13:10 AM by Anonymous
Comments: great
Left on 5/20/2009 1:45:04 PM by Anonymous
Comments: Shows "create" but not the call.
Left on 5/2/2009 3:32:29 PM by Anonymous
Comments: Very good for beginners.
Left on 4/29/2009 11:59:14 PM by Anonymous
Comments: very nice but give some realtime examples

Left on 4/29/2009 11:56:53 PM by Anonymous
Comments: very nice
Left on 2/19/2009 5:04:53 AM by Anonymous
Comments: little bit helpful but not upto the mark
Left on 2/16/2009 7:00:27 PM by Anonymous
Comments: examples would make it a lot clearer
Left on 1/23/2009 5:05:47 AM by Anonymous
Comments: 678686
Left on 1/12/2009 3:37:00 AM by Anonymous
Comments: very easy to understand
No ratings available.
Left on 1/7/2009 8:03:09 PM by Anonymous
Comments: Like this article. very convincing
Left on 12/23/2008 2:44:41 AM by Anonymous
Comments: good article
Left on 12/8/2008 1:38:42 AM by Anonymous
Comments: its good need some code for explain it..
Left on 11/13/2008 12:26:59 PM by Anonymous
Comments: pls provide some examples
No ratings available.
Left on 11/3/2008 5:43:05 AM by Anonymous
Comments: it will useful to every one.
Left on 9/20/2008 12:30:35 AM by Anonymous
Comments: good very helpful.more info in required
Left on 8/1/2008 6:05:38 AM by Anonymous
Comments: Excellent overview..
Left on 8/1/2008 3:53:00 AM by Anonymous
Comments: very easy 2 understand good
Left on 7/11/2008 7:42:22 PM by Anonymous
Comments: Hi, Can you tell me whats the difference b/w procedure and stored procedure with examples then it will be eas to understand

No ratings available.
Left on 7/11/2008 7:34:05 PM by Anonymous
Comments: Thanks and excellent and If you try to explain with one more example then it will be easy for all to understand.

Left on 7/4/2008 1:11:07 PM by Anonymous
Comments: some more examples would have been an advantage.nyways thanks.
Left on 6/25/2008 8:56:20 AM by Anonymous
Comments: thanks
Left on 6/24/2008 6:46:38 AM by Anonymous
Comments: ok
Left on 6/16/2008 4:52:19 AM by Anonymous
Comments: jhljljlkl
Left on 5/7/2008 11:43:12 PM by Anonymous
Comments: Excellent for those who start their carrier in DB
No ratings available.
Left on 4/25/2008 2:56:07 AM by Anonymous
Comments: very good but no  maintain good example
No ratings available.
Left on 12/28/2007 10:38:39 PM by Anonymous
Comments: plz give some examples using procedure
Left on 12/28/2007 10:37:12 PM by Anonymous
Comments: thanks, easy to understood
Left on 12/12/2007 10:08:14 AM by Anonymous
Comments: Good explanation of stored procs but what about the downsides, like lacking the flexibility to quickly amend the code if the results are not as expected
Left on 11/2/2007 7:22:08 AM by Anonymous
Comments: theory was good but need more of examples!
Left on 10/26/2007 6:11:27 AM by Anonymous
Comments: =dbo.TBl_otherTableName
Left on 10/9/2007 6:08:03 AM by Anonymous
Comments: very very useful
Left on 9/13/2007 1:15:49 AM by Anonymous
Comments: its good and helpful for freshers
Left on 9/10/2007 4:44:19 AM by Anonymous
Comments: Good for me
Left on 8/10/2007 7:58:16 AM by Anonymous
Comments: really usefull for me.
Left on 7/17/2007 2:29:33 PM by Anonymous
Comments: Good explanation
Left on 6/15/2007 9:28:34 AM by Anonymous
Comments: Great Page
Left on 9/4/2006 4:25:58 PM by Anonymous
Comments: Great for beginners like me
Left on 9/4/2006 3:48:26 AM by Anonymous
Comments: simple and good for the firs idea. Thanks!
Left on 9/2/2006 1:43:40 AM by Anonymous
Comments:
Left on 7/30/2006 11:40:06 PM by Anonymous
Comments: simple and good
Left on 7/28/2006 2:09:27 AM by Anonymous
Comments: good explanation for starters
Left on 7/17/2006 3:15:57 AM by Anonymous
Comments: good basic information given that's fantastic thanks
Left on 6/9/2006 3:21:44 AM by Anonymous
Comments: Good explain but when the client execute a stored procedure, the value return ll be stored in cache for further execuiting. So if there many many stored procedures are called, the cache which s stored data can be overfloat??? or we can say dump memory? It s true? If so, s there anyway to clear the cache after some given time?? pls help me to answer this quest. Thanz a lot
No ratings available.
Left on 5/18/2006 2:57:48 AM by Anonymous
Comments: Very nice and good explanation -biju: amazinglicoriceKHILADI420@gmail.com
No ratings available.
Left on 5/17/2006 1:12:00 PM by Anonymous
Comments: good explanation of why..
Left on 5/17/2006 6:43:29 AM by Anonymous
Comments: simple and precise..
No ratings available.
Left on 5/3/2006 1:17:58 AM by Anonymous
Comments: good explanation, really worthful.
Left on 3/16/2006 10:48:09 AM by Anonymous
Comments: good
Left on 3/13/2006 4:28:44 PM by Anonymous
Comments: Good to start ;-)
No ratings available.
Left on 3/11/2006 2:31:52 AM by Anonymous
Comments: this is good.
No ratings available.
Left on 2/21/2006 3:56:57 AM by Anonymous
Comments: good explanation mate
No ratings available.
Left on 2/20/2006 12:51:34 AM by Anonymous
Comments: Its very helpfull
Left on 2/8/2006 4:51:49 PM by Anonymous
Comments: it is too less
Left on 2/2/2006 2:40:25 AM by Anonymous
Comments: nice explaination but it would be better if more examples have been given....
No ratings available.
Left on 1/26/2006 12:07:21 PM by Anonymous
Comments: Title is  Using Stored Procedures in SQL

but the article didn't tell me HOW to RUN Stored Procedures in SQL !!
Left on 1/25/2006 4:04:45 AM by Anonymous
Comments: bad...
Left on 1/17/2006 8:37:20 AM by Anonymous
Comments: good

Left on 3/30/2005 1:13:54 AM by Anonymous
Comments: gr8 article.... really helping to understand Stored Procedures
No ratings available.
Left on 3/26/2005 7:07:58 AM by Anonymous
Comments: simple and effective
Left on 3/13/2005 2:12:12 AM by Anonymous
Comments: It is very clear to me. I am new to SQL server programming.
Left on 3/9/2005 4:17:38 PM by Anonymous
Comments: Eeek, Ike, OOO, DODO
That means its BAD
No ratings available.
Left on 2/15/2005 1:14:33 PM by Anonymous
Comments: One comment, don't use the prefix "sp" as it has overhead with the systems' always being checked first. We use "usp" for our procs.
Left on 2/11/2005 1:07:04 AM by Anonymous
Comments: This article is good to know why we should use store procedure and more thing it will be too good if you provide more tutorial about store procedure
No ratings available.
Left on 2/8/2005 12:11:15 AM by Anonymous
Comments: very good to understand the difference between normal query and store procedure
No ratings available.
Left on 2/4/2005 10:19:14 PM by Anonymous
Comments: Great points. But your paying a price for using the prefix _sp - reserved for system procedures. The master database will get checked for this procedure first.
No ratings available.
Left on 2/2/2005 11:56:59 AM by Anonymous
Comments: i am of the opinion that you lot explain explicitly whatever you want to share with ardent readers. Half delivered examples don't help.
No ratings available.
Left on 2/1/2005 9:36:58 AM by Anonymous
Comments: Looks nice....
No ratings available.
Left on 1/27/2005 12:14:19 AM by Anonymous
Comments: this code is good
No ratings available.
Left on 1/22/2005 2:18:37 AM by Anonymous
Comments: combine two table using procedure
No ratings available.
Left on 1/21/2005 6:08:46 AM by Anonymous
Comments: Excellent Article on Performance of SP.Thanks!
Left on 1/18/2005 2:21:26 AM by Anonymous
Comments: Is a stored procedure somewhat like set processing?
Left on 1/10/2005 12:10:17 AM by Anonymous
Comments: over all it is good but it would be better if you escribe it in detail
No ratings available.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970