mar.ko (8/28/2015)
Ed Wagner (8/28/2015)
The statement that contains the CTE needs to end with a query that uses the CTE. Like Luis said, please post the code; that should clear up the confusion.Bingo - therein lies the problem. I was not aware of that restriction.
Honestly, that's kind of a dumb one....CTE's should persist for the duration of the procedure and be able to be referenced at any time.
temp tables exist for the duration. that's the tool you want.
CTE's are just a convenient way to format/visualize a sub select.
you can't propagate a sub select twice without doing the same work twice.(two queries cannot use it separately)
if it needs to exist, throw it in a temp table.
Lowell