im trying to combine these two ideas
recursive function - http://technet.microsoft.com/en-us/library/ms186243(v=sql.105).aspx
SQL Call Stored Procedure for each Row without using a cursor
actually i have the two working individually
i can get a recursive function to work
and i can get a stored procedure to work on one row
but im having a hard time figuring out
how to get my recursive stored procedure to work on a selected row
i am also referencing this post:
sql recursive function - to find managers
so if i have a specific row
id mngr_id person_id
-- ------- ---------
12 1777 923
i want to count how many levels to the CEO which has a mngr_id = null
(as in one of the above examples)
the second part of this question:
if i wanted to do this on a random number of specific rows
would this be the efficient way to do it?
[post edited for clarification]
this code (adjusted for my sample data above)
which is from the above example 1 is excellent and works well
GO
WITH levels (mngr_id, person_id, id, Level)
AS
(
-- Anchor member definition
SELECT e.mngr_id, e.person_id, e.id, 0 AS Level
FROM MyEmployees AS e
WHERE mngr_id IS NULL
UNION ALL
-- Recursive member definition
SELECT e.mngr_id, e.person_id, e.id, Level + 1
FROM MyEmployees AS e
INNER JOIN levels AS d
ON e.mngr_id= d.person_id
)
-- Statement that executes the CTE
SELECT mngr_id, person_id, id, Level
FROM levels
where id=@whatever_random_id_i_choose -- <---- pseudocode
GO
note:
the reason why im posting is cuase my pseudocode doesnt work
and breaks when person_id is not unique
thank you all for your input