A Recursive User-Defined Function (SQL Server 2000)
Anyone know what this will return?
create function RecursiveTest(@V int)
returns @t table (i int)
as
begin
set @v = @v - 1
if @v<>0
insert into @t
select @v union
select * from dbo.RecursiveTest(@v)
else
insert into @t values (0)
return
end
select * from dbo.RecursiveTest(10)
Try it out!
Unfortunately, the argument must be less than or equal to 32 due to the number of levels of recursion SQL can handle. It's probably not very efficient, either. But, it's pretty cool!
Legacy Comments
Jay
2003-11-25 |
re: User-Defined Function Recursion That is pretty cool! I actually was trying something like this for the Fibonacci series post and then noticed your post. A nice tool to have for small recursive functions. Although 32 does not give you much headroom. |
Annesh Singh
2005-10-14 |
re: User-Defined Function Recursion Yep, this is quite neat, i had built on this idea for to pull through a small hierachy of data for work purposes. Shot! |
kautilya
2010-03-31 |
re: A Recursive User-Defined Function (SQL Server 2000) thnax. It's really helpful to me. |