SQL Stored Procedures (original) (raw)

Last Updated : 9 Apr, 2026

Stored procedures are used to group SQL statements and business logic into a single reusable unit that runs inside the database. These help in tasks such as:

**Syntax:

CREATE PROCEDURE procedure_name
(parameter1 data_type, parameter2 data_type, ...)
AS
BEGIN
   -- SQL statements to be executed
END

Types of SQL Stored Procedures

SQL stored procedures are categorized into different types based on their use case and functionality. Understanding these categories can help developers choose the right type of procedure for a specific scenario

**1. System Stored Procedures

These are predefined stored procedures provided by the SQL Server for performing administrative tasks such as database management, troubleshooting, or system configuration. Examples include:

**2. User-Defined Stored Procedures (UDPs)

These are stored procedures created by users to perform specific business operations. Examples include:

**3. Extended Stored Procedures

These are stored procedures that allow SQL Server to execute external programs written in languages such as C or C++. Examples include:

**4. CLR Stored Procedures

These are stored procedures written in .NET languages like C# and executed inside SQL Server. Examples include:

Benefits of SQL Stored Procedures

There are several key reasons why SQL Stored Procedures are widely used in database management:

**Example of Creating a Stored Procedure

In this example, creates a stored procedure GetCustomersByCountry that takes a Country and returns the CustomerName and ContactName for customers from that country.

Screenshot-2026-02-02-113057

By passing a country as a parameter, the stored procedure dynamically fetches the relevant customer details from the table

**Query:

CREATE PROCEDURE GetCustomersByCountry
@Country VARCHAR(50)
AS
BEGIN
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = @Country;
END;

EXEC GetCustomersByCountry @Country = 'Sri lanka';

**Output:

Screenshot-2026-02-02-113312

**Real-World Use Cases for SQL Stored Procedures

These are real-world scenarios where stored procedures are used.

**Best Practices for Writing SQL Stored Procedures

These are guidelines for writing better stored procedures.