简体   繁体   中英

select TOP n Rows from table where n is in another table?

How can I achieve this without using Dynamic SqlQuery?

I have this Query,

SELECT TOP n FROM mytable WHERE ID = @id

To get the value of n,

SELECT nCOUNT FROM myAnotherTable WHERE ID = @id

Can I use Row_index() for this?

Try like this....but make sure that your select ncount return single row.....if not than it will select top row ncount

SELECT TOP(SELECT TOP 1 nCOUNT FROM myAnotherTable WHERE ID = @id) * FROM mytable WHERE ID = @id

SqlFiddle: http://www.sqlfiddle.com/#!3/75c76/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM