
Untitled
By: a guest on
May 4th, 2012 | syntax:
None | size: 0.97 KB | hits: 19 | expires: Never
MySQL LIMIT clause equivalent for SQL SERVER
"SELECT ID, Name, Price, Image FROM Products ORDER BY ID ASC LIMIT $start_from, $items_on_page"
SELECT ID, Name, Price, Image
FROM Products
ORDER BY ID ASC
OFFSET (@start_from - 1) ROWS -- not sure if you need -1
-- because I don't know how you calculated @start_from
FETCH NEXT @items_on_page ROWS ONLY;
;WITH o AS
(
SELECT TOP ((@start_from - 1) + @items_on_page)
-- again, not sure if you need -1 because I
-- don't know how you calculated @start_from
RowNum = ROW_NUMBER() OVER (ORDER BY ID ASC)
/* , other columns */
FROM Products
)
SELECT
RowNum
/* , other columns */
FROM
o
WHERE
RowNum >= @start_from
ORDER BY
RowNum;
SELECT TOP $items_on_page ID, Name, Price, Image
FROM (SELECT TOP $start_from + $items_on_page - 1 * FROM Products ORDER BY ID) as T
ORDER BY ID DESC
SET ROWCOUNT 100
SELECT TOP 100 * FROM Sometable ORDER BY somecolumn