Clause similar to MySQL's LIMIT in MSSQL

5

I need to run the query below in PHP with SQL Server, however I can not use the LIMIT clause missing in Microsoft queries .

$SqlTabelaAtual="SELECT * 
         FROM BusinessCadTabPreco
         RIGHT JOIN BusinessCadTabPrecoItem ON BusinessCadTabPreco.CdTabela = BusinessCadTabPrecoItem.CdTabela
         WHERE  BusinessCadTabPreco.CdEmpresa =01
         AND CdProduto =".$row['CdProduto']."
         ORDER BY  BusinessCadTabPreco.DtSincronizar DESC LIMIT 1
    
asked by anonymous 15.01.2016 / 15:17

2 answers

5

SQL Server uses the syntax TOP :

$SqlTabelaAtual="SELECT TOP 1 * 
         FROM BusinessCadTabPreco
         RIGHT JOIN BusinessCadTabPrecoItem ON BusinessCadTabPreco.CdTabela = BusinessCadTabPrecoItem.CdTabela
         WHERE  BusinessCadTabPreco.CdEmpresa =01
         AND CdProduto =".$row['CdProduto']."
         ORDER BY  BusinessCadTabPreco.DtSincronizar DESC

The 2012 and later versions allow ANSI syntax with OFFSET FETCH .

    
15.01.2016 / 15:19
4

Depending on the case TOP is suitable for limiting rows of a query, in more recent versions there are other more flexible clauses.

SELECT TOP 10 * FROM tabela.

Related:

Result Paging in SQL Server 2000

    
15.01.2016 / 15:21