由于前两个评论我已经删除了所有我自己的代码,并直接从4个人放在这里的例子。高效的sql服务器分页
我对'select @first_id'应该如何编码感兴趣。这个例子显示了使用连接拉动的行,我期望first_id不是一个有效的开始位置,因为它不使用相同的连接语法。
CREATE PROCEDURE [dbo].[usp_PageResults_NAI]
(
@startRowIndex int,
@maximumRows int
)
AS
DECLARE @first_id int, @startRow int
-- A check can be added to make sure @startRowIndex isn't > count(1)
-- from employees before doing any actual work unless it is guaranteed
-- the caller won't do that
-- Get the first employeeID for our page of records
SET ROWCOUNT @startRowIndex
SELECT @first_id = employeeID FROM employees ORDER BY employeeid
-- Now, set the row count to MaximumRows and get
-- all records >= @first_id
SET ROWCOUNT @maximumRows
SELECT e.*, d.name as DepartmentName
FROM employees e
INNER JOIN Departments D ON
e.DepartmentID = d.DepartmentID
WHERE employeeid >= @first_id
ORDER BY e.EmployeeID
SET ROWCOUNT 0
GO
高效分页纵观复杂我。 –
'请在没有上下文的情况下调试我的代码' - 不用了,谢谢。 – JNK
这个例子看起来不错,因为一个Employee只在一个部门中,所以连接不会改变行数。尽管如此,这是一种SQL Server 2000方法。 'row_number'在2005+更容易 –