Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 0.97 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. MySQL LIMIT clause equivalent for SQL SERVER
  2. "SELECT ID, Name, Price, Image FROM Products ORDER BY ID ASC LIMIT $start_from, $items_on_page"
  3.        
  4. SELECT ID, Name, Price, Image
  5.   FROM Products
  6.   ORDER BY ID ASC
  7.   OFFSET (@start_from - 1) ROWS -- not sure if you need -1
  8.     -- because I don't know how you calculated @start_from
  9.   FETCH NEXT @items_on_page ROWS ONLY;
  10.        
  11. ;WITH o AS
  12. (
  13.     SELECT TOP ((@start_from - 1) + @items_on_page)
  14.          -- again, not sure if you need -1 because I
  15.          -- don't know how you calculated @start_from
  16.       RowNum = ROW_NUMBER() OVER (ORDER BY ID ASC)
  17.       /* , other columns */
  18.     FROM Products
  19. )
  20. SELECT
  21.     RowNum
  22.     /* , other columns */
  23. FROM
  24.     o
  25. WHERE
  26.     RowNum >= @start_from
  27. ORDER BY
  28.     RowNum;
  29.        
  30. SELECT TOP $items_on_page ID, Name, Price, Image
  31. FROM (SELECT TOP $start_from + $items_on_page - 1 * FROM Products ORDER BY ID) as T
  32. ORDER BY ID DESC
  33.        
  34. SET ROWCOUNT 100
  35.        
  36. SELECT TOP 100 * FROM Sometable ORDER BY somecolumn