In this tutorial we will learn how to Create sql function to calculate the factorial with example.
1 2 3 4 5 6 7 8 9 10 11 |
Create function factor(@number int) returns int as begin Declare @i int = 1,@result int=1 while (@i<=@number) Begin Set @result = @result * @i Set @i += 1 End return @result End |
First you must run the code ebove. Then you can calculate any number of factorial with following code.
1 |
Select dbo.factor(10) |
Result::3628800
CLICK HERE TO SEE MORE EXAMPLE ABOUT SCALAR VALUED FUNCTIONS