Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can you please share some more examples of stored procedures
Posted

 
Share this answer
 
A stored procedure is group of T-SQL (Transact SQL) statements. If you have a situation, where you write the same query over and over again, you can save that specific query as a stored procedure and call it just by it's name.


Creating a stored procedure with input parameters: This SP, accepts GENDER and DEPARTMENTID parameters. Parameters and variables have an @ prefix in their name.




Create Procedure spGetEmployeesByGenderAndDepartment
@Gender nvarchar(50),
@DepartmentId int
as
Begin
Select Name, Gender from tblEmployee Where Gender = @Gender and DepartmentId = @DepartmentId
End


then execute your procedure using on of the below syntax:

1. EXECUTE spGetEmployeesByGenderAndDepartment 'Male', 1 ( here the order of assiging value should be same as i it is used while creating the input parameter)


2.EXECUTE spGetEmployeesByGenderAndDepartment @DepartmentId=1, @Gender ='Male'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900