SQL Functions - factorial

前端 未结 12 1494
傲寒
傲寒 2020-11-30 14:55

I am a beginner in SQL Functions. What is the best way to create a function for factorial in SQL Server- Say 10!

12条回答
  •  半阙折子戏
    2020-11-30 15:29

    Another way:

    create function Fact(@num int)
    returns bigint
    as
    begin
    declare @i int = 1
    
     while @num>1
     begin
      set @i = @num *  @i
      set @num=@num-1
      end
    
    return @i
    end
    
    select dbo.Fact(5)
    

提交回复
热议问题