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 »

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 »

As part of my planning for the SQL Saturday Presentation in Vancouver I am creating an hour long presentation on Common Table Expressions. The easiest way to do a recursive query in SQL server is to use a recursive CTE (Common Table Expression). What is a Common Table Expression in SQL Server? Similar to the ease of a temporary table …

Recursive CTE’s Read more »

Other SQL Server CTE Material, Posts, and Samples On This Site CTE Hierarchy compared to the alternative Recursive CTE’s Calculating Factorials with a Recursive CTE Fibonacci Sequence CTE – With An Insert Statement Generating a Tree Path with a CTE CTE Query Performance Multiple CTE’s in a single Query Recursive CTE’s CTE Category on this site CTE Tag on this …

Common Table Expressions (CTE) Read more »

Hey there, SQL enthusiasts and fellow learners! Grab your spot and capitalize on our special pricing. I’m genuinely excited to share something I’ve been working on: the Everything SQL Server Bundle. Whether you’re just embarking on your SQL journey or you’re already deep into the SQL world, I believe this bundle will add significant value to your learning curve. Inside …

The Everything SQL Server Class Bundle Read more »

If you are reading this article becuase you are trying to repair your corruptin database, please reach out ot us at Stedman Solutions we can take a quick look and help determine your options for reparing the database. Contact us for a free 30 minute consultation. The frequency with which you should run the DBCC CHECKDB command on your SQL …

SQL Server Corruption: How Often Should I Run CheckDB Read more »

Welcome to all SQL enthusiasts! We’re ecstatic to introduce our comprehensive offering – The Everything SQL Server Bundle. Whether you’re just starting out or a seasoned professional, this bundle promises to elevate your SQL Server prowess. Course Lineup We’ve curated a diverse and powerful set of courses to ensure you’re equipped with holistic SQL Server knowledge: SQL Server Interview Questions …

The Everything SQL Server Class Bundle Read more »