I pass the command SELECT * FROM dbo.NewsTable ORDER BY Date LIMIT 2 The error "Wrong syntax near the construction" LIMIT "." What am I writing wrong?

  • 2
    What is the database? - Mstislav Pavlov
  • @ MstislavPavlov, MS sql express - max619

2 answers 2

The LIMIT instruction is not supported by MSSQL Express, instead you need to use the analog: TOP .

Example:

 SELECT TOP 2 * FROM dbo.NewsTable ORDER BY Date 

Command syntax:

 SELECT TOP (top_value) [ PERCENT ] expressions FROM tables [WHERE conditions] [ORDER BY expression [ ASC | DESC ]]; 

Here you can find even more ways to select N rows.

  • it works this way, but it takes the values ​​first and sorts them by date, rather than sorts by date, and takes the first two values - max619

If you want to sort first, and then take the first 2 values, you can use a subquery to sort. For example:

 SELECT TOP 2 * FROM (SELECT * FROM dbo.NewsTable ORDER BY Date)