Consider the following cte
with Folders as
(
select a, b, c from foo
),
SubFolders as
(
select
x.d
, x.e
from
(
select
bla as d
, boo as e
from
foobar
) x
group by
x.d,
x.e
),
FullTable as
(
select * from Folders
cross join Subfolders
)
select * from Fulltable left join Subfolders on ...;
select s.d from Subfolders s; -- obviously does not work
(In reality, the cte has more expressions). I would now like query the resultset of Subfolders
elsewhere in my script. The rest of the cte-expressions I only need once.
What is the most elegant way of doing this (plus: leaving the script as much as is)? I tried what is mentioned in Combining INSERT INTO and WITH/CTE but it gives an error: The SELECT list for the INSERT statement contains fewer items than the insert list
even though I am positive that Subfolders
ahs only 2 columns. What am I doing wrong?
.
.
.
),
FullTable as
(
select * from Folders
cross join Subfolders
)
insert into #temp (
Subfolders.d
, Subfolders.e
)
select * from Fulltable left join Subfolders on ...;
...