Stored Procedures  «Prev  Next»

Lesson 2 What is a stored procedure?
Objective Describe what a stored procedure is and how it can be used.

What Is a Stored Procedure?

A stored procedure is a named program stored inside SQL Server. It contains one or more Transact-SQL (T-SQL) statements that SQL Server can execute on demand. From an application developer’s perspective, a stored procedure is similar to a function or method: you call it by name, pass it inputs (parameters), and receive a result such as a result set, output parameters, and/or a return code.

Stored procedures are said to encapsulate functionality because callers do not need to know the internal SQL logic. They only need to know what the procedure does and how to call it correctly (the parameters it expects and the meaning of its output).

How Stored Procedures Are Used

In real systems, stored procedures are often used to implement repeatable database tasks and business transactions, such as:
  • Creating or updating an order (insert/update multiple tables in one transaction)
  • Applying a business rule (validate inputs and enforce constraints consistently)
  • Generating a report dataset (parameterized queries that return a consistent result shape)
  • Performing administrative or diagnostic tasks (using system procedures such as sp_help)
The goal is consistency: the same database operation is performed the same way every time, regardless of which application calls it.

Why Stored Procedures Can Improve Performance

Stored procedures can run efficiently because SQL Server can reuse cached execution plans for repeated calls. When SQL Server executes a parameterized procedure, it can often avoid repeating the full parse/optimize/compile cycle for every execution. This reduces overhead for high-frequency operations.

That said, performance is not “automatic.” The actual speed depends on indexing, query design, statistics, and how parameters are used. For example, the same procedure might perform differently for different parameter values due to plan selection (often discussed as parameter sensitivity or parameter sniffing). In production tuning, you focus on writing good queries first and then use procedures to package and reuse them.

SQL-Server 2019

Security and Access Control Benefits

A major reason businesses use stored procedures is controlled access. Instead of granting users direct permissions on tables, you can grant permission to execute a procedure. This supports the principle of least privilege: users can perform approved operations without being able to arbitrarily query or modify underlying data structures.

This approach is particularly useful when procedures enforce business rules (for example, validating that a status transition is allowed, or preventing updates unless conditions are met).

Advantages of Stored Procedures

Stored procedures are commonly favored over ad-hoc SQL sent from an application at runtime because they:
  1. Promote reuse by packaging a task into a named operation.
  2. Improve maintainability by centralizing business logic in one place.
  3. Support security by allowing execute-only access patterns.
  4. Enable modular design, where each procedure performs a specific unit of work.

Tradeoffs to Consider

Stored procedures also introduce design tradeoffs:
  • Schema coupling: procedure code must be maintained as tables and columns evolve.
  • Deployment discipline: changes must be versioned and deployed alongside application updates.
  • Debugging and ownership: teams must decide whether logic belongs primarily in the database layer or the application layer.
In practice, many organizations use a hybrid approach: core transactional rules in the database (procedures, constraints), and higher-level workflow orchestration in the application.

Common Types of Stored Procedures

You will encounter multiple categories of procedures in SQL Server:
  1. User-defined stored procedures: procedures you create to implement application transactions and business logic.
  2. System stored procedures: built-in procedures supplied by Microsoft to support administration and metadata inspection (many use the sp_ prefix).
  3. Extended stored procedures (legacy): procedures implemented in external DLLs (often using the xp_ prefix). These are a legacy extensibility mechanism and are generally not recommended for new development in modern SQL Server environments.

In the next lesson, you will learn how stored procedures can be used to enforce business rules.

SEMrush Software 2 SEMrush Banner 2