Return with case statement : Returning value « Procedure Function « SQL Server / T-SQL Tutorial






3> CREATE FUNCTION fn_factorial (@param decimal(38, 0) )
4> RETURNS decimal(38, 0)
5>
6> AS
7> BEGIN
8>     IF (@param < 0 OR @param > 32) RETURN (0)
9>     RETURN (CASE
10>         WHEN @param > 1 THEN @param  * dbo.fn_factorial(@param - 1)
11>         ELSE 1
12>         END)
13> END
14> GO
1>
2> SELECT factorial = dbo.fn_factorial(10)
3> GO
factorial
----------------------------------------
                                 3628800








21.15.Returning value
21.15.1.Multi-Statement Table-Valued Functions
21.15.2.Return with aggregate function
21.15.3.Return with case statement
21.15.4.UDFs Returning a Scalar Value