My stored proc looks like:
WITH MYCTE(...)
AS
(
..
)
SELECT cte.*
FROM MYCTE cte
SELECT *
FROM table1
INNER JOIN MYCTE ...
So things were working fine with a single result set, I added the last SELECT statement to my sproc and now I get an error saying it doesn’t know what MYCTE is.
Why am I not allowed to do this?
Assuming it works somehow (with your advice), do I have to change my dataset.fill call to bring back 2 result sets (tables)?
Are you trying to return a single result set? Then you’ll have to do a UNION or else use a temp table or table variable to combine them.
But to answer the question: CTEs are only good for a single SELECT (or UPDATE, DELETE, etc.) They’re actually part of the SELECT statement. So when you do the second select, it has no idea what MYCTE is supposed to be.