i was asked to Create script that will expect whoever runs it to provide an employee id. Locate all employees that the provided employee supervises in any depth.
My code is :
CREATE FUNCTION [dbo].[GetNames] (@V uniqueidentifier)
RETURNS @OldNames TABLE (EMP_NAME varchar(50))
AS
BEGIN
DECLARE @master uniqueidentifier
SET @master=(SELECT EMP_Supervisor FROM Employee WHERE EMP_ID=@v)
IF @master=NULL return
INSERT INTO @OldNames(EMP_NAME)
SELECT (SELECT EMP_NAME FROM Employee WHERE EMP_ID = @master)
FROM Employee
UNION
SELECT EMP_NAME FROM GetNames(@master)
RETURN
END
And when i want to see if it works, i execute this :
SELECT * from GetNames('561e2d88-a747-460f-99e1-cfb1d3d8ca5c')
where "561e2d88-a747-460f-99e1-cfb1d3d8ca5c" is an ui of an employee and i get this as an exception:
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).
can you please help me? Thanks in advance!!