Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
Normalization in SQL Server
DDL and DML in SQL Server
SQL Server System Databases
SQL Server System Tables
Joins in SQL Server
SQL Server - Indexes
CTE in SQL Server
SQL Server - Locks
Optimization Techniques in SQL Server
How to Setup NDF Files in SQL Server
Deadlocks in SQL Server
Transactions in SQL Server
Difference UDF vs Stored Procedure
SQL Server Triggers
SQL Server Isolation Levels
SQL Server Pivot Tables
SQL Server - Group By Statement
SQL Server - Find Second highest Number Query
SQL Server - Recursion in User Defined Function
SQL Server Error Handling
Difference Table Variable and Temporary Table
SQL Server - Temporal Tables
User Defined Functions - SQL Server
Stored Procedures - SQL Server

Stored Procedures - SQL Server



Stored Procedures in SQL Server are precompiled collections of one or more SQL statements that can be executed as a single unit. They are used to perform various database operations, such as querying data, modifying data, and managing database objects. Here' a deeper dive into Stored Procedures:

Creating a Stored Procedure

A basic example of creating a stored procedure to retrieve employee information:

sql
CREATE PROCEDURE dbo.GetEmployeeDetails
    @EmployeeID INT
AS
BEGIN
    SELECT EmployeeID, FirstName, LastName, DepartmentID, Salary
    FROM Employees
    WHERE EmployeeID = @EmployeeID;
END;

Executing a Stored Procedure

To execute the stored procedure created above, you would use the EXEC command:

sql
EXEC dbo.GetEmployeeDetails @EmployeeID = 1;

Parameters

Stored procedures can accept input parameters, output parameters, or both. Here' an example with an output parameter:

sql
CREATE PROCEDURE dbo.GetEmployeeSalary
    @EmployeeID INT,
    @Salary DECIMAL(10, 2) OUTPUT
AS
BEGIN
    SELECT @Salary = Salary
    FROM Employees
    WHERE EmployeeID = @EmployeeID;
END;

To execute this stored procedure with an output parameter:

sql
DECLARE @EmployeeSalary DECIMAL(10, 2);
EXEC dbo.GetEmployeeSalary @EmployeeID = 1, @Salary = @EmployeeSalary OUTPUT;
SELECT @EmployeeSalary;

Benefits of Stored Procedures

  • Performance: They are precompiled, reducing the need to recompile the SQL statements each time they are executed.

  • Security: They can encapsulate database logic, reducing the risk of SQL injection attacks.

  • Maintainability: Simplify complex operations and promote code reuse.

  • Transaction Control: Can include multiple statements within a single transaction.

Best Practices

  • Naming Conventions: Use consistent naming conventions for ease of maintenance.

  • Error Handling: Implement robust error handling using TRY...CATCH blocks.

  • Parameterization: Use parameters to make procedures more flexible and secure.

  • **Avoid SELECT * **: Always specify the required columns to optimize performance and reduce network traffic.

Here's an example that includes error handling:

sql
CREATE PROCEDURE dbo.UpdateEmployeeSalary
    @EmployeeID INT,
    @NewSalary DECIMAL(10, 2)
AS
BEGIN
    BEGIN TRY
        BEGIN TRANSACTION;
        
        UPDATE Employees
        SET Salary = @NewSalary
        WHERE EmployeeID = @EmployeeID;
        
        COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION;
        DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
        RAISERROR (@ErrorMessage, 16, 1);
    END CATCH
END;

Stored Procedures are powerful tools for optimizing database operations and enhancing security. Would you like to explore any specific features or scenarios involving Stored Procedures in more detail?




All rights reserved | Privacy Policy | Sitemap