Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

Stored Procedures Introduction

A Stored Procedure is a precompiled collection of SQL statements stored inside SQL Server. It can be executed whenever required and is commonly used to perform database operations in a structured and reusable way.

What is a Stored Procedure?

A Stored Procedure is a named group of one or more SQL statements stored in the SQL Server database.

Instead of writing the same SQL query repeatedly, we can create a stored procedure once and execute it whenever required.

Simple Definition

A Stored Procedure is a reusable set of SQL statements stored in SQL Server and executed using its procedure name.

Why Use Stored Procedures?

Stored Procedures provide several benefits when developing database applications.

  • Reusability: The same procedure can be executed multiple times.
  • Maintainability: Database logic can be maintained in one place.
  • Security: Users can be given permission to execute procedures without giving direct access to underlying tables.
  • Performance: SQL Server can optimize and reuse execution plans for stored procedures.
  • Reduced Repetition: Common database operations do not need to be written again and again.

Basic Syntax of a Stored Procedure

The basic syntax for creating a stored procedure is:

SQL
CREATE PROCEDURE ProcedureName
AS
BEGIN

    -- SQL statements

END;

Simple Stored Procedure Example

Consider an Employees table. We can create a stored procedure that retrieves all employees.

SQL
CREATE PROCEDURE GetEmployees
AS
BEGIN

    SELECT *
    FROM Employees;

END;

The procedure named GetEmployees contains a SELECT statement that returns all records from the Employees table.

Executing a Stored Procedure

After creating a stored procedure, we can execute it using the EXEC statement.

SQL
EXEC GetEmployees;
Output:

The query returns all records from the Employees table.

Stored Procedure for a Specific Query

A stored procedure can contain any valid SQL statement. For example, we can create a procedure to retrieve employees from a specific department.

SQL
CREATE PROCEDURE GetITEmployees
AS
BEGIN

    SELECT *
    FROM Employees
    WHERE Department = 'IT';

END;

Execute the procedure using:

SQL
EXEC GetITEmployees;

Components of a Stored Procedure

Component Description
CREATE PROCEDURE Used to create a new stored procedure.
Procedure Name The unique name used to identify the procedure.
Parameters Optional values passed to the procedure.
AS Indicates the beginning of the procedure definition.
BEGIN ... END Contains the SQL statements executed by the procedure.
EXEC Used to execute a stored procedure.

Advantages of Stored Procedures

  • Reusable database logic.
  • Easier database maintenance.
  • Better security through controlled permissions.
  • Can accept input and output parameters.
  • Can contain multiple SQL statements.
  • Useful for implementing business logic at the database level.

Important Points

  • A stored procedure is stored inside the database.
  • It can contain one or multiple SQL statements.
  • Parameters can be used to make procedures dynamic.
  • The EXEC command is commonly used to execute a stored procedure.
  • Stored procedures can be created, modified and removed.

Summary

Stored Procedures are reusable collections of SQL statements stored inside SQL Server. They help organize database logic, improve maintainability and security, and can accept parameters to perform dynamic database operations. In the next topic, we will learn how to create a stored procedure in detail.