I was asked in an interview to create a function to calculate factorial of a number, that calls itself recursively.
My attempt to do this resulted in an error message:
"Msg 217, Level 16, State 1, Line 1 Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32)."
Here is the query:
CREATE FUNCTION fnGetFactorial ( @num INT )
RETURNS INT
AS
BEGIN
DECLARE @result INT
, @n INT
WHILE ( @num <> 1 )
BEGIN
SELECT @n = dbo.fnGetFactorial(@num)
SELECT @result = @num * @n
SET @num = @num - 1
END
RETURN @result
END
So, is it possible do it in a similar manner to my code above, with minimal modification? I don't want to use a CTE.