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.
It can contain one or more SQL statements.
You can call it from your backend application instead of writing the SQL query every time.
Stored procedures can accept input parameters and return results.
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
All database logic is in one place.
If you need to change the logic, update the stored procedure once, and all applications using it automatically get the updated behavior.
5. Simplifies Complex Operations
Stored procedures can include conditional statements, loops, and transactions.
Good for complex business logic inside the database.
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"]);
}
}
}
Using stored procedures with EF Core or ADO.NET is secure and efficient.
Frontend frameworks like Angular or React never interact directly with SQL. They call API endpoints, which in turn call the stored procedures.
Best Practices for Beginners
Use Parameters Always: Avoid dynamic SQL inside procedures to prevent SQL injection.
Keep Procedures Focused: One procedure should do one task only.
Name Procedures Clearly: Example: GetUserById, AddUser.
Use Transactions for Critical Operations: Ensures data integrity.
Document Your Procedures: Helps you and team members understand them later.
Advantages Summary
| Advantage | Why It Matters |
|---|
| Reusability | Write once, use multiple times |
| Security | Protects database from SQL injection |
| Performance | Optimized by the database |
| Maintainability | Easy to update logic in one place |
| Supports Complex Logic | Loops, conditions, and transactions possible |
Conclusion
Stored procedures are a beginner-friendly yet powerful tool for full-stack developers.
They simplify database operations, improve security, and enhance performance.
Learning how to create, execute, and use stored procedures is essential for professional full-stack projects.
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.