I have a typical SQL Server hierarchical query:
WITH bhp AS (
SELECT name, 0 AS level
FROM dbo.BhpNode
WHERE parent_id IS NULL
UNION ALL
SELECT a.name, level + 1
FROM dbo.BhpNode a
INNER JOIN dbo.BhpNode b
ON b.bhp_node_id = a.parent_id )
SELECT * FROM bhp
This seems to match the various examples of hierarchical queries I've found around the web, but for some reason it's producting this error:
Msg 207, Level 16, State 1, Line 12
Invalid column name 'level'.
I'm sure I'm missing something obvious, but I've stared at it too long to see it. Any idea where I'm going wrong?