I cannot seem to access a temp table based on the results of a CTE expression.
how do you create a temp table, and access the temp declared within a CTE.
in the example below, the last line will throw an error.
Thanks
DECLARE @tbl TABLE
(
Id int
,ParentId int
)
INSERT INTO @tbl
( Id, ParentId )
select t_package.package_id, t_package.parent_ID from t_package
;
WITH abcd
AS (
-- anchor
SELECT id
,ParentID
,CAST(id AS VARCHAR(100)) AS [Path]
,0 as depth
FROM @tbl
WHERE ParentId = 0
UNION ALL
--recursive member
SELECT t.id
,t.ParentID
,CAST(a.[Path] + ',' + CAST( t.ID AS VARCHAR(100)) AS varchar(100)) AS [Path]
,a.depth +1
FROM @tbl AS t
JOIN abcd AS a ON t.ParentId = a.id
)
SELECT * from abcd;
insert into #TMP (id,parent,branch,depth) (select * from abcd)