4> CREATE FUNCTION dbo.fibonacci(
5> @n AS int
6> )
7> RETURNS int
8> AS
9> BEGIN
10> RETURN CASE
11> WHEN @n > 1 THEN @n + dbo.fibonacci(@n - 1) --recursive invocation
12> WHEN @n IN (0, 1) THEN @n
13> ELSE NULL
14> END
15> END
16> GO
1>
2> --Invoking the Recursive fibonacci() User-Defined Function
3>
4> SELECT dbo.fibonacci(2) -- succeeds
5> GO
-----------
3
(1 rows affected)
1>
2> drop function dbo.fibonacci
3> GO
|