DECLARE @startRowIndex int
set @startRowIndex=0
Declare @maximumRows int
set @maximumRows=5
SET ROWCOUNT 5;
WITH OrderedEmployees As
SELECT *, ROW_NUMBER() OVER (Order By EmployeeID ASC) as RowNum FROM Employees
SELECT * FROM OrderedEmployees Where RowNum > @startRowIndex
Order By EmployeeID ASC
我从一个网站上得到了这个查询,但是当我在SQL Server Management Studio中运行它时,它给了我以下错误请帮帮我,我有直觉说它只是一个语法错误让我难过,请帮助我出
Msg 156,Level 15,State 1,Line 10
附近的语法不正确
关键字'SELECT'
答案 0 :(得分:3)
我相信这就是你想要的:
DECLARE @startRowIndex int
set @startRowIndex=0
Declare @maximumRows int
set @maximumRows=5
SET ROWCOUNT 5
;WITH OrderedEmployees As
(
SELECT *, ROW_NUMBER() OVER (Order By EmployeeID ASC) as RowNum
FROM Employees
)
SELECT *
FROM OrderedEmployees
Where RowNum > @startRowIndex
Order By EmployeeID ASC
尽管如此,我真的不知道为什么你选择使用ROWCOUNT
而不仅仅是过滤你的CTE。我会用:
DECLARE @startRowIndex int
set @startRowIndex=0
Declare @maximumRows int
set @maximumRows=5
;WITH OrderedEmployees As
(
SELECT *, ROW_NUMBER() OVER (Order By EmployeeID ASC) as RowNum
FROM Employees
)
SELECT *
FROM OrderedEmployees
Where RowNum > @startRowIndex AND RowNum <= @maximumRows
Order By EmployeeID ASC