How do I convert a MySQL query with LIMIT to a SQL Server query?
SELECT *
FROM tableEating
WHERE person = '$identity'
LIMIT 1;
How do I convert a MySQL query with LIMIT to a SQL Server query?
SELECT *
FROM tableEating
WHERE person = '$identity'
LIMIT 1;
LIMIT does not work in T-SQL. Use TOP instead:
SELECT TOP(1) * FROM tableEating WHERE person = '$identity';
As Aaron says, you also need an ORDER BY
if you don't want to get an arbitrary row.
LIMIT
does not work and TOP(1)
may also not work in nested statements.
So the right approach is to use OFFSET... FETCH NEXT
:
SELECT * FROM TableName
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
That basically tells TSQL to take one row (NEXT 1 ROWS ONLY
) starting from the first one (OFFSET 0
).
Take a look at the Microsoft page for 2008, later versions work the same way. If you like to do it dynamic use:
DECLARE @xCount INT = 20
SELECT TOP(@xCount) * FROM ...