Function problem - pages 419-420
Hi...
I have created function and I have tried to run it forEmployeeID = 2 - query runs forever without the results.
Below is a code I have used for the function:
CREATE FUNCTION dbo.fnGetReports
(@EmployeeID AS int)
RETURNS @Reports TABLE
(
EmployeeID int NOT NULL,
ReportsToID int NULL
)
AS
BEGIN
DECLARE @Employee AS int
INSERT INTO @Reports
SELECT EmployeeID, ReportsTo
FROM Employees
WHERE EmployeeID = @EmployeeID
SELECT @Employee = MIN(EmployeeID)
FROM Employees
WHERE ReportsTo = @EmployeeID
WHILE @EmployeeID IS NOT NULL
BEGIN
INSERT INTO @Reports
SELECT *
FROM fnGetReports(@Employee)
SELECT @Employee = MIN(EmployeeID)
FROM Employees
WHERE EmployeeID > @Employee AND ReportsTo = @EmployeeID
END
RETURN
END
GO
After function has been created...
SELECT * FROM fnGetReports(2)
Anybody with the same experience and solution - please help
marek
|