![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
I had looked at CTE as just another way to write a derived table (when not dealing with recursion). I wanted to see if when I unwound the procedure if it would run the same. I tried to do the following but I get an error on MyCTE. SELECT RetailerName 'Retailer' ,sQuestionText 'Question' ,sChoiceText 'Response Choice' -- ,COUNT(*) FROM ( SELECT ... ) MyCTE WHERE (@Status = 'ALL') OR ((@Status <> 'ALL') AND (dStart = (Select MAX(dStart) from MyCTE WHERE MyCTE.RetailerName = RetailerName and MyCTE.StoreID = StoreID and MyCTE.nQuestionID = nQuestionID))) The error I get is Invalid object name: MyCTE. I thought you could refer to the derived table in my where clause but apparently not. |
#12
| |||
| |||
|
|
On Tue, 19 Oct 2010 16:25:25 -0700, tshad wrote: "Andrew Morton" wrote tshad wrote: WHERE (@Status = 'ALL') OR ((@Status <> 'ALL') AND (dStart = (Select ... Unless my brain deceives me at this early hour, A OR (NOT A AND B) is the same as A OR B. |
#13
| |||
| |||
|
|
Hugo Kornelis wrote: On Tue, 19 Oct 2010 16:25:25 -0700, tshad wrote: "Andrew Morton" wrote tshad wrote: WHERE (@Status = 'ALL') OR ((@Status <> 'ALL') AND (dStart = (Select ... Unless my brain deceives me at this early hour, A OR (NOT A AND B) is the same as A OR B. Umm... would I be wrong if @Status was NULL? |

#14
| |||
| |||
|
|
WHERE (@sRecordStatus = 'ALL') OR ((@sRecordStatus <> 'ALL') AND (dStart = (Select MAX(dStart) from @tfs WHERE @tfs.Retailer = Retailer and @tfs.StoreID = StoreID and @tfs.QuestionID = QuestionID))) |
#15
| |||
| |||
|
|
I thought the CTE actually creates temp table internally. Not in SQL Server. It is an in-line macro and the recursive CTEs are cursors and procedural code in disguise. DB2 and Oracle optimizers can make a decision to materialize or not materialize a CTE. |
#16
| |||
| |||
|
|
tshad (tfs (AT) dslextreme (DOT) com) writes: It's just curious as to what it is doing since all the time is being done by the SELECT after the CTE definition. Yes, that's the way it is. The CTE is just a definition. The CTE does nothing on it own. It is almost like it is doing the Select in the definition for each row in the SELECT statement. Yes, that is what is going on. I would have assumed that the Select with the Temp table would work the same as the CTE but obviously that is not the case. There are other products that are smart enough to do this, but SQL Server is not there yet. I agree there. |
|
-- Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx |
![]() |
| Thread Tools | |
| Display Modes | |
| |