In SQL Server, a stored procedure is a precompiled collection of one or more SQL statements that perform a specific task. Stored procedures are used to encapsulate and organize SQL code, improve code reusability, and enhance security. They can accept input parameters, return output parameters, and execute a series of SQL statements as a single unit of work.
Here's how you can create a simple stored procedure in SQL Server:
Syntax:
CREATE PROCEDURE procedure_name
@parameter1 datatype1,
@parameter2 datatype2,
-- Add more parameters as needed
AS
BEGIN
-- SQL statements go here
END;
Example: Let's create a stored procedure that retrieves employees from a table based on a specified department:
CREATE PROCEDURE GetEmployeesByDepartment
@deptName NVARCHAR(50)
AS
BEGIN
SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE Department = @deptName;
END;
In this example:
CREATE PROCEDURE GetEmployeesByDepartment: This line declares the name of the stored procedure.
@deptName NVARCHAR(50): Here, @deptName is an input parameter of type
NVARCHAR(50) that represents the department name.
AS: Indicates the beginning of the stored procedure's body.
SELECT EmployeeID, FirstName, LastName FROM Employees WHERE Department = @deptName;: This is the SQL statement inside the stored procedure. It retrieves employee information based on the specified department.
To execute the stored procedure, you can use the EXEC or
EXECUTE statement:
EXEC GetEmployeesByDepartment @deptName = 'HR';
This executes the GetEmployeesByDepartment stored procedure with the department name parameter set to 'HR'.
Advantages of Stored Procedures:
Code Reusability:
Stored procedures can be reused in multiple parts of an application, reducing the need to duplicate code.
Improved Performance:
Stored procedures are precompiled and stored in the database, leading to better performance compared to dynamically executed SQL statements.
Security:
Permissions on tables can be restricted, and users can be granted permission to execute stored procedures without direct access to underlying tables.
Modularity:
Large and complex tasks can be divided into smaller, more manageable stored procedures, making the code easier to maintain.
Encapsulation:
Stored procedures encapsulate the logic, making it easier to update and maintain the code without affecting the application.
Remember to choose meaningful names for your stored procedures and follow best practices to enhance code readability and maintainability.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In SQL Server, a stored procedure is a precompiled collection of one or more SQL statements that perform a specific task. Stored procedures are used to encapsulate and organize SQL code, improve code reusability, and enhance security. They can accept input parameters, return output parameters, and execute a series of SQL statements as a single unit of work.
Here's how you can create a simple stored procedure in SQL Server:
Syntax:
Example: Let's create a stored procedure that retrieves employees from a table based on a specified department:
In this example:
CREATE PROCEDURE GetEmployeesByDepartment: This line declares the name of the stored procedure.
@deptName NVARCHAR(50): Here, @deptName is an input parameter of type NVARCHAR(50) that represents the department name.
AS: Indicates the beginning of the stored procedure's body.
SELECT EmployeeID, FirstName, LastName FROM Employees WHERE Department = @deptName;: This is the SQL statement inside the stored procedure. It retrieves employee information based on the specified department.
To execute the stored procedure, you can use the EXEC or EXECUTE statement:
This executes the GetEmployeesByDepartment stored procedure with the department name parameter set to 'HR'.
Advantages of Stored Procedures:
Code Reusability:
Improved Performance:
Security:
Modularity:
Encapsulation:
Remember to choose meaningful names for your stored procedures and follow best practices to enhance code readability and maintainability.