Problem / Solution
Having the ability to write dynamic SQL statements is sometimes the only option besides having to write several versions of a SQL statement to cover all possible scenarios. In this video tip we look at a few different simple examples of how you can create SQL statements that are dynamic in nature. Note that dynamic SQL statements should be your last option, but in some cases this may be the only option.
Key Learning Items
- Building dynamic T-SQL statements
- Using SQL Server variables
- Using EXEC to execute dynamic SQL
- Using sp_executesql to execute dynamic SQL
Read Companion Tip

Greg Robidoux has been working with databases for 35+ years with extensive hands on SQL Server experience from version 6.5 to 2025. He has authored over 250 technical articles and delivered several presentations online and at various conventions. Greg is also the President and founder of Edgewood Solutions, a technology services company delivering services and solutions for Microsoft SQL Server.