[SQL Server] SQL 2000 function Recursion Algorithm

Source: Internet
Author: User

-- SQL 2000 function RecursionAlgorithm
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

(One row is affected)

*/

 

-- SQL 2000 function recursive algorithm 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

(One row is affected)

*/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.