Recursive procedure with output parameter : Output parameter « Procedure Function « SQL Server / T-SQL Tutorial






6>    CREATE PROC spTriangular
7>    @ValueIn int,
8>    @ValueOut int OUTPUT
9>    AS
10>    DECLARE @InWorking int
11>    DECLARE @OutWorking int
12>    IF @ValueIn != 1
13>    BEGIN
14>            SELECT @InWorking = @ValueIn - 1
15>            EXEC spTriangular @InWorking, @OutWorking OUTPUT
16>
17>            SELECT @ValueOut = @ValueIn + @OutWorking
18>    END
19>    ELSE
20>    BEGIN
21>            SELECT @ValueOut = 1
22>    END
23>    RETURN
24>    GO
1>
2>    DECLARE @WorkingOut int
3>    DECLARE @WorkingIn int
4>    SELECT @WorkingIn = 5
5>    EXEC spTriangular @WorkingIn, @WorkingOut OUTPUT
6>
7>    PRINT CAST(@WorkingIn AS varchar) + ' Triangular is ' + CAST(@WorkingOut AS varchar)
8>    GO
5 Triangular is 15
1>
2>    drop PROC spTriangular;
3>    GO








21.14.Output parameter
21.14.1.Stored procedure returning value using an Output parameter
21.14.2.Parameter for passing value out of a procedure
21.14.3.Recursive procedure with output parameter