I believe this needs "DECLARE <name> INSENSITIVE CURSOR FOR";otherwise its not in a loop...
Insert Into @temp
Select Distinct employeeid
From directory
Where higmanagerempid = @empid
My suggestion was: recalling a UDF with @empid (for each level) until none found. You can still share temp tables and results, so all iterations provide a complete tree search.
The UDF is passed @empid the first time and retrieves next level; it calls itself on each new @empid found until none found (thus recursive heirarchy) and until all managerial relations are found under first @empid.
The example of UDF iteration or recursion provided previously was a simple one showing UDF recursion being used to break-out a string.
Later this month I'll be building UDF recursive functions for about 3 or 4 different tables where UDF iteration will be used to retrieve data tree relations (based on same table hierarchy) & will post when complete.
Coach James