I have this query from mysql
SELECT *
FROM table
LIMIT 100, 200
Now how do I write similar query in SQL Server 2008. Basically I'm using php and SQL Server as backend and need to display records in range (1-100 then 100-200)
I have this query from mysql
SELECT *
FROM table
LIMIT 100, 200
Now how do I write similar query in SQL Server 2008. Basically I'm using php and SQL Server as backend and need to display records in range (1-100 then 100-200)
Try this:
SELECT *
FROM
(SELECT
SomeColumn, ...,
ROW_NUMBER() OVER (ORDER BY SomeColumn) AS RowNumber
FROM
table) Aux
WHERE
RowNumber >= @start AND RowNumber < (@start + @length)
Notes: You need mandatory order by a column and also you have to make a subquery to get access to RowNumber
in the WHERE
clause
You can try this approach:
SELECT ID, ItemID, ItemName, UnitCost, UnitPrice
FROM
(
SELECT ID, ItemID, ItemName, UnitCost, UnitPrice,
ROW_NUMBER() OVER (ORDER BY UnitCost) AS Seq
FROM dbo.Inventory
)t
WHERE Seq BETWEEN 100 AND 200
you will basically use row_number to fetch they information that you need.