USE master
GO
EXEC sp_configure 'allow updates', 1
GO
RECONFIGURE WITH OVERRIDE
GO
CREATE FUNCTION system_function_schema.fn_factorial (@param1 int)
RETURNS NUMERIC(38, 0)
AS
BEGIN
DECLARE @counter int, @result NUMERIC(38, 0)
IF (@param1 < 0 OR @param1 > 33) RETURN (0)
SET @counter=1 SET @result=1
WHILE (@counter < @param1 AND @param1 <> 0 )
BEGIN
SET @result=@result * (@counter + 1)
SET @counter=@counter + 1
END
RETURN (@result)
END
GO
EXEC sp_configure 'allow updates', 0
GO
RECONFIGURE WITH OVERRIDE
GO
-- Invoke the function
USE pubs
GO
SELECT fn_factorial(5)