Queries with Common table expressions (CTE) are made up of two parts, the CTE part, and the SQL that references the CTE.  In preparation for SQL Saturday, the question came up of can you use an INSERT or UPDATE statement with a CTE.  Referring to the documentation I confirmed that using an insert or update inside of the CTE is …

CTE – With An Insert Statement Read more »

The following example shows how to create a Tree Path with a Common Table Expression (CTE). First off what is a tree path? For this example I will be referring to the product categories at a camping and fitness store, where you have a top level with 4 categories, and some of the categories have subcategories: Camping Tents 1 Person …

Generating a Tree Path with a CTE Read more »

The following question came up when working on my CTE presentation for SQL Saturday. Does a query that JOINs a CTE to itself execute the CTE query once or twice? For instance:  ;WITH deptCTE(id, department, parent)     AS (SELECT id,department,parent FROM   Departments) SELECT q1.department,q2.department   FROM deptCTE q1  INNER JOIN deptCTE q2 ON q1.id = q2.parent  WHERE q1.parent IS NULL;    The following execution plan is produced showing that the Departments table is hit twice with a table scan …

CTE Query Performance Read more »

After being approved as a speaker at SQL Saturday 114 – Vancouver BC, and SQL Saturday 108 – Redmond WA, I have decided to give it another shot at SQL Saturday 120 in Orange County California. I have submitted 2 abstracts for sessions: The first one is titled Unleashing Common Table Expressions in SQL Server, and if accepted this will be …

Sessions Submitted for SQL Saturday #120 Orange County CA Read more »

Common Table Expressions are a great way to simplify complex derived table queries. If it makes sense to break out one derived table or subquery into a CTE, it can make sense to do multiple CTE’s. To use multiple CTE’s in a single query you just need to: Finish the first CTE Add a comma Declare the name and optional …

Multiple CTE’s in a single Query Read more »

In preparation for an upcoming SQL Saturday presentation I have created another report to add to the FREE SQL Server Health reports project. The new Index Statistics Report is the 8th report in the series, intended to give DBA’s and database developers access to the information they need to analyze the health of their database.   Here is a screen …

Index Statistics Report added to the FREE SQL Server Health Reports Read more »

In preparation for an upcoming SQL Saturday presentation I have created another report to add to the FREE SQL Server Health reports project. This report has been replaced with the Backup Report in the Database Health Project. The Backup Set Report has been added to the SQL Health Reports.  The Backup Set Report  is the 7th report to be added to the Database …

Backup Set Report added to FREE Server Health Reports Read more »