Why the Output parameter did not work?

Siver 100 Reputation points

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:

@GroupNum int,
@max_value int Output
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.
9,819 questions
0 comments No comments
{count} votes

Accepted answer
  1. CosmogHong-MSFT 11,881 Reputation points Microsoft Vendor

    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