[英]select data from #Temp table after #temp table create in another query in asp.net c#
[英]Batching data from a temp table
我创建一个临时表,插入数据(并输出)并选择。 我需要批处理它们并阅读。
示例:如果我有110条记录,并且我想每次读取50条记录,那么我将第一次读取1-50,第二次读取51-100,第三次读取100-110。
查询如下
create table #mytable
{
customerID int not null
}
insert into #mytable(id)
Output inserted.CustomerID
select top 50 customerID from customer where customerID not in (select c.customerID from #mytable c)
在C#中,我的代码是这样的
do {
getCustomerInformation() //this method does the query processing
} while(customerInfo.Any());
当我在SQL Server上但不在C#中运行此查询时,此查询有效。 一直返回前50行。
简单使用此查询,
1)第一次@PageNumber参数为1,第二次为2,第三次发送3
DECLARE @PageNumber AS INT, @RowspPage AS INT
SET @PageNumber = 1 -- 2 , 3
SET @RowspPage = 50
SELECT customerID FROM (
SELECT ROW_NUMBER() OVER(ORDER BY id) AS Numero,
customerID FROM customer
) AS TBL
WHERE Numero BETWEEN ((@PageNumber - 1) * @RowspPage + 1) AND (@PageNumber * @RowspPage)
ORDER BY customerID
在SSMS中,您拥有临时表,并通过检查临时表来检查批次的ID。
临时表通常仅在会话中存在。 因此,如果在C#中每次调用该方法都初始化一个新会话,则该会话将看不到前一个会话的临时表。
您可以使用## two散列来解决此问题,或者仅创建实际的登台表。
您可以使用LINQ提供的Take()
和Skip()
方法在C#中实现分页:
const int batchSize = 50;
do {
var currentBatch = customerInfo.Take(batchSize);
customerInfo = customerInfo.Skip(batchSize);
} while(customerInfo.Any());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.