TCM
TCM

Reputation: 16900

Pagination sql query

Why is this query not working in Sql Server 2008?

Select Top @PageSize Skip @Page * @PageSize * From someTable

Strangely even this doesn't work :-

Select Top @PageSize * From Tree

But this one works :-

Select Top 10 * From Tree

This statement is inside Stored procedure.

Thanks in advance :)

Upvotes: 1

Views: 664

Answers (2)

SQLMenace
SQLMenace

Reputation: 134923

you need parentheses

Select Top ( @PageSize ) * From Tree

However without an order by it doesn't make a lot of sense

SKIP is not valid SQL Server syntax, the first query won't work

Upvotes: 2

Martin Smith
Martin Smith

Reputation: 452957

Your first query is not valid syntax at all.

The second one just needs brackets (and should have an ORDER BY)

Select Top (@PageSize) * 
From Tree
ORDER BY /*TODO: Add column(s)*/

To achieve what you are apparently trying to do in the first case you can use ROW_NUMBER and BETWEEN as in this answer T-SQL Skip Take Stored Procedure

Upvotes: 2

Related Questions