1

I must execute the query below in PHP with SQLServer, however I can not use the missing LIMIT clause 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
Igor Borisenko
  • 3,806
  • 3
  • 34
  • 49
denis
  • 140
  • 1
  • 8

2 Answers2

2

Use this code, in SQLServer the keyword Limit is 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"
rdn87
  • 739
  • 5
  • 18
  • ;) Thank u too @denis – rdn87 Jan 15 '16 at 14:31
  • If this worked for you please [accept the answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). This will help other people who encountered the same issue, as well as rewarding rdn87 for his/her effort. Could you also review these past questions [here](http://stackoverflow.com/questions/24934508/compare-integers-and-strings-php) and [here](http://stackoverflow.com/questions/24538070/run-an-audio-file-instantly-when-clicking-on-the-application-button-android). – David Rushton Jan 15 '16 at 15:00
0

You can limit the records returned by adding the TOP expression to your SELECT clause.

Usage

/* Returns the first 10 records.  
 * Combine with an ORDER BY if you want control over the records returned.
 */
SELECT TOP 10
    Id
FROM
    TableName
;

Or

-- Percent return.
SELECT TOP 10 PERCENT
    Id
FROM
    TableName
;
David Rushton
  • 4,915
  • 1
  • 17
  • 31