Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
Common Table Expressions
16 posts, Page 1 of 2
1
2
»»
Common Table Expressions
Rate Topic
Display Mode
Topic Options
Author
Message
BaldingLoopMan
BaldingLoopMan
Posted Tuesday, December 08, 2009 3:57 PM
Old Hand
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:23 AM
Points: 304,
Visits: 457
so for example. I have the below. Is this the same basic principle as using an into #tablename and then querrying it? Are the CTE's specific to the runtime of a stored procedure or once their defined thay can be refferenced forever?
WITH EmployeeSubordinatesReport (EmployeeID, LastName, FirstName, NumberOfSubordinates, ReportsTo) AS
(
SELECT
EmployeeID,
LastName,
FirstName,
(SELECT COUNT(1) FROM Employees e2
WHERE e2.ReportsTo = e.EmployeeID) as NumberOfSubordinates,
ReportsTo
FROM Employees e
)
SELECT LastName, FirstName, NumberOfSubordinates
FROM EmployeeSubordinatesReport
Post #831093
Lynn Pettis
Lynn Pettis
Posted Tuesday, December 08, 2009 4:13 PM
SSC-Insane
Group: General Forum Members
Last Login: Yesterday @ 11:07 PM
Points: 21,625,
Visits: 27,468
BaldingLoopMan (12/8/2009)
so for example. I have the below. Is this the same basic principle as using an into #tablename and then querrying it? Are the CTE's specific to the runtime of a stored procedure or once their defined thay can be refferenced forever?
WITH EmployeeSubordinatesReport (EmployeeID, LastName, FirstName, NumberOfSubordinates, ReportsTo) AS
(
SELECT
EmployeeID,
LastName,
FirstName,
(SELECT COUNT(1) FROM Employees e2
WHERE e2.ReportsTo = e.EmployeeID) as NumberOfSubordinates,
ReportsTo
FROM Employees e
)
SELECT LastName, FirstName, NumberOfSubordinates
FROM EmployeeSubordinatesReport
They can only be referenced by the immediately following SELECT/INSERT/UPDATE/DELETE statement. Think of them more as a derived table.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #831095
Lynn Pettis
Lynn Pettis
Posted Tuesday, December 08, 2009 4:34 PM
SSC-Insane
Group: General Forum Members
Last Login: Yesterday @ 11:07 PM
Points: 21,625,
Visits: 27,468
If you haven't already, you may also want to read this
article
.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #831098
niall.baird
niall.baird
Posted Tuesday, December 08, 2009 6:22 PM
SSC Veteran
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 12:59 AM
Points: 204,
Visits: 457
Seriously? You can only use a CTE in the statement immediately following?
When I read the article, the impression I got was that you could define a CTE at the top of something like a stored proc, then use it all the way through. Is my understanding wrong?
Post #831123
Garadin
Garadin
Posted Tuesday, December 08, 2009 7:02 PM
SSCommitted
Group: General Forum Members
Last Login: Wednesday, November 07, 2012 4:08 PM
Points: 1,525,
Visits: 4,047
niall.baird (12/8/2009)
Seriously? You can only use a CTE in the statement immediately following?
When I read the article, the impression I got was that you could define a CTE at the top of something like a stored proc, then use it all the way through. Is my understanding wrong?
Lynn Pettis(12/8/2009)
They can only be referenced by the immediately following SELECT/INSERT/UPDATE/DELETE statement.
Seth Phelabaum
Consistency is only a virtue if you're not a screwup.
Links
:
How to Post Sample Data
::
Running Totals
::
Tally Table
::
Cross Tabs/Pivots
::
String Concatenation
Post #831128
niall.baird
niall.baird
Posted Tuesday, December 08, 2009 7:07 PM
SSC Veteran
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 12:59 AM
Points: 204,
Visits: 457
I *can* read Garadin. That is why I was questioning the above statement. Seems to me that defining a #table would be more use, as you can use it in more places - unless of course, I am missing something.
Post #831129
Lynn Pettis
Lynn Pettis
Posted Tuesday, December 08, 2009 7:29 PM
SSC-Insane
Group: General Forum Members
Last Login: Yesterday @ 11:07 PM
Points: 21,625,
Visits: 27,468
In SQL Server 2000, at a previous employer, I wrote several queries that used the same derived table several times in a single select statement. It was a royal pain to write, and when the derived table had to be modified it had to be modified in several places. Enter SQL Server 2005 and CTE's. Now, just before the select statement, I can define to "derived table" once and use it several times, just like a table in the select statement. If the CTE needs to be modified, one place to make the change.
CTE's are very useful when used properly. Sometimes a temporary table is a better option in a stored proc.
Lynn Pettis
For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here
or
when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here
and
here
Managing Transaction Logs
SQL Musings from the Desert
Fountain Valley SQL
(My Mirror Blog)
Post #831131
niall.baird
niall.baird
Posted Tuesday, December 08, 2009 7:31 PM
SSC Veteran
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 12:59 AM
Points: 204,
Visits: 457
That makes a bit more sense - thanks for explaining, Lynn
Post #831132
Garadin
Garadin
Posted Tuesday, December 08, 2009 8:52 PM
SSCommitted
Group: General Forum Members
Last Login: Wednesday, November 07, 2012 4:08 PM
Points: 1,525,
Visits: 4,047
niall.baird (12/8/2009)
I *can* read Garadin. That is why I was questioning the above statement. Seems to me that defining a #table would be more use, as you can use it in more places - unless of course, I am missing something.
heh, I couldn't resist.
Seriously though, don't take Lynn's word for it, run up a quick test and see for yourself.
They see a ton of usage even with their limitations. They *can* also incur less overhead than creating a temp table, depending on your usage.
Seth Phelabaum
Consistency is only a virtue if you're not a screwup.
Links
:
How to Post Sample Data
::
Running Totals
::
Tally Table
::
Cross Tabs/Pivots
::
String Concatenation
Post #831139
BaldingLoopMan
BaldingLoopMan
Posted Wednesday, December 09, 2009 7:13 AM
Old Hand
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:23 AM
Points: 304,
Visits: 457
All very useful info guys. Once again it appears the geniuses at sql server have designed a new way to do the same thing but w/ different syntax. Sure there are some minor performance gains as usual. Sorry for the disgruntled attitude. The gators lost, I just got in, and haven’t finished my coffee. Also I just ate about 2 lbs of bacon at an all u can eat breakfast buffet. Bacon is my weakness and I’m paying for it now.
Personally i develop using the #tables then convert them to @tables where needed when moving my code to prod. I like the #tables in development because i can see what is in them after the process runs and so on. It's essential to the dev process.
Perhaps i will start to integrate these CTE's going forward and be a good boy.
Post #831382
« Prev Topic
|
Next Topic »
16 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.