3

i know there is no limit x,y in sqlserver instead of it i use:

 select  ROW_NUMBER(),name OVER (ORDER BY name) AS
 myrow from pack  where myrow > 5 and myrow < 10

but it has following error:

Msg 207, Level 16, State 1, Line 1
Invalid column name 'myrow'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'myrow'.

any idea?

Edit

i saw What is the Equivalent syntax of mysql " LIMIT " clause in SQL Server but it didn't solve my problem.

5
  • possible duplicate of What is the Equivalent syntax of mysql " LIMIT " clause in SQL Server Commented May 27, 2013 at 13:47
  • Did you try pasting your question title into Google? Commented May 27, 2013 at 13:48
  • @SLaks: that dupe is a bad answer to this question and generally Commented May 27, 2013 at 13:50
  • i see that page but have same error. Commented May 27, 2013 at 13:51
  • @Mahdi_Nine: you were getting there, just need to tweak syntax Commented May 27, 2013 at 13:52

2 Answers 2

1

In SQL Server 2012 there it the OFFSET FETCH syntax

However, for older versions you have to use ROW_NUMBER but in a derived table

select
    name
from
    (
    select
       name,
       ROW_NUMBER() OVER (ORDER BY name) AS myrow
    from
       pack
    ) X
where
   myrow > 5 and myrow <= 10

Don't use 3 nested TOPs as per the suggested answer in the proposed duplicate

Sign up to request clarification or add additional context in comments.

Comments

0

Try this one (for 2005 and higher) -

SELECT p.name
FROM ( 
    SELECT  
          name
        , myrow = ROW_NUMBER() OVER (ORDER BY name)  
    FROM dbo.pack
) p
WHERE myrow BETWEEN 5 AND 9

Or try this (for 2012) -

SELECT name
FROM dbo.pack
ORDER BY name 
OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY

1 Comment

@Mahdi_Nine, second query only for MS SQL 2012

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.