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.
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.
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.
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.
Yes its possible..you can return multiple value from sproc. For that you need to declare multiple output parameter in stored procedure.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With