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

I got this query from a website but when I run it in SQL Server Management Studio, it gives me the following error please help me out, I have intuition that its just a syntax mistake that is stumping me here , please help me out

Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'SELECT'

0

1 Answer 1

3

I believe that this is what you want:

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

Though, I don't really know why you are choosing to use ROWCOUNT instead of just filtering your CTE. I would use:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.