Technical Article

Recursive Factorial Function ( n ! )

,

Calculates the factorials of numbers between 1 - 31.
High level is 31 because maximum stored procedure, function, trigger, or view nesting level is limited to 32.

CREATE Function N_Faktorial
(@Input INT)
RETURNS NUMERIC(38,0)
BEGIN 

DECLARE @Output NUMERIC(38,0)
SET @Output = @Input

IF @Input = 0

BEGIN
SET @Output = 1
RETURN @Output
END

ELSE 

BEGIN
SET @Output = @Output * dbo.N_Faktorial(@Output-1)
END

RETURN @Output

END

-- SELECT dbo.N_Faktorial(5)

Rate

1 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

1 (1)

You rated this post out of 5. Change rating