How to write (MySQL) "LIMIT" in SQL Server?

I have a problem trying to change a query from LIMIT from MYSQL to SQL-Server.

Make sure that:

SELECT * FROM tableEating WHERE person = '$identity' LIMIT 1; 

I tried changing it with some queries, but nothing worked.

+8
mysql sql-server
source share
2 answers

LIMIT does not work in T-SQL.

Instead, you should use TOP:

 SELECT TOP(1) * FROM tableEating WHERE person='$identity'; 

I hope this works for you.

As Aaron says, you also need ORDER BY if you don't want to receive an arbitrary string.

+10
source share

LIMIT does not work, and TOP(1) also cannot work in nested statements.

So the correct approach is to use OFFSET... FETCH NEXT :

 SELECT * FROM TableName OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY 

This basically tells TSQL to take one row ( NEXT 1 ROWS ONLY ), starting from the first ( OFFSET 0 ).

0
source share

All Articles