0
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 中运行它时,它给了我以下错误,请帮帮我,我直觉这只是一个语法错误,让我很困惑,请帮帮我

消息 156,级别 15,状态 1,第 10 行
关键字“SELECT”附近的语法不正确

4

1 回答 1

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
于 2012-05-30T20:59:30.203 回答