ASPSnippets

Alerts
Get notified when a new article is published.

Name
 
Email

Your email will always be private and will not be shared.

Follow us on twitter.
 
Using Stored Procedures in SQL Server Database
Author Name: Mudassar Khan Published Date: May 07, 2009
Filed Under :
SQL Server
Views: 2155

In this article, I am explaining How to write stored procedures in SQL Server 2000 / 2005 / 2008.

It is good to follow practice of using Stored Procedures as they are precompiled thus reduce the overhead of compiling each time.

Stored Procedures also help in preventing SQL Injections since parameters are used in it. You can refer my article on Parameterized queries to avoid SQL Injection Parameterized Queries ADO.Net

Other benefits include

1. Create once and call it N number of times

2. Reduce traffic since instead of whole query only stored procedure name is sent from front end

3. You can give selected users right to execute a particular stored procedure.

 

Creating a Stored Procedure

Below figure displays the syntax for creating a stored procedure. As you can see below to create a stored procedure CREATE keyword is used.



Creating SQL Stored Procedure

Example

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE GetEmployeeDetails

      @EmployeeID int = 0

AS

BEGIN

      SET NOCOUNT ON;

      SELECT FirstName, LastName, BirthDate, City, Country

      FROM Employees WHERE EmployeeID=@EmployeeID

END

GO

 

      

Alter or Modify a Stored Procedure

Below figure displays the syntax for alter a stored procedure. As you can see below to modify a stored procedure ALTER keyword is used rest all remains the same.



Alter a SQL Stored Procedure

Example

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE GetEmployeeDetails

      @EmployeeID int = 0

AS

BEGIN

      SET NOCOUNT ON;

      SELECT FirstName, LastName, BirthDate, City, Country

      FROM Employees WHERE EmployeeID=@EmployeeID

END

GO

 

 

Drop or Delete a Stored Procedure

Figure below displays how to drop a stored procedure. As you can see below to delete a stored procedure DROP keyword is used proceeded by the name of the stored procedure.



Drop a SQL Stored Procedure

Example

DROP PROCEDURE GetEmployeeDetails

 

This completes this article. In my next article I’ll explain how to call stored procedures from front end in .net


If you like this article, help us grow by bookmarking this page on any social bookmarking site.
Bookmark and Share Page copy protected against web site content infringement by Copyscape

Related Articles

Comments

Add Comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
There is no need to add BR tags. Simply press enter for new line

Name*  
Email*
Comment*  
Security code
Security code