SQL Tutorial: How to Create Stored Procedure in SQL Server

How To Create a Stored Procedure in SQL Server

Stored Procedure are a batch of SQL statements that can executed in several ways. Most of the major DBMS’s support stored procedure. The Benefit of stored procedure is that you can centralize your database access logic in a single place which is very easy to optimize.

This topics will show you how to create a stored procedure in sql server and how to run it.

Create a stored procedure:

Let’s say we have a table named Employee in our database. Employee Table has following column

EmployeeID, FirstName,LastName,Salary

So what we wants to do is write a stored procedure which will return a list of employee by a filtering FirstName and LastName

CREATE PROCEDURE SpGetEmployeeList
	
@FirstName nvarchar(50),
@LastName nvarchar(50)

AS
SELECT *FROM Employee WHERE FirstName = @FirstName
and @LastName = @LastName

Run the above query on your database where the Employee Table with following column already exists. Make sure you have that stored procedure in your selected database.

After successfully running it, you are now able to execute it by following command:

Exec SpGetEmployeeList 'John','Smith'

 

Nahidur Rahman Rifath

Passionate Software Engineer, Content Writer, Reader

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *