7 March 2022

Recursive user-defined function in SQL Server example

Recursive user-defined function

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


How to Call Recursive user-defined function:

select * from dbo.RecursiveTest(10) 


Github Link: https://github.com/adi501/SQL-Server-Examples


No comments:

Post a Comment