15,878,231 members
1.00/5 (1 vote)
See more:
How to use recursion in Stored Procedures
Posted

## Solution 1

Check this

Recursion in T–SQL[^]

Debopam Pal 28-Nov-13 1:28am
Maciej Los 28-Nov-13 11:15am
Thanks7872 30-Nov-13 6:23am
Don't you see the URL at your browser's address bar? Its of Microsoft. What do you mean by do something? He can not do anything.
Debopam Pal 30-Nov-13 6:41am
Then he should delete the URL and refer for another URL where readers can download the associated zip file.
Thanks7872 30-Nov-13 6:45am

## Solution 3

Create Stored Procedure

SQL
```CREATE PROCEDURE Factorial_ap
@Number Integer,
@RetVal Integer OUTPUT
AS
BEGIN
DECLARE @In Integer, @Out Integer
IF @Number != 1
BEGIN
SELECT @In = @Number-1
EXEC Factorial_ap @In, @Out OUTPUT
SELECT @RetVal = @Number * @Out
END
ELSE
BEGIN
SELECT @RetVal = 1
END
RETURN @RetVal
END
GO```

Call the Stored Procedure

SQL
```DECLARE @RESULT Integer
SET @RESULT = 0
EXEC Factorial_ap 5, @RESULT OUTPUT
PRINT @RESULT```

Maciej Los 28-Nov-13 11:48am
Who vote 1? Why?

## Solution 4

It is possible to use recursion with Common Table Expression[^].

SQL
```;WITH MyCTE AS
(
--initial value
SELECT 1 AS ID
UNION ALL
--recursive part
SELECT ID + 1 AS ID
FROM MyCTE
WHERE ID<100
)
SELECT ID
FROM MyCTE```

v3