Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 Stored Procedure with multiple output parameters

I have a stored procedure as follows:

ALTER PROCEDURE [dbo].[sp_web_orders_insert]
(
    @userId int = default,
    @custId int = default,
    @orderDate datetime = default,
    @orderTotal money = default,
    @statusId int = default,
    @orderReference varchar(50) = default,
    @custReference varchar(50) = default,
    @order_ID INT output,
    @orderReferenceOutput varchar(50) output
)
AS


    SET NOCOUNT OFF;
    INSERT INTO [web_orders] ([user_ID], [cust_ID], [orderDate], [orderTotal], [statusId], [orderReference], [custReference]) VALUES (@userId, @custId, @orderDate, @orderTotal, @statusId , 'PLC' + REPLICATE('0', (7 - LEN((select MAX(order_ID) from web_orders)))) +  CAST((select(max(order_ID)+1) from web_orders) AS VARCHAR(5)), @custReference);

    SELECT @order_ID = @@IDENTITY
    RETURN @order_ID

    SELECT @orderReferenceOutput = 'PLC' + REPLICATE('0', (7 - LEN((select MAX(order_ID) from web_orders)))) +  CAST((select(max(order_ID)+1) from web_orders) AS VARCHAR(5))
    RETURN @orderReferenceOutput 

For some reason the second output parameter @orderReferenceOutput returns nothing. The purpose of this second output parameter is to retrieve a column I have just inserted into the database.

like image 272
StevieB Avatar asked Aug 09 '11 16:08

StevieB


People also ask

Can SQL stored procedure have multiple output parameters?

A Stored Procedure can have any number of output parameters. The simple logic is this — If you want to return 1 value then use 1 output parameter, for returning 5 values use 5 output parameters, for 10 use 10, and so on.

How can we return two output parameters in stored procedure?

In order to fetch the multiple returned values from the Stored Procedure, you need to make use of a variable with data type and size same as the Output parameter and pass it as Output parameter using OUTPUT keyword. You can also make use of the Split function to split the comma separated (delimited) values into rows.

Can a stored procedure return multiple result sets?

Most stored procedures return multiple result sets. Such a stored procedure usually includes one or more select statements. The consumer needs to consider this inclusion to handle all the result sets.

Can stored procedure return multiple values in SQL Server?

Yes its possible..you can return multiple value from sproc. For that you need to declare multiple output parameter in stored procedure.


2 Answers

You have multiple output parameters, you should be using them. RETURN values are for error/status codes, not for data.

ALTER PROCEDURE [dbo].[sp_web_orders_insert]
    @userId ...,
    @order_ID INT OUTPUT,
    @orderReferenceOutput VARCHAR(50) OUTPUT
AS
BEGIN
    SET NOCOUNT OFF; -- WHY?????????

    INSERT INTO [web_orders] (user_ID, ...) SELECT @userId, ...;

    SELECT @order_ID = SCOPE_IDENTITY(); -- preferred over @@IDENTITY;

    -- using @order_ID here instead of SELECT MAX() twice:

    SELECT @orderReferenceOutput = 'PLC' 
        + REPLICATE('0', (7 - LEN((@order_ID+1))))
        + CAST((@order_ID+1) AS VARCHAR(5)) -- (5)? This breaks when you hit order #100,000

    RETURN; -- do not RETURN any data - it's already in your OUTPUT parameters!
END
GO
like image 138
Aaron Bertrand Avatar answered Nov 15 '22 21:11

Aaron Bertrand


Procedure execution ends after your first RETURN which "Exits unconditionally from a query or procedure."

RETURN @order_ID

Instead, consider returning both values as one recordset with

SELECT @order_ID AS OrderID, @orderReferenceOutput AS OrderReference

at the end of the procedure.

like image 34
Joe Stefanelli Avatar answered Nov 15 '22 19:11

Joe Stefanelli