Why the Output parameter did not work?

Riley 380 Reputation points
2023-05-19T01:43:42.0966667+00:00

Suppose I have table with columns: ID, Group Number, Value. Want the SP code output the Max value of specific Group Number and then use this max value in further query.

Tried this:

CREATE PROC SP_TestOutput
@GroupNum int,
@max_value int Output
AS
Select @max_value = MAX(val)
From mytable
Where GroupNum = @GroupNum

EXEC SP_TestOutput  @GroupNum=1001

Select ID
From mytable Where Col2 = @max_value

Please help, thanks in advance.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,654 questions
0 comments No comments
{count} votes

Accepted answer
  1. CosmogHong-MSFT 22,621 Reputation points Microsoft Vendor
    2023-05-19T01:52:47.76+00:00

    Hi @Siver

    You need another parameter to receive the output value in the Execute clause.

    Try this:

    EXEC SP_TestOutput  @GroupNum=1001 , @max_value=@max_value_final OUTPUT
    Select ID
    From mytable Where Col2 = @max_value_final
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful