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

Creating a Stored Procedure

Learn how to create a Stored Procedure in SQL Server using the CREATE PROCEDURE statement.

Creating a Stored Procedure

A Stored Procedure is created using the CREATE PROCEDURE statement in SQL Server.

A Stored Procedure can contain one or more SQL statements. Once created, it can be executed whenever required.

Stored Procedures are useful for storing frequently used SQL operations inside the database.

Basic Syntax

The basic syntax for creating a Stored Procedure is:

CREATE PROCEDURE ProcedureName
AS
BEGIN
    -- SQL statements
END;

Simple Stored Procedure Example

Suppose we have a table named Students. We can create a Stored Procedure to display all students.

CREATE PROCEDURE GetAllStudents
AS
BEGIN

    SELECT *
    FROM Students;

END;

Executing a Stored Procedure

After creating the Stored Procedure, we can execute it using the EXEC statement.

EXEC GetAllStudents;

The above statement executes the GetAllStudents Stored Procedure and returns the records from the Students table.

Stored Procedure with WHERE Clause

A Stored Procedure can also contain conditions using the WHERE clause.

CREATE PROCEDURE GetActiveStudents
AS
BEGIN

    SELECT *
    FROM Students
    WHERE IsActive = 1;

END;

This procedure returns only the students whose IsActive value is 1.

CREATE OR ALTER PROCEDURE

SQL Server also supports CREATE OR ALTER PROCEDURE. It can be used to create a new procedure or modify an existing procedure.

CREATE OR ALTER PROCEDURE GetAllStudents
AS
BEGIN

    SELECT *
    FROM Students;

END;

Important Points

  • Stored Procedures are created using CREATE PROCEDURE.
  • SQL statements are written inside the procedure.
  • EXEC is used to execute a Stored Procedure.
  • A Stored Procedure can contain SELECT, INSERT, UPDATE and DELETE statements.
  • Parameters can be added to make a Stored Procedure reusable.
  • Stored Procedures can be modified using ALTER PROCEDURE or CREATE OR ALTER PROCEDURE.

Advantages of Stored Procedures

  • Reusable SQL code
  • Improves database code organization
  • Can accept input parameters
  • Can return result sets
  • Can contain multiple SQL statements
  • Useful for implementing database operations

Summary

A Stored Procedure is a pre-defined collection of SQL statements stored in the SQL Server database. It is created using CREATE PROCEDURE and executed using EXEC. Stored Procedures can contain different SQL operations and can also use parameters to make them more flexible and reusable.