create function fn_fibo(@end int)returns varchar(255)asbegindeclare @a int, @b int,@fib int, @counter intdeclare @fstring varchar(255) set @end = @end - 2set @a = 0set @b = 1set @fib = 0set @counter = 0select @fstring = CAST(@a as varchar(10)) + ','select @fstring = @fstring + CAST(@b as varchar(10))while @counter < @endbeginselect @fstring = @fstring + ','set @fib = @a + @bset @a = @bset @b = @fibselect @fstring = @fstring + CAST(@fib as varchar(20))set @counter = @counter + 1 end

create FUNCTION fn_fibo (@end INT)RETURNS VARCHAR(255)ASBEGIN DECLARE @a INT ,@b INT ,@fib INT ,@counter INT DECLARE @fstring VARCHAR(255) SET @end = @end - 2 SET @a = 0 SET @b = 1 SET @fib = 0 SET @counter = 0 SELECT @fstring = CAST(@a AS VARCHAR(10)) + ',' SELECT @fstring = @fstring + CAST(@b AS VARCHAR(10)) WHILE @counter < @end BEGIN SELECT @fstring = @fstring + ',' SET @fib = @a + @b SET @a = @b SET @b = @fib SELECT @fstring = @fstring + CAST(@fib AS VARCHAR(20)) SET @counter = @counter + 1 END RETURN @fstringEND

create FUNCTION itvf_fibo (@end INT)RETURNS @FibNums table( FibNum int)ASBEGIN DECLARE @a INT ,@b INT ,@fib INT ,@counter INT SET @end = @end - 2 SET @a = 0 SET @b = 1 SET @fib = 0 SET @counter = 0 insert @FibNums select @a union all select @b WHILE @counter < @end BEGIN SET @fib = @a + @b SET @a = @b SET @b = @fib insert @FibNums select @fib SET @counter = @counter + 1 END return;END

CREATE FUNCTION dbo.FibNumber (@End INT)RETURNS TABLE WITH SCHEMABINDING AS RETURN WITH R3(N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1), R81(N) AS (SELECT 1 FROM R3 a, R3 b, R3 c, R3 d),cteTally(N) AS (SELECT TOP (@End) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM R81) SELECT t.N, FibNumber = CAST((1/SQRT(5))*(POWER((1+SQRT(5))/2,t.N)-POWER((1-SQRT(5))/2,t.N)) AS BIGINT) FROM cteTally t WHERE 1 = CASE WHEN @End BETWEEN 1 AND 70 THEN 1 ELSE 1/'[Error: Parameter of FibNumber greater than 70]' END;

SELECT * FROM dbo.FibNumber(20);