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

Dynamic SQL

Dynamic SQL allows you to construct and execute SQL statements dynamically at runtime.

What is Dynamic SQL?

Dynamic SQL is a technique where SQL statements are created dynamically as strings and executed at runtime.

It is useful when table names, column names, filtering conditions, or other parts of a SQL statement need to be determined dynamically.

Basic Syntax

DECLARE @SQL NVARCHAR(MAX);

SET @SQL = 'SELECT * FROM Employees';

EXEC sp_executesql @SQL;

Example 1: Simple Dynamic SQL

In this example, a SQL query is stored inside a variable and executed using sp_executesql.

DECLARE @SQL NVARCHAR(MAX);

SET @SQL = 'SELECT * FROM Employees';

EXEC sp_executesql @SQL;

Example 2: Dynamic WHERE Condition

A condition can also be passed dynamically to a SQL query.

DECLARE @SQL NVARCHAR(MAX);
DECLARE @Department NVARCHAR(50);

SET @Department = 'IT';

SET @SQL =
    'SELECT * FROM Employees
     WHERE Department = @Dept';

EXEC sp_executesql
    @SQL,
    N'@Dept NVARCHAR(50)',
    @Dept = @Department;

Parameterized Dynamic SQL

Parameters should be used whenever possible instead of directly concatenating user input into a SQL string.

DECLARE @SQL NVARCHAR(MAX);

SET @SQL =
    'SELECT *
     FROM Employees
     WHERE Salary > @Salary';

EXEC sp_executesql
    @SQL,
    N'@Salary DECIMAL(10,2)',
    @Salary = 50000;

EXEC vs sp_executesql

EXEC sp_executesql
Can execute dynamic SQL Can execute dynamic SQL
Parameter handling is limited Supports parameters
String concatenation is commonly used Parameterized queries are supported
Less suitable when parameters are required Preferred for parameterized dynamic SQL

Advantages of Dynamic SQL

  • Allows SQL statements to be generated dynamically.
  • Useful when table or column names are determined at runtime.
  • Can be useful for dynamic filtering and reporting.
  • Parameterized dynamic SQL can improve security and maintainability.

Important Points

  • Avoid directly concatenating untrusted user input.
  • Prefer sp_executesql with parameters when values are supplied dynamically.
  • Dynamic SQL should be used only when it provides a real benefit over static SQL.

Summary

Dynamic SQL allows SQL statements to be constructed and executed at runtime.

It is useful for dynamic queries, filtering, reporting, and situations where parts of the SQL statement are determined at runtime. Parameterized sp_executesql is preferred when dynamic values are required.