Stored Procedure in SQL Server

πŸ“Œ Introduction

When working with database in ASP.NET or C#, we often write SQL queries like:

SELECT * FROM Students

But in real projects, developers prefer using:

πŸ‘‰ Stored Procedure

In this article, you will learn:

  • What is Stored Procedure?

  • Why we use it?

  • How to create it?

  • How to execute it?

  • Example with output

  • Advantages

  • Interview questions

Everything explained in very simple words 😊

🧠 What is a Stored Procedure?

A Stored Procedure is:

πŸ‘‰ A pre-written SQL query
πŸ‘‰ Stored inside the database
πŸ‘‰ Can be executed anytime

Simple meaning:

It is like a saved function inside SQL Server.

🎯 Why We Use Stored Procedure?

Instead of writing SQL query again and again in application code:

We store it once in database and reuse it.

Benefits:

βœ” Better performance
βœ” More security
βœ” Reusable code
βœ” Easy maintenance
βœ” Cleaner application code

Real-Life Example

Think like this:

You order food from restaurant.

Instead of telling recipe every time,
Chef already saved recipe.

You just say:

β€œMake Paneer Butter Masala”

Chef knows everything.

That is Stored Procedure.

πŸ›  How to Create Stored Procedure in

Microsoft SQL Server

Step 1 – Create Table

CREATE TABLE Students
(
    Id INT PRIMARY KEY,
    Name VARCHAR(50),
    Age INT
);

Step 2 – Insert Data

INSERT INTO Students VALUES (1, 'Rahul', 22);
INSERT INTO Students VALUES (2, 'Amit', 21);
INSERT INTO Students VALUES (3, 'Neha', 23);

Step 3 – Create Stored Procedure

CREATE PROCEDURE GetAllStudents
AS
BEGIN
    SELECT * FROM Students;
END

Now procedure is saved in database.

β–Ά How to Execute Stored Procedure

EXEC GetAllStudents;

πŸ–₯ Output

Id   Name    Age
1    Rahul   22
2    Amit    21
3    Neha    23

🧠 Stored Procedure with Parameter

We can also pass values.

Example:

CREATE PROCEDURE GetStudentById
    @Id INT
AS
BEGIN
    SELECT * FROM Students WHERE Id = @Id;
END

Execute:

EXEC GetStudentById 2;

πŸ–₯ Output

Id   Name   Age
2    Amit   21

πŸ” Why Stored Procedure is More Secure?

If we write query directly in C#:

string query = "SELECT * FROM Students WHERE Id=" + id;

It may cause SQL Injection.

But stored procedure:

βœ” Protects against SQL Injection
βœ” Safer

πŸ“Š Simple Difference – Query vs Stored Procedure

Normal QueryStored Procedure
Written in codeStored in DB
Less secureMore secure
Repeated codeReusable
Hard to manageEasy to maintain

🎯 When Should You Use Stored Procedure?

Use it when:

βœ” Working with large data
βœ” Building enterprise application
βœ” Need security
βœ” Need better performance
βœ” Using ASP.NET with SQL Server

πŸ’‘ Interview Questions

Common questions:

  1. What is Stored Procedure?

  2. Difference between Function and Stored Procedure?

  3. Can Stored Procedure return value?

  4. What is parameter in Stored Procedure?

🏁 Conclusion

Stored Procedure is a powerful feature of SQL Server.

It helps to:

βœ” Improve performance
βœ” Increase security
βœ” Reuse SQL logic
βœ” Keep application clean

If you are learning ASP.NET or C#, you must understand Stored Procedures.