February 25, 2005 at 10:31 am
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/sSampath/commontableexpressionsinsqlserver2005.asp
HTH,
Srinivas Sampath
Blog: http://blogs.sqlxml.org/srinivassampath
March 2, 2005 at 1:16 am
Nice job and you got right to the point. Great comparison example against derived tables. My hat's off to you.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 2, 2005 at 2:21 am
March 2, 2005 at 5:53 am
I wiould urge DBA's to be mindful of places where CTEs get used, but views would provide either better performance or a resource that could be used in several places.
Is there any information on the performance of a CTE vs. that of a view?
March 2, 2005 at 5:56 am
Hi all,
CTE 's are really powerful with their recursive usages.
I have also written an article on http://www.kodyaz.com/article.aspx?ArticleID=18 about Common Table Expressions.
You may check it for the examples...
Thanks.
Eralper
March 2, 2005 at 6:01 am
Hi Parker,
As far as I experienced, CTEs are using same execution plans as a normal view query.
So as you wrote I believe it makes no sense to use CTEs instead of views.
But if you are working with hierarchical data then CTEs are the address to look for a reliable solution
Eralper
March 2, 2005 at 6:12 am
good material. looking forward for more enhanced feature in sql 2005
March 2, 2005 at 7:40 am
I would argue that CTEs are an excellent development tool at the very least. They might point out an area where a view might be put in place but why create a view until you need it. I would also consider using a CTE if there was only one query that needed it. If I needed it in another place I would create the view and refactor the original query to use the view instead since I would surely have used stored procedures.
[font="Tahoma"]Bryant E. Byrd, BSSE MCDBA MCAD[/font]
Business Intelligence Administrator
MSBI Administration Blog
March 2, 2005 at 8:00 am
Hi Bryant,
I think that in sql statements where you can use CTEs as views, there exists ways of writing the CTE as an inner join or sub query.
Also, I tested with a few samples and saw that there is not a notiable increase in performance.
So I meant that the real reason or necessity for a CTE is not the usage of CTE instead of a view. You are right you should not need to create a view if it will be used once. But you can not also refer to a CTE for a second time. You can use it just after the definition of the CTE.
But even this usage is a plus and gives developers a flexiblity in their coding processes.
And I tried to mention that the real power of a CTE is visible when it is used as a recursive common table expression.
Eralper
March 2, 2005 at 8:04 am
I can see the use of this CTE in many places. Since I don't have the sql 2005 installed, is it possible to use variables inside the CTEs? The below example is for illustration purpose only and of no practical use i think of:
declare @maxcount as int
set @maxcount=10
WITH TitleCount (authorID, titleCount) AS
(
SELECT au_id, COUNT(title_id)
FROM titleauthor
GROUP BY au_id having count(title_id) <= @maxcount
)
SELECT au_id, au_lname, au_fname, titleCount
FROM authors a
INNER JOIN TitleCount
ON TitleCount.authorID = a.au_id
Thanks
March 2, 2005 at 8:32 am
You can use variables in CTEs.
Below there is a sample you can widely meet in Yukon T-SQL enhancements in SQL Server 2005 books.
Note that you should use ";" after any sql statement just running before CTE (or With keyword)
Declare @Id as int
Set @Id = 13;
With SampleCTE (Id, Unit, ReportstoId, TypeId)
As
(
Select Id, Unit, ReportstoId, TypeId From CompanyUsers Where Id = @Id
Union All
Select CompanyUsers.Id, CompanyUsers.Unit, CompanyUsers.ReportstoId, CompanyUsers.TypeId
From CompanyUsers
Inner Join SampleCTE On SampleCTE.Id = CompanyUsers.ReportstoId
)
Select TypeName, Unit From SampleCTE
Inner Join Types On Types.Id = SampleCTE.TypeId
Go
Eralper
htpp://www.kodyaz.com
March 2, 2005 at 8:56 am
I don't mean to be dense but what is the difference between CTEs and temporary tables?
March 2, 2005 at 9:25 am
Temporary tables are kept until you drop them or the session is closed. And you can use them repeatedly within the session.
But CTE is only used by the Select, Insert, Update or Delete statement that comes after the CTE.
You can see the temporary table created under the tempdb user tables section by using SQL Query Analyzer.
March 2, 2005 at 10:03 am
Hello. Would it be correct to say that in views CTEs give most of the flexability of temporary tables while temporary tables still have a place in scripts since they can be "forward-referenced"?
Everett Wilson
ewilson10@yahoo.com
March 2, 2005 at 7:52 pm
Guess I didn't know that... how do you forward reference a temp table?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 31 total)
You must be logged in to reply to this topic. Login to reply