With a MS SQL Stored Procedure I am getting the following error “Invalid Column Name NavigationID”.
Can anyone let me know what I am doing incorrectly?
DECLARE @NavigationID INT
SET @NavigationID = 5
CREATE TABLE #tmp (NavigationID int , ParentID int);
INSERT INTO #tmp SELECT NavigationID, ParentID FROM Nav;
WITH Parent AS
(
SELECT NavigationID, ParentID FROM #tmp WHERE NavigationID = @NavigationID
UNION ALL
SELECT t.NavigationID, t.ParentID FROM Parent
INNER JOIN #tmp t ON t.NavigationID = Parent.ParentID
)
SELECT NavigationID FROM ParentID
WHERE NavigationID <> @NavigationID;
With the code you posted you get.
Change
FROM ParentIDtoFROM Parent.You also need a column
NavigationIDin tableNav.Try this:
Result:
Replace @Nav with whatever table you are using. @Nav is only here so that this code can be copied and tested.