I have a stored procedure that returns all fields of an object.
CREATE PROCEDURE getCustomer
(
@CustomerId int OUTPUT
)
AS
BEGIN
SELECT @CustomerId=CustomerId,FirstName,LastName FROM Customers
END
I want to be able to return the id of the object as an output parameter, so that another sproc can use it. I get this error in this example:
A SELECT statement that assigns a value to a variable must not be combined with
data-retrieval operations.
CREATE PROCEDURE getCustomer_and_more
AS
BEGIN
DECLARE @CustomerId int
EXEC getCustomer @CustomerId OUTPUT
-- call another sproc that requires this @CustomerId
END
When your
SELECTstatement assigns variable values, all fields in the SELECT must assign to a local variable.Change your SPROC to
You don’t have to use them variables, but they must be assigned in this fashion.
Your BETTER option is to remove the fields. If they’re not needed, don’t bother selecting them.
[EDIT] to respond to your comment
So, you could do what you’re looking to do by splitting out the assignment of the variable and selecting the other fields as follows:
This would allow you to get your output param and select all of the other data without having to define params for each field. You have to weigh ease of use (not having to define a lot of local vars) versus performance (having to run two statements as opposed to one). This seems a little strange to me however.
I wasn’t quite clear if you needed the other values from the GetCustomer sproc available to you in getCustomer_and_more. If you do, then yes, you’d have to define OUTPUT params for each value you needed