--SQL SERVER中的函数递归;
--按1,1,2,3,5,8,13...等算出第30个出现的数字;
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE id = OBJECT_ID(N'P','FN'))
BEGIN
DROP FUNCTION P;
END
GO
CREATE FUNCTION P
(
@i INT
)
RETURNS INT
AS
BEGIN
DECLARE @RETURN INT;
IF @i = 0
BEGIN
SET @RETURN = 0;
END
ELSE IF @i = 1
BEGIN
SET @RETURN = 1;
END
ELSE
BEGIN
SET @RETURN = dbo.P(@i - 1) + dbo.P(@i - 2);
END
RETURN @RETURN;
END
GO
SELECT dbo.P(30);
GO
--查找31行到40行的数据
SELECT * FROM (SELECT *,ROW_NUMBER() OVER(ORDER BY number ASC) AS id FROM dbo.spt_values) A WHERE id BETWEEN 31 AND 40;