|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, February 01, 2012 6:49 AM
Points: 178,
Visits: 463
|
|
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
Eralper SQL Server and T-SQL Tutorials and Articles Microsoft Certification and Certification Exams
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 21, 2008 2:22 PM
Points: 3,
Visits: 5
|
|
| I don't mean to be dense but what is the difference between CTEs and temporary tables?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, February 01, 2012 6:49 AM
Points: 178,
Visits: 463
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, March 23, 2009 9:44 AM
Points: 179,
Visits: 22
|
|
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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 7:51 PM
Points: 32,910,
Visits: 26,800
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, February 01, 2012 6:49 AM
Points: 178,
Visits: 463
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, March 23, 2009 9:44 AM
Points: 179,
Visits: 22
|
|
Hm, well I took it to mean the data in the table can be modified after it's creation. From the article:
Note that by defining multiple CTEs, you can incrementally build on the earlier CTEs or define new results that are then used later on. Note however, that you cannot create a CTE that uses forward-reference (a CTE that is yet to be defined).
Everett Wilson ewilson10@yahoo.com
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, September 16, 2012 3:26 AM
Points: 1,038,
Visits: 443
|
|
If I repeat the same derived table 2 or 3 times in my SQL select statement, the execution plan looks like it executes the same select code several times. Do CTEs make the execution plan more efficient in such cases or is it merely a (very) nice shorthand?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:22 PM
Points: 192,
Visits: 111
|
|
Srinivas Sampath: Very nice job with almost no extra words in explain the CTE. Some thoughts: 1. It looks like CTE could replace table variable in most of the case, is this right? 2. How long the live of CTE will be lasted? If is created in procA and it at the end called procB, could the same CTE be used in procB as a ## global temp table? Want to see more articles from you! thanks David
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, February 01, 2012 6:49 AM
Points: 178,
Visits: 463
|
|
|
|
|