SQL Server:TOP子句有替代品嗎? [重復]

[英]SQL Server : is there any alternate for TOP clause? [duplicate]


Possible Duplicate:
Equivalent of LIMIT and OFFSET for SQL Server?

可能重復:SQL Server的LIMIT和OFFSET的等價物?

I am working on a table which currently contains 2500 rows, and will be much more and more in future.

我正在研究一個目前包含2500行的表,將來會越來越多。

I am using jqgrid to display the records from the database .

我正在使用jqgrid來顯示數據庫中的記錄。

Whenever the grid is loading, I am retrieving the top 500 rows from the database. However when I am started using pagination in the grid, the number of records is adding.

每當網格加載時,我都會從數據庫中檢索前500行。但是當我開始在網格中使用分頁時,記錄的數量正在增加。

Example : first the number of rows is TOP 100, then going onto the next page its increased to TOP 200 and so on in the query.

示例:首先行數為TOP 100,然后進入下一頁,其增加到TOP 200,依此類推。

Now I want a solution to avoid this issue such that I can limit the rows instead of retrieving top n rows based on the grid. Say using a range or LIMIT

現在我想要一個解決方案來避免這個問題,這樣我就可以限制行而不是根據網格檢索前n行。假設使用范圍或LIMIT

Example

When I am navigating to next page in grid, instead of TOP 200 rows I want to fetch rows from 101 to 200

當我在網格中導航到下一頁時,我想要從101到200獲取行,而不是前200行

 select top 500
        exe.id,
        bat.BName,
        bat.tid,
        bat.freq,
        exe.status,
        exe.Msg,
        exe.time,
        exe.Fi
    from
       XXX exe,
        YYY bat 
    where
        exe.id=bat.id   
    order by     
        CONVERT(VARCHAR(10),
        exe.time,
        120) DESC,
        exe.status,
        exe.id DESC

Please guide me in this, as I am new to jQGrid and not much aware how to handle this DB related stuff

請指導我,因為我是jQGrid的新手並且不太了解如何處理這個與DB相關的東西

I also referred few links in Stackoverflow but can't understand

我還在Stackoverflow中引用了一些鏈接但無法理解

Thanks in advance

提前致謝

1 个解决方案

#1


5  

You can use ROW_NUMBER(), the row_number() is not assigned until you query the data so if you want to retrieve the data by the row_number() value, then you will need to use CTE or a sub-select to get the value for use in a WHERE clause.

你可以使用ROW_NUMBER(),在查詢數據之前不會分配row_number(),所以如果你想通過row_number()值檢索數據,那么你需要使用CTE或子選擇來獲取值用於WHERE子句。

SELECT *
FROM
(
  select  exe.id,
        bat.BName,
        bat.tid,
        bat.freq,
        exe.status,
        exe.Msg,
        exe.time,
        exe.Fi,
        row_number() over(order by CONVERT(VARCHAR(10), exe.time, 120) DESC,
                   exe.status, exe.id DESC) rn
  from XXX exe,
       YYY bat 
  where exe.id=bat.id  
) x
WHERE rn between 101 and 200

I would also suggest a change in your JOIN syntax to use ANSI syntax:

我還建議您更改JOIN語法以使用ANSI語法:

SELECT *
FROM
(
  select  exe.id,
        bat.BName,
        bat.tid,
        bat.freq,
        exe.status,
        exe.Msg,
        exe.time,
        exe.File,
        row_number() over(order by CONVERT(VARCHAR(10), exe.time, 120) DESC,
                   exe.status, exe.id DESC) rn
  from XXXexe
  INNER JOIN  YYY bat 
     ON exe.id=bat.id  
) x
WHERE rn between 101 and 200

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/08/23/31c4002ed59552f40767bac477ebd585.html



 
  © 2014-2022 ITdaan.com