You are on page 1of 7

Sql Server - How To Write a Stored Procedure in SQL Server - CodePr... http://www.codeproject.

com/Articles/126898/Sql-Server-How-To-Writ...

12,503,838 members (54,559 online) Sign in

Search for articles, questions, tips

home articles quick answers discussions features community help

Articles » Database » Database » SQL Server
 

Sql Server - How To Write a Stored Procedure in SQL Server
Vivek Johari, 12 Nov 2010 CPOL Rate this:
   4.80 (106 votes)

Stored Procedure in SQL Server can be defined as the set of logically group of SQL statement which are grouped to perform a
specific task. This article will describe the Stored Procedure in SQL Server and their benefits.

Stored Procedure: Stored Procedure in SQL Server can be defined as the set of logical group of SQL statements which are
grouped to perform a specific task. There are many benefits of using a stored procedure. The main benefit of using a stored
procedure is that it increases the performance of the database.The other benefits of using the Stored Procedure are given below.

Benefits of Using the Stored Procedure
1. One of the main benefits of using the Stored procedure is that it reduces the amount of information sent to the database
server. It can become a more important benefit when the bandwidth of the network is less. Since if we send the SQL
query (statement) which is executing in a loop to the server through network and the network gets disconnected, then
the execution of the SQL statement doesn't return the expected results, if the SQL query is not used between Transaction
statement and rollback statement is not used.
2. Compilation step is required only once when the stored procedure is created. Then after it does not require
recompilation before executing unless it is modified and reutilizes the same execution plan whereas the SQL statements
need to be compiled every time whenever it is sent for execution even if we send the same SQL statement every time.
3. It helps in re usability of the SQL code because it can be used by multiple users and by multiple clients since we need to
just call the stored procedure instead of writing the same SQL statement every time. It helps in reducing the
development time.
4. Stored procedure is helpful in enhancing the security since we can grant permission to the user for executing the Stored
procedure instead of giving permission on the tables used in the Stored procedure.
5. Sometimes, it is useful to use the database for storing the business logic in the form of stored procedure since it makes
it secure and if any change is needed in the business logic, then we may only need to make changes in the stored
procedure and not in the files contained on the web server.

How to Write a Stored Procedure in SQL Server
Suppose there is a table called tbl_Students whose structure is given below:

Hide   Copy Code
CREATE TABLE tbl_Students

(

1 de 7 28/09/2016 2:10

lastname. http://www.com') Insert into tbl_Students (Firstname. lastname..CodePr. 'Johari'. lastname. lastname. Email) Values('Abhishek'. Then. 'Kumar'. 'manish@abc. Studentid of the student ) AS BEGIN SELECT Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END We can also collect the student name in the output parameter of the Stored Procedure.. suppose we need to create a Stored Procedure which will return a student name whose studentid is given as the input parameter to the stored procedure. Email) Values('Pankaj'. [Firstname] [nvarchar](200) NOT NULL.How To Write a Stored Procedure in SQL Server .com') Insert into tbl_Students (Firstname. the first step will be to write the Create Procedure statement as the first statement: Hide   Copy Code Create Procedure Procedure-name ( Input parameters .codeproject. Email) Values('Manish'.com') Now. Email) Values('Vivek'. [Email] [nvarchar](100) NULL ) Support we insert the following data into the above table: Hide   Copy Code Insert into tbl_Students (Firstname.comm') Insert into tbl_Students (Firstname. Output Parameters (If required) ) As Begin Sql statement used in the stored procedure End Now. Email) Values('Amit'.1) NOT NULL. 'Kumar'. 'abhishek@abc. 'Singh'.com/Articles/126898/Sql-Server-How-To-Writ. [Studentid] [int] IDENTITY(1. 'amit@abc.. while writing a Stored Procedure. 'pankaj@abc. lastname. 'Singh'.com') Insert into tbl_Students (Firstname. 'vivek@abc. For example: Hide   Copy Code /* GetstudentnameInOutputVariable is the name of the stored procedure which uses output variable @Studentname to collect the student name returns by the stored procedure */ Create PROCEDURE GetstudentnameInOutputVariable ( 2 de 7 28/09/2016 2:10 ..Sql Server . [Lastname] [nvarchar](200) NULL. the Stored Procedure will be: Hide   Copy Code /* Getstudentname is the name of the stored procedure*/ Create PROCEDURE Getstudentname( @studentid INT --Input parameter .

*/ Create Procedure InsertStudentrecord ( @StudentFirstName Varchar(200). @StudentEmail=email FROM tbl_Students WHERE studentid=@studentid END Note: It is not necessary that a stored procedure will have to return.Sql Server .. Studentid of the student @studentname VARCHAR (200) OUT.How To Write a Stored Procedure in SQL Server .Output Parameter to collect the student email ) AS BEGIN SELECT @studentname= Firstname+' '+Lastname. delete or update a SQL statement.CodePr. @studentid INT.@StudentEmail) End Execution of the Stored Procedure in SQL Server 3 de 7 28/09/2016 2:10 . Email) Values(@StudentFirstName. @StudentLastName. Hide   Copy Code /* This Stored procedure is used to Insert value into the table tbl_students. --Input parameter .Out parameter declared with the help of OUT keyword ) AS BEGIN SELECT @studentname= Firstname+' '+Lastname FROM tbl_Students WHERE studentid=@studentid END Note:-/* */ is used to write comments in one or multiple lines -. --Input parameter . @StudentEmail Varchar(50) ) As Begin Insert into tbl_Students (Firstname. http://www.. Now if we want to get student email address through the same procedure GetstudentnameInOutputVariable. So we need to modify it by adding one more output parameter "@StudentEmail " which is shown below: Hide   Copy Code /* Stored Procedure GetstudentnameInOutputVariable is modified to collect the email address of the student with the help of the Alert Keyword */ Alter PROCEDURE GetstudentnameInOutputVariable ( @studentid INT. For example.com/Articles/126898/Sql-Server-How-To-Writ. a stored procedure can be used to Insert.codeproject.. -. lastname..is used to write a comment in a single line How to Alter a Stored Procedure in a SQL Server In SQL Server. a stored procedure can be modified with the help of the Alter keyword. @StudentLastName Varchar(200). Studentid of the student @studentname VARCHAR(200) OUT -. the below stored procedure is used to insert value into the table tbl_students.Output parameter to collect the student name @StudentEmail VARCHAR (200)OUT -. For example. There can be a case when a stored procedure doesn't returns anything.

if we want to execute the stored procedure "Getstudentname".Declaring the variable to collect the Studentname Declare @Studentemail as nvarchar(50) -. we can say that a Stored procedure not only enhances the possibility of reusing the code and execution plan."Select" Statement is used to show the output from Procedure Summary In the end.com/Articles/126898/Sql-Server-How-To-Writ. Execution of the Stored Procedure which doesn't have an Output Parameter A stored procedure is used in the SQL Server with the help of the "Execute" or "Exec" Keyword.codeproject. http://www.How To Write a Stored Procedure in SQL Server .Sql Server . then we first need to declare the variable to collect the output values.. @Studentname output. For example.CodePr.5 years of experience in database. @Studentemail output select @Studentname... Degree:- Master Degree in Computer(MCA) 4 de 7 28/09/2016 2:10 .@Studentemail -. but it also increases the performance of the database by reducing the traffic of the network by reducing the amount of information sent over the network. For example: Hide   Copy Code Declare @Studentname as nvarchar(200) -. Hide   Copy Code Execute Getstudentname 1 Exec Getstudentname 1 Execution of the Stored Procedure using the Output Parameter If we want to execute the Stored procedure "GetstudentnameInOutputVariable" . along with any associated source code and files. License This article. then we will use the following statement. is licensed under The Code Project Open License (CPOL) Share EMAIL TWITTER About the Author Vivek Johari Technical Lead India I am currently working as a Analyst and have around 7..Declaring the variable to collect the Studentemail Execute GetstudentnameInOutputVariable 1 .

How To Write a Stored Procedure in SQL Server .Functions.com/Articles/126898/Sql-Server-How-To-Writ.Triggers etc.codeproject.Sql Server .5 and Microsoft SQL Server 2014 Comments and Discussions   You must Sign In to use this message board. Oracle certified profession DBA in ORACLE 10g (OCP) certified profession DBA in ORACLE 9i (OCP) My other publication Technical Blog:. Database Server Maintenance. Designing SQL Reports using SSRS. Certification:- Microsoft certified Sql DBA in Sql server 2008 (MCTS). Database Optimization.Technologies with Vivek Johari Moderator and Blogger at BeyondRelational.. Writing Complex Stored Procedures.. Search Comments   Go Profile popups    Spacing Relaxed   Layout Normal   Per page 25     Update First Prev Next Thanks Member 12173401 31-Jul-16 0:36  5 de 7 28/09/2016 2:10 .com You may also be interested in.. Designing and developing SSIS & DTS packages. http://www...com Guest Author and Blogger at sqlservercentral. Overview of SQL Server Stored Pro Big Data: Why Transaction Procedure Data is Mission Critical to Success Stored Procedure Generator Microsoft Data Science Virtual For SQL Server Machine for Windows and Linux now available Pro Value of Database Resilience: Deep Learning on Windows: A Comparing Costs of Downtime Getting Started Guide for IBM DB2 10. Microsoft certified BI professional in Sql server 2008 (MCTS).CodePr. Work experience:- Designing of the database..

asax page i got some error. here in global. now i'm trying to do a project Member 12393172 15-Mar-16 0:33  something related to mail access. i 'm new to asp. Thank Member 12173401 31-Jul-16 0:36  Short and clear Member 8648152 26-Jul-16 1:11  Thank You Member 12613361 30-Jun-16 20:33  My vote of 5 Kasar_7 29-Jun-16 2:46  Hi..net. My vote of 5 AnantPithadiya 1-Jan-16 0:08  reply Musheer Ali TM 6-Sep-15 19:07  Re: reply Vivek Johari 6-Sep-15 20:30  thanks abb4s 6-Jun-15 9:19  Re: thanks Vivek Johari 9-Jun-15 6:45  Good Suggestion Member 11664765 4-May-15 20:29  Re: Good Suggestion Vivek Johari 9-Jun-15 6:45  Thank you Pr!y@ 20-Feb-15 8:24  Re: Thank you Vivek Johari 9-Jun-15 6:44  Great article but you should use "return" instead of MadhureshK 8-Oct-14 2:45  "written" Re: Great article but you should use "return" instead of ManojSridhar91 19-Mar-15 1:36  "written" Re: Great article but you should use "return" instead of Vivek Johari 24-Mar-15 21:46  "written" This extremely simplistic useless article PokemonCraft 24-Aug-14 13:55  Re: This extremely simplistic useless article Vivek Johari 24-Mar-15 2:12  Methods of Writing Appllication Member 10977161 1-Aug-14 4:27  Re: Methods of Writing Appllication Akhil Mittal 3-Aug-14 23:17  Use of Output parameters Ajay Shedge 21-Jul-14 2:15  output parameter with WITH Member 10949365 15-Jul-14 18:48  -..Sql Server .CodePr.How To Write a Stored Procedure in SQL Server ..codeproject. http://www. Member 10904598 10-Jul-14 0:54  Last Visit: 31-Dec-99 18:00     Last Update: 26-Sep-16 14:32 Refresh 1 2 3 4 Next » 6 de 7 28/09/2016 2:10 ..com/Articles/126898/Sql-Server-How-To-Writ. someone please help me to solve this.

1 | Last Updated 12 Nov 2010 Everything else Copyright © CodeProject. Ctrl+Shift+Left/Right to switch pages. Permalink | Advertise | Privacy | Terms of Use | Mobile Layout: fixed | fluid Article Copyright 2010 by Vivek Johari Web02 | 2.8..CodePr.Sql Server . http://www.160919.com/Articles/126898/Sql-Server-How-To-Writ..How To Write a Stored Procedure in SQL Server .. Ctrl+Up/Down to switch threads.. General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin    Use Ctrl+Left/Right to switch messages. 1999-2016 7 de 7 28/09/2016 2:10 .codeproject.