SQL查询中的语法错误不正确

时间:2012-05-30 20:57:30

标签: sql sql-server

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'

附近的语法不正确

1 个答案:

答案 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