HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Select TOP @N where @N is a variable from another Query

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
topwherequeryanotherselectfromvariable

Problem

I am building a report on server hardware projections, and I'm getting stuck here.

I have a query that correctly returns the number of HDD's installed on the server, and am storing that number into a variable called @OtherHDDs, and it is an INT.

I need to now do this...

Select top @OtherHDDs.... 
from ...


How do I do this correctly?

Solution

You need to enclose the variable in parenthesis:

select top (@OtherHDDs) -- your column list here
from YourTable


MSDN Reference on SELECT

Code Snippets

select top (@OtherHDDs) -- your column list here
from YourTable

Context

StackExchange Database Administrators Q#21838, answer score: 12

Revisions (0)

No revisions yet.