Use a CTE to organize one statement, a temp table to store and reuse an intermediate result, or a table variable for local storage when its optimizer limitations are acceptable. See how to choose and test each option.
Tag: Query performance
Mastering T-SQL: Essential Commands and Techniques for SQL Server
Learn to write reliable T-SQL in SQL Server, from filtering rows and joining tables to protecting updates with transactions and reading execution plans.