2

I am Working on pagination in jsp(and i am new to writing sql).

I done my research and found simple queries from

pagination in SQL server 2008 and How to do pagination in SQL Server 2008

SELECT * FROM document  ORDER BY id OFFSET
                    "+iPageNo+"  ROWS FETCH NEXT 10 ROWS ONLY;

in the above query ipageNo is coming 0 for the first time(even i tried by putting 1 manually)

Still giving the error Incorrect syntax near 'OFFSET'.

What i missed here ? Thanks in advance.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307

4 Answers4

5

You will note from ORDER BY Clause (Transact-SQL) this syntax is not supported in SQL Server 2008.

You can see from the 2008 documentation

**Syntax**

[ ORDER BY 
    {
    order_by_expression 
  [ COLLATE collation_name ] 
  [ ASC | DESC ] 
    } [ ,...n ] 
]

where as the 2012 documentation

**Syntax**
ORDER BY order_by_expression
    [ COLLATE collation_name ] 
    [ ASC | DESC ] 
    [ ,...n ] 
[ <offset_fetch> ]


<offset_fetch> ::=
{ 
    OFFSET { integer_constant | offset_row_count_expression } { ROW | ROWS }
    [
      FETCH { FIRST | NEXT } {integer_constant | fetch_row_count_expression } { ROW | ROWS } ONLY
    ]
}

Maybe also have a look at how to do pagination in sql server 2008

Community
  • 1
  • 1
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
3

Here is my work around and working fine now.

SELECT * FROM   (SELECT ROW_NUMBER() OVER(ORDER BY id) AS rownumber,*
        FROM document)  as somex  WHERE  rownumber >= (1+1)*10-9
                         AND rownumber <=(1+1)*10

In the above query i am replacing (1+1) with (pageNUmber+1).

enter image description here

Please feel free to suggest me if any elegant way available.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Hi how can we add order by to the query? I tried. It gave me an error saying "The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified." Is it possible to have aggregate functions like "Group By and Order By" ? – yohan.jayarathna Feb 24 '14 at 14:35
1

I hope this will help (works on SQL Server 2008).

msi77
  • 1,602
  • 1
  • 11
  • 10
0
DECLARE @Page int
SET @Page = 2
DECLARE @Amount int
SET @Amount = 25
SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY group_id) AS rownumber, * FROM table_name 
WHERE Column LIKE '%Search_Value%') as somex  
WHERE  rownumber >= (@Page+1)* @Amount-(@Amount - 1) AND rownumber <= (@Page+1) * @Amount

I took it one step further. Added some variables to make inputting the information a little better. At my company we have older databases so this feed helped me a lot.