Here, I have constructed a query that provides a solution in SQL Server to determine the factorial of a specified number. If you have programmed in languages like C, C++ or other languages then you are probably familiar with the word factorial. Transact-SQL also gives you this option to repeat the expression using CTE (Common Table Expression). A factorial is denoted by n!. The factorial of a number is defined as n! = 1 * 2 * ... * n.
Let's have a look at a practical example. The example is developed in SQL Server 2012 using the SQL Server Management Studio.
SQL Common Table Expression (CTE)
SQL Server has a very powerful feature that has been added for the programmers' benefit: Common Table Expression (CTE). Common Table Expressions, or CTE, are a new construct introduced in Microsoft SQL Server 2005 that offers a more readable form of the derived table that can be declared once and referenced multiple times in a query.
We have a simple table Employeein our database.

Example
- ;WITH EmployeeCTE AS
- ( SELECT [EmpID]
- ,[EmpName]
- ,[EmpSalary]
- FROM [master].[dbo].[Employee]
- WHERE [EmpSalary]>4000
- )
- SELECT * FROM EmployeeCTE
Output

Calculating Factorial of Numbers using CTE
The example of factorial numbers should look as follows:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
The following code defines how to find the factorial of a number:

Sowjanya DPosted Nov 7, 2014, 3:50 AM
nice...can u give me your mail id so that i can send my file to solve some quiries which i have doubts