The basic syntax of CTE - Pinal Dave
The basic syntax of CTE - Pinal Dave
The basic syntax of CTE - Pinal Dave
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Ahmedabad SQL Server User Group<br />
Ahmedabad, Gujarat, India<br />
September 20, 2008<br />
<strong>Pinal</strong> <strong>Dave</strong><br />
Micros<strong>of</strong>t MVP –SQL Server<br />
Founder – SQLAuthority.com<br />
pinal@SQLAuthority.com<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Speaker Pr<strong>of</strong>ile<br />
• Micros<strong>of</strong>t Most Valuable Pr<strong>of</strong>essional –SQL Server<br />
• Masters <strong>of</strong> Science (Computer Networks) –<br />
University <strong>of</strong> Southern California, Los Angeles, USA.<br />
• Bachelors <strong>of</strong> Engineering (Electronics &<br />
Communication) –Nirma Institutes <strong>of</strong> Technology,<br />
Ahmedabad, India.<br />
• MCP, MCDBA, MCAD<br />
• Founder – SQLAuthority.com<br />
• 5+ Years <strong>of</strong> experience as Database Administrator<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Agenda<br />
• Introduction<br />
• Speaker Pr<strong>of</strong>ile<br />
• Agenda<br />
• SQL Server Common Table Expression(<strong>CTE</strong>)<br />
• Recursive <strong>CTE</strong><br />
• Summary<br />
• Questions & Answers<br />
• References<br />
• End Note<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Common Table Expression (<strong>CTE</strong>)<br />
• A Common Table Expression (<strong>CTE</strong>) is an expression<br />
that can be thought <strong>of</strong> as a temporary result set<br />
which is defined within the execution <strong>of</strong> a single<br />
SQL statement. A <strong>CTE</strong> is similar to a derived table in<br />
that it is not stored as an object and lasts only for<br />
the duration <strong>of</strong> the query.<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
<strong>CTE</strong> Advantages<br />
‐Using <strong>CTE</strong> improves the readability and makes<br />
maintenance <strong>of</strong> complex queries easy.<br />
‐<strong>The</strong> query can be divided into separate, simple, logical<br />
building blocks which can be then used to build more<br />
complex <strong>CTE</strong>s until final result set is generated.<br />
‐<strong>CTE</strong> can be defined in functions, stored procedures,<br />
triggers or even views.<br />
‐After a <strong>CTE</strong> is defined, it can be used as a Table or a View<br />
and can SELECT, INSERT, UPDATE or DELETE Data.<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Basic Structure <strong>of</strong> <strong>CTE</strong><br />
<strong>The</strong> <strong>basic</strong> <strong>syntax</strong> <strong>of</strong> <strong>CTE</strong>:<br />
;WITH expression_name [( column_name [,...n] )]<br />
AS<br />
(<strong>CTE</strong>_query_definition)<br />
<strong>The</strong> statement to use the <strong>CTE</strong> is:<br />
SELECT <br />
FROM expression_name;<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
<strong>CTE</strong> Example<br />
USE AdventureWorks<br />
GO<br />
;WITH EmployeeDepartment_<strong>CTE</strong> AS (<br />
SELECT EmployeeID,DepartmentID,ShiftID<br />
FROM HumanResources.EmployeeDepartmentHistory<br />
WHERE EmployeeID = 127<br />
)<br />
SELECT ecte.EmployeeId,ed.DepartmentID, ed.Name,ecte.ShiftID<br />
FROM HumanResources.Department ed<br />
INNER JOIN EmployeeDepartment_<strong>CTE</strong> ecte ON ecte.DepartmentID =<br />
ed.DepartmentID<br />
GO<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
<strong>CTE</strong> Example Resultset<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Recursive <strong>CTE</strong><br />
• Process in which query executes itself is referred as<br />
Recursive Process in which the result set is based on<br />
the output <strong>of</strong> the base query.<br />
• Unlike a derived table, a <strong>CTE</strong> can be selfreferencing<br />
and can be referenced multiple times in<br />
the same query which means in Recursive <strong>CTE</strong>, the<br />
result <strong>of</strong> <strong>CTE</strong> is repeatedly used to get the final<br />
result set.<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Recursive <strong>CTE</strong> Example<br />
;WITH Emp_<strong>CTE</strong> AS (<br />
SELECT EmployeeID, ContactID, LoginID, ManagerID, Title, BirthDate<br />
FROM HumanResources.Employee<br />
WHERE ManagerID IS NULL<br />
UNION ALL<br />
SELECT e.EmployeeID, e.ContactID, e.LoginID, e.ManagerID, e.Title,e.BirthDate<br />
FROM HumanResources.Employee e<br />
INNER JOIN Emp_<strong>CTE</strong> ecte ON ecte.EmployeeID = e.ManagerID<br />
)<br />
SELECT *<br />
FROM Emp_<strong>CTE</strong><br />
GO<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Recursive <strong>CTE</strong> Example Resultset<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Recursive <strong>CTE</strong> Example Explanation<br />
• In the given example Emp_<strong>CTE</strong> is a Common Expression Table, the base<br />
record for the <strong>CTE</strong> is derived by the first sql query before UNION ALL.<br />
<strong>The</strong> result <strong>of</strong> the query gives you the EmployeeID which don’t have<br />
ManagerID.<br />
• Second query after UNION ALL is executed repeatedly to get results and<br />
it will continue until it returns no rows. For above e.g. Result will have<br />
EmployeeIDs which have ManagerID (ie, EmployeeID <strong>of</strong> the first<br />
result). This is obtained by joining <strong>CTE</strong> result with Employee table on<br />
columns EmployeeID <strong>of</strong> <strong>CTE</strong> with ManagerID <strong>of</strong> table Employee.<br />
• This process is recursive and will continue till there is no ManagerID who<br />
doesn’t have EmployeeID.<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Different ways <strong>of</strong> using <strong>CTE</strong> 1<br />
;WITH First_<strong>CTE</strong><br />
SELECT Statement,<br />
Second_<strong>CTE</strong><br />
SELECT Statement,<br />
Third_<strong>CTE</strong><br />
SELECT Statement,<br />
SELECT Statement<br />
FROM First_<strong>CTE</strong><br />
INNER JOIN Second_<strong>CTE</strong><br />
INNER JOIN Third_<strong>CTE</strong><br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Different ways <strong>of</strong> using <strong>CTE</strong> 2<br />
;WITH First_<strong>CTE</strong><br />
SELECT Statement,<br />
Second_<strong>CTE</strong><br />
SELECT Statement<br />
FROM First_<strong>CTE</strong>,<br />
Third_<strong>CTE</strong><br />
SELECT Statement<br />
FROM Second_<strong>CTE</strong>,<br />
SELECT Statement<br />
FROM Third_<strong>CTE</strong><br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Your Ideas on <strong>CTE</strong><br />
Your Suggestions<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Summary<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
Questions and Answers<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
References<br />
• Micros<strong>of</strong>t Book Online<br />
• SQLAuthority.com<br />
• SQL Server 2008<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008
End Note<br />
• Thank You!<br />
• For any questions or queries contact me at<br />
pinal@SQLAuthority.com<br />
© <strong>Pinal</strong> <strong>Dave</strong><br />
September 20, 2008