问题描述
我有一个像 (OrderID [uniqueidentifier], OrderDeciption [nvarchar]) 这样的表结构,我使用的是 ADO.Net + C# + VSTS 2008 + SQL Server 2008.表很大,我想让客户给我两个输入,开始范围索引和结束范围索引,我将返回范围内(开始范围索引和结束范围索引之间)的表的特定行.
I have a table structure like (OrderID [uniqueidentifier], OrderDesciption [nvarchar]), I am using ADO.Net + C# + VSTS 2008 + SQL Server 2008. The table is big, and I want to let client give me two inputs, begin range index and end range index, and I will return specific rows of the table which is in the range (between begin range index and end range index).
比如客户端给我输入50、100,我想返回第50行直到第100行.
For example, if the client inputs to me 50, 100, and I want to return the 50th row until the 100th row.
提前致谢,乔治
推荐答案
您可以在 TSQL(2005 年以后)中使用 ROW_NUMBER
来执行此操作:
You can use ROW_NUMBER
in TSQL (2005 onwards) to do this:
SELECT ID, Foo, Bar
FROM (SELECT ROW_NUMBER() OVER (ORDER BY ID ASC) AS Row,
ID, Foo, Bar
FROM SomeTable) tmp
WHERE Row >= 50 AND Row <= 100
或使用 LINQ-to-SQL 等:
Or with LINQ-to-SQL etc:
var qry = ctx.Table.Skip(50).Take(50); // or similar
这篇关于检索 SQL Server 表中特定范围的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!