SQL Stored Procedures
A stored procedure in SQL Server is a set of SQL statements that can be saved and reused. It is similar to a function in other programming languages. Stored procedures can accept input parameters, return output parameters, and provide a way to encapsulate logic, ensuring code reusability, security, and easier maintenance.
Creating a Simple Stored Procedure
Here is a basic example of creating and using a stored procedure in SQL Server:
Example 1: Simple Stored Procedure without Parameters
- Creating the Stored Procedure
CREATE PROCEDURE GetEmployees
AS
BEGIN
SELECT * FROM Employees
END
- Executing the Stored Procedure
EXEC GetEmployees
Output:
EmployeeID | FirstName | LastName | Department |
---|---|---|---|
1 | John | Doe | HR |
2 | Jane | Smith | Finance |
3 | Bob | Brown | IT |
Stored Procedure with Input Parameters
- Creating the Stored Procedure with Parameters
CREATE PROCEDURE GetEmployeeByID
@EmployeeID INT
AS
BEGIN
SELECT * FROM Employees WHERE EmployeeID = @EmployeeID
END
- Executing the Stored Procedure with Parameters
EXEC GetEmployeeByID @EmployeeID = 1
Output:
EmployeeID | FirstName | LastName | Department |
---|---|---|---|
1 | John | Doe | HR |
Stored Procedure with Output Parameters
- Creating the Stored Procedure with Output Parameters
CREATE PROCEDURE GetEmployeeName
@EmployeeID INT,
@FirstName NVARCHAR(50) OUTPUT,
@LastName NVARCHAR(50) OUTPUT
AS
BEGIN
SELECT @FirstName = FirstName, @LastName = LastName
FROM Employees
WHERE EmployeeID = @EmployeeID
END
- Executing the Stored Procedure and Retrieving Output Parameters
DECLARE @FirstName NVARCHAR(50)
DECLARE @LastName NVARCHAR(50)
EXEC GetEmployeeName @EmployeeID = 1, @FirstName = @FirstName OUTPUT, @LastName = @LastName OUTPUT
SELECT @FirstName AS FirstName, @LastName AS LastName
Output:
FirstName | LastName |
---|---|
John | Doe |
Stored Procedure with Input and Output Parameters
- Creating the Stored Procedure
CREATE PROCEDURE CalculateBonus
@EmployeeID INT,
@BonusAmount DECIMAL(10, 2) OUTPUT
AS
BEGIN
DECLARE @Salary DECIMAL(10, 2)
SELECT @Salary = Salary
FROM Employees
WHERE EmployeeID = @EmployeeID
SET @BonusAmount = @Salary * 0.10
END
- Executing the Stored Procedure and Retrieving Output Parameters
DECLARE @BonusAmount DECIMAL(10, 2)
EXEC CalculateBonus @EmployeeID = 1, @BonusAmount = @BonusAmount OUTPUT
SELECT @BonusAmount AS BonusAmount
Output:
BonusAmount |
---|
500.00 |
Benefits of Using Stored Procedures
- Performance: Stored procedures are cached by SQL Server, reducing the need for repeated parsing and optimizing.
- Security: Access can be restricted to the stored procedures, hiding the underlying tables and views.
- Maintainability: Logic can be encapsulated in stored procedures, making it easier to manage and update.
- Reusability: Common operations can be encapsulated into stored procedures, promoting code reuse.
These examples and explanations provide a basic understanding of how to create and use stored procedures in SQL Server. You can create more complex stored procedures by including error handling, transactions, and more complex business logic.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.