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
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'