• How to write a stored procedure in SQL Server

    by  • May 23, 2013 • Microsoft SQL Server • 2 Comments

    In a simple way, a stored procedure is a set of SQL statements and these statements are grouped to perform some tasks in SQL Server. It is actually a saved set of one or more queries. You can write different statements of INSERT, UPDATE, DELETE and SELECT in one stored procedure. The basic purpose of stored procedure is to increase the performance of database. Another benefit is the reusability of the same set of statements.

    Writing stored procedure in simple. It is same like writing a function or method in any language. You just need to do following to write a stored procedure.

    Define a name

    Give one or more parameters or it can be without parameter

    Write one or more queries

    Return values or without any returning values

    Syntax to create a stored procedure in SQL Server

    Here is the simple stored procedure I have created for you.

    I have given a single parameter and written a single statement in my stored procedure but you can give multiple parameters and write multiple statements in you stored procedure according to your requirements.  Here in this stored procedure, I have provided INPUT parameters. We can also write a stored procedure with OUTPUT parameters. I will explain it in upcoming article.

    Now let’s see how we can execute this stored procedure. I want to get record of the customer which belongs to the CustomerID 5.

    Best WordPress Themes and Plugins with Great Team and Support!

    Avatar of GetCodeSnippet.com


    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    2 Responses to How to write a stored procedure in SQL Server

    1. Matt
      May 24, 2013 at 10:06 am


      I like your website and code samples. Can you please also add code samples for Windows Presentation Foundation (WPF), Windows Communication Foundation (WCF), Windows Services, Web Services, and LINQ etc. So that the new guys can learn those Microsoft technologies as well. This will be great help. 🙂


      • Avatar of admin
        May 25, 2013 at 2:53 am

        Hey Matt,

        Thanks for the suggestion. 🙂 We’re working in that direction and you’ll soon see articles on these technologies as well. Thanks again for getting in touch.

    Leave a Reply