MySQL Limit Equivalent in SQL Server

27

Does anyone know how to relate the differences between MySQL and SQL Server, including using LIMIT n,n ? Or if they have a link that lists the differences would be cool.

Example

SELECT * FROM tabela LIMIT 50, 100

What would be the equivalent in SQL Server?

    
asked by anonymous 26.03.2014 / 17:01

5 answers

36

If you are using SQLServer 2012 you can use OFFSET and FETCH

SELECT * FROM Tabela 
ORDER BY coluna1
OFFSET 50 ROWS FETCH NEXT 100 ROWS ONLY  

Note:
OFFSET and FETCH can only be used in conjunction with ORDER BY

See OFFSET FETCH Clause

    
26.03.2014 / 17:20
21

From the SQL Server 2005 release, you can also use ROW_NUMBER :

SELECT TOP (100) *
  FROM (
       SELECT row_number() OVER (ORDER BY coluna1 ASC) AS row_number, *
       FROM Tabela
       ) TabelaNumerada
 WHERE row_number > 50

Better yet filtering with BETWEEN :

SELECT *
  FROM (
       SELECT row_number() OVER (ORDER BY coluna1 ASC) AS row_number, *
       FROM Tabela
       ) TabelaNumerada
 WHERE row_number BETWEEN 51 AND 150
    
26.03.2014 / 18:05
14

I'll put it another way, without group by, without temporary tables, just a simple select. The query below has the same result of the "50, 100" limit, that is, it gets between record 51 and 100.

SELECT TOP 50 *
FROM tabela 
WHERE not ID in ( 
  SELECT TOP 50 ID
  FROM   tabela
  ORDER BY ID ASC
) a 
ORDER BY ID ASC;
    
26.03.2014 / 18:48
3

I did a quick and functional game:

I created a VIEW with SELECT below without conditioning Where:

SELECT row_number() OVER (ORDER BY p.idprod ASC) as paginacao, [demaiscampos] FROM [tabela]

Then I called the view using:

SELECT paginacao, [demaiscampos] FROM [tabela]  WHERE paginacao BETWEEN [reginicial] AND [regfinal]

In this way I can page the query without overloading the bank processes.

There is also the method:

DECLARE @PageNumber AS INT, @RowspPage AS INT
SET @PageNumber = 2
SET @RowspPage = 5

SELECT * FROM (
             SELECT ROW_NUMBER() OVER(ORDER BY ID_EXAMPLE) AS NUMBER,
                    ID_EXAMPLE, NM_EXAMPLE, DT_CREATE FROM TB_EXAMPLE
               ) AS TBL
WHERE NUMBER BETWEEN ((@PageNumber - 1) * @RowspPage + 1) AND (@PageNumber * @RowspPage)
ORDER BY ID_EXAMPLE

and also this:

--CREATING A PAGING WITH OFFSET and FETCH clauses IN "SQL SERVER 2012"
DECLARE @PageNumber AS INT, @RowspPage AS INT
SET @PageNumber = 2
SET @RowspPage = 10 

SELECT ID_EXAMPLE, NM_EXAMPLE, DT_CREATE
FROM TB_EXAMPLE
ORDER BY ID_EXAMPLE
OFFSET ((@PageNumber - 1) * @RowspPage) ROWS
FETCH NEXT @RowspPage ROWS ONLY;
    
27.02.2016 / 15:31
0

A MySQL-equivalent solution that does not require ORDER BY and works in any version from SQL Server 2000 is:

SELECT  TOP 100 *
FROM    Tabela 
WHERE   Coluna1 NOT IN (SELECT TOP 50 Coluna1 FROM Tabela)
  

Remembering 50 refers to the number of lines that will be   skipped / skipped and 100 the number of rows returned.

In SQL Server 2005/2008 another option is to use CTE :

WITH CTE AS
(
    SELECT  *, ROW_NUMBER() OVER (ORDER BY Coluna1) AS Linha
    FROM    Tabela 
)
SELECT  *
FROM    CTE
WHERE   Linha BETWEEN 51 AND 150

If the order and version are not a problem the command LIMIT 50, 100 of MySQL is equivalent to [OFFSET] 50 ROWS FETCH ONLY 100 ROWS ONLY of SQL Server 2012 > as already mentioned.

    
26.08.2018 / 18:10