[sql server]sql 2000 函数递归算法

--sql 2000 函数递归算法
if object_id('f_test') is not null drop function f_test
go
create function f_test(@dt int)
returns int
as
begin
declare @i int
select @i=@dt+isnull(dbo.f_test(number),0) from master..spt_values where type='p' and number=@dt-1
return @i
end
go
select dbo.f_test(10)

/*

-----------
55

(1 行受影响)

*/

--sql 2000 函数递归算法2
if object_id('f_test') is not null drop function f_test
go
create function f_test(@dt int)
returns numeric(10,4)
as
begin
declare @i numeric(10,4)
select @i=number+isnull(dbo.f_test(number),0)*0.7 from master..spt_values where type='p' and number=@dt-1
return @i
end
go
select number+dbo.f_test(number)*0.7 from master..spt_values where type='p' and number=3

---select (2+0.7)*0.7+3
/*
---------------------------------------
4.89000

(1 行受影响)

*/

你可能感兴趣的:(SQL Server)