SQL Functions - factorial

前端 未结 12 1453
傲寒
傲寒 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:45

    Here is an other method to calculate factorial value of an integer in SQL Server

     create function sqlFactorial (@int int)
     returns int
     begin
      declare @factorial bigint = 1
      select @factorial = @factorial * i from dbo.NumbersTable(1,@int,1)
      return @factorial
     end
    

    You need to use a SQL numbers table for this solution. The Select statement updates the declared integer variable for each row in the FROM part with multiplying it with the ordered integer values

提交回复
热议问题