1

Is it possible to use a top distinction with an output parameter in SQL?
I am trying to use the below code and getting a syntax error:

@returnParam int output = 0

Select @returnParam = top 1 Id 
From table
Where xyz

Whereas taking out the top 1 the query has no errors. I could probably nest the query to get the top 1 from an inner query, but I don't think that is ideal.

Thanks!

2 Answers 2

3

Try it like this:

@returnParam int output = 0

Select top (1) 
    @returnParam =  Id 
From table
Where xyz

Put the TOP (1) first - before the assignment to the output parameter.

You can find this (and much more) in the official MSDN documentation (the "SQL Server Books Online" - freely available to anyone!)

Sign up to request clarification or add additional context in comments.

1 Comment

thank you this solved it! also, honorable mention to Rahul who cam in only 24 seconds after your answer
0

There is a bit syntax issue with your query. It should be

Select top 1 @returnParam =  Id 
From table
Where xyz;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.