简体   繁体   English

选择TOP n表中的行,其中n在另一个表中?

[英]select TOP n Rows from table where n is in another table?

How can I achieve this without using Dynamic SqlQuery? 不使用动态SqlQuery如何实现?

I have this Query, 我有这个查询,

SELECT TOP n FROM mytable WHERE ID = @id

To get the value of n, 要获得n的值,

SELECT nCOUNT FROM myAnotherTable WHERE ID = @id

Can I use Row_index() for this? 我可以为此使用Row_index()吗?

Try like this....but make sure that your select ncount return single row.....if not than it will select top row ncount 尝试这样....但请确保您select ncount返回单行.....如果不然,它将选择第一行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 SqlFiddle: http ://www.sqlfiddle.com/#!3 / 75c76 /1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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