Tuesday, April 29, 2008

Recursive Function SQL Server

Simple Recursive Function finding the factorial: -

create FUNCTION Factorial(@x int ) RETURNS INT
AS
BEGIN
DECLARE @i int
IF @x <= 1 SET @i = 1
ELSE
SET @i = @x * dbo.Factorial( @x - 1 )
RETURN (@i)
END

Calling the function

select dbo.factorial(4)
ans: 24

Complex Recursive function

The Problem Suppose you have an employee called John and John directly reports Nancy. Nancy directly reports to Peter and Peter directly reports to Diana. The table you have set up to model this relationship has two fields for simplicity EmployeeID varchar(50), ManagerID varchar(50). In your table are the following entries


EmployeeID ManagerID

Diana
Peter Diana
Nancy Peter
John Nancy


Now John is a very insecure person and knows he is way down on the food chain. He requests a report from you that gives him a synopsis of where he stands in the food chain of the company to figure out how far he needs to climb. You think about it for a minute and come up with this recursive function that given a person's employee id will provide a list of all his direct and indirect supervisors ordered by rank. The function looks as follows

CREATE FUNCTION ReportsTo (@EmployeeID varchar(50), @depth int)
RETURNS @retFindReports TABLE (EmployeeID varchar(50) primary key, depth int) AS BEGIN
DECLARE @ManagerID varchar(50)
DECLARE @nextDepth int
SET @nextDepth = @depth + 1
SET @ManagerID = (SELECT ManagerID FROM Employees WHERE EmployeeID = @EmployeeID)

IF @ManagerID IS NOT NULL and @ManagerID > '' and @depth <= 5 and @ManagerID <> @EmployeeID
/***We stop if a person is their own manager or a person has no manager or We've exceeded a depth of 5 (to prevent potential infinite recursion ***/
INSERT INTO @retFindReports(EmployeeID, depth)
SELECT EmployeeID , @depth As Depth FROM Employees M Where M.EmployeeID =@ManagerID
UNION
--Recurseively call the function to append on the manager of a person's manager until no more
SELECT EmployeeID, Depth FROM ReportsTo(@managerID, @nextDepth)
RETURN
END

To figure out who John's supervisors are in order of rank you perform the following select statement

SELECT * From reportsTo('John',0) order by depth DESC

This returns

Diana 2
Peter 1
Nancy 0