Introduction

When working with databases in full-stack development, you often need to perform complex operations like retrieving, updating, or deleting data.

While simple SQL queries can handle basic tasks, stored procedures provide a more structured, reusable, and efficient way to interact with the database.

In this article, we will explain what stored procedures are, how they work, and why beginners should start using them in their projects.

What is a Stored Procedure?

A stored procedure is a predefined SQL program stored inside the database.

In simple terms:

A stored procedure is like a function in programming, but it runs inside the database.

Why Use Stored Procedures?

1. Reusability

Example

CREATE PROCEDURE GetAllUsers
ASBEGIN
    SELECT * FROM Users;
END;

2. Improved Performance

3. Security

4. Maintainability

5. Simplifies Complex Operations

How to Create a Stored Procedure

Example: Get All Users

CREATE PROCEDURE GetAllUsers
ASBEGIN
    SELECT Id, Name, Email, CreatedAt
    FROM Users
END;

Example: Get User by ID with Parameter

CREATE PROCEDURE GetUserById
    @UserId INTASBEGIN
    SELECT Id, Name, Email, CreatedAt
    FROM Users
    WHERE Id = @UserIdEND;

How to Execute a Stored Procedure

-- Execute without parametersEXEC GetAllUsers;

-- Execute with parameterEXEC GetUserById @UserId = 1;

Stored Procedures with INSERT, UPDATE, DELETE

Example: Insert User

CREATE PROCEDURE AddUser
    @Name NVARCHAR(100),
    @Email NVARCHAR(100)
ASBEGIN
    INSERT INTO Users (Name, Email)
    VALUES (@Name, @Email)
END;

Execute

EXEC AddUser @Name = 'Rahul Sharma', @Email = '[email protected]';

Example: Update User

CREATE PROCEDURE UpdateUserEmail
    @UserId INT,
    @Email NVARCHAR(100)
ASBEGIN
    UPDATE Users
    SET Email = @Email
    WHERE Id = @UserIdEND;

Execute

EXEC UpdateUserEmail @UserId = 1, @Email = '[email protected]';

Example: Delete User

CREATE PROCEDURE DeleteUser
    @UserId INTASBEGIN
    DELETE FROM Users
    WHERE Id = @UserIdEND;

Execute

EXEC DeleteUser @UserId = 2;

Using Stored Procedures in Full-Stack Projects

ASP.NET Core Example

using (var command = _dbContext.Database.GetDbConnection().CreateCommand())
{
    command.CommandText = "GetUserById";
    command.CommandType = System.Data.CommandType.StoredProcedure;

    var param = command.CreateParameter();
    param.ParameterName = "@UserId";
    param.Value = 1;
    command.Parameters.Add(param);

    _dbContext.Database.OpenConnection();
    using (var result = command.ExecuteReader())
    {
        while (result.Read())
        {
            Console.WriteLine(result["Name"]);
        }
    }
}

Best Practices for Beginners

  1. Use Parameters Always: Avoid dynamic SQL inside procedures to prevent SQL injection.

  2. Keep Procedures Focused: One procedure should do one task only.

  3. Name Procedures Clearly: Example: GetUserById, AddUser.

  4. Use Transactions for Critical Operations: Ensures data integrity.

  5. Document Your Procedures: Helps you and team members understand them later.

Advantages Summary

AdvantageWhy It Matters
ReusabilityWrite once, use multiple times
SecurityProtects database from SQL injection
PerformanceOptimized by the database
MaintainabilityEasy to update logic in one place
Supports Complex LogicLoops, conditions, and transactions possible

Conclusion

Stored procedures are a beginner-friendly yet powerful tool for full-stack developers.

Once you are comfortable with stored procedures, you can integrate them with ASP.NET Core APIs or Node.js backends to build robust and secure applications.