简体   繁体   English

获取 SQL 导致零件

[英]Getting SQL results in parts

I know i can limit the count of received results using the TOP keyword, but is there a way to receive the next let say 1000 results using something like <give me the next 1000 results> which gives me each time the next cached 1000 ones?我知道我可以使用TOP关键字来限制接收到的结果的计数,但是有没有办法使用类似<give me the next 1000 results>这样的东西来接收下一个让说 1000 个结果,每次下一个缓存的 1000 个结果给我?

So assume my query has 100000 and the first run i get 1-1000 i would like to receive the 1000-2000 and so on.所以假设我的查询有 100000 并且第一次运行我得到 1-1000 我想收到 1000-2000 等等。

When the data is changing between queries, rownum/between solutions will give you approximations of the next chunk of data.当数据在查询之间发生变化时,rownum/between 解决方案将为您提供下一个数据块的近似值。

If you need to get a specific chunk of data from a fixed result, insert all of the results into a table and then consume the data as needed.如果您需要从固定结果中获取特定数据块,请将所有结果插入表中,然后根据需要使用数据。 The results will stay fixed until you refresh the data in the table again.结果将保持不变,直到您再次刷新表中的数据。

This works well when you must get the exact next set (forward or backward).当您必须获得准确的下一组(向前或向后)时,这很有效。 This might be useful, depending on your situation.这可能很有用,具体取决于您的情况。

You could use ROW_NUMBER()你可以使用ROW_NUMBER()

SELECT 
    col1, 
    col2 
FROM 
(
     SELECT 
         col1, 
         col2, 
         ROW_NUMBER() OVER (ORDER BY ID) AS RowNum
     FROM 
        MyTable
) AS MyDerivedTable
WHERE 
    MyDerivedTable.RowNum BETWEEN @startRow AND @endRow

Use the ROW_NUMBER function to order your data as desired in a subquery or CTE .使用ROW_NUMBER function 在子查询或CTE中根据需要对数据进行排序。 Then select from that subquery/CTE with the desired values (eg, WHERE RowNum > 1000 AND RowNum <= 2000 ).然后来自该子查询/CTE 的 select 具有所需的值(例如, WHERE RowNum > 1000 AND RowNum <= 2000 )。

Suppose there is following table假设有下表

create table #temp
(
    ID int Identity(1,1) ,
    name varchar(1000)
)

Suppose there are following records in the table假设表中有以下记录

select * from #temp

总记录

We starts with declaring following variables我们从声明以下变量开始

Declare @PageIndex INT //indicates the start index that means your page number Declare @PageIndex INT //表示起始索引,表示你的页码

Declare @PageSize INT //the total amount of records you want to display in a page Declare @PageSize INT //要在一个页面中显示的记录总数

Set @PageIndex = 1
Set @PageSize = 10

I am setting the page size = 10 because I have only 15 records in my table.我正在设置页面大小 = 10,因为我的表中只有 15 条记录。 But you can replace this number by 1000 or even greater than this number.但是您可以将此数字替换为 1000 甚至大于此数字。

SELECT * FROM 
(
        SELECT ROW_NUMBER() OVER (ORDER BY ID)
        AS Row, * FROM #temp
)T WHERE Row between 
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize

If you execute the below query it will give you following records in the screen shot.如果您执行以下查询,它将在屏幕截图中为您提供以下记录。

查询结果

While moving to second page using below query.使用以下查询移至第二页时。

Declare @PageIndex INT
Declare @PageSize INT

set @PageIndex = 2
set @PageSize = 10

SELECT * FROM 
(
   SELECT ROW_NUMBER() OVER (ORDER BY ID)
   AS Row, * FROM #temp
)T WHERE Row between 
(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize

The result will be below in the screen shot.结果将在屏幕截图下方。

在此处输入图像描述

So similarly you can check with 1000 records.所以同样你可以检查 1000 条记录。

Hope this will help you.希望这会帮助你。

Let me know in case of any confusion.如果有任何混淆,请告诉我。

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

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