从表中选择TOP N行,其中n是在另一个表?(select TOP n Rows from tabl

2019-10-29 14:09发布

我怎样才能做到这一点不使用动态类SqlQuery?

我有这个疑问,

SELECT TOP n FROM mytable WHERE ID = @id

为了得到n的值,

SELECT nCOUNT FROM myAnotherTable WHERE ID = @id

我可以使用ROW_INDEX()这个?

Answer 1:

尝试这样的....但要确保你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



文章来源: select TOP n Rows from table where n is in another table?