Incorrect syntax near "LIMIT" using mssql

I am trying to extract some data from a database, which should be the top 10 agents with the highest result.

My request:

SELECT AgentScores.agentID, AgentScores.totalScore, Agents.firstname, Agents.lastname FROM AgentScores INNER JOIN Agents ON AgentScores.AgentId=Agents.Agent_id ORDER BY AgentScores.totalScore DESC LIMIT 10 

Internal connections work. I found a SELECT TOP 10 sql SELECT TOP 10 , but .. I want 10 agents with the highest result, not the first 10 id. As you can see, I order on a total sign.

Does anyone know how to fix this?

Error: Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'LIMIT'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'LIMIT'. ) )

Thanks!

+4
source share
2 answers

You should use TOP clause instead of LIMIT

 SELECT TOP 10 AgentScores.agentID, AgentScores.totalScore, Agents.firstname, Agents.lastname FROM AgentScores INNER JOIN Agents ON AgentScores.AgentId=Agents.Agent_id ORDER BY AgentScores.totalScore DESC 
+7
source

To limit rows in MSSQL, you should use SELECT TOP 10 .... instead of LIMIT 10 (limit is a MySQL SELECT TOP 10 , not MSSQL)

+5
source

All Articles