Limit in SQL query not working

3 次查看(过去 30 天)
George Mathai
George Mathai 2016-4-4
Hello,
For some reason the command
exec(conn, 'SELECT * from "CATPROD"."dbo"."machLiebherr1" LIMIT 10')
or
exec(conn,'SELECT * from "CATPROD"."dbo"."machLiebherr1" order by idx DESC LIMIT 10')
returns an error
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '10'
I have similar problems with using TOP in an SQL script.
From what I know, the syntax is correct and is also recommended in an answer to the question 'How do I efficiently make a large SQL query into Matlab?' on this forum. Could someone please let me know how to fix the error?
This command works fine, so it is definitely something to do with the 'Limit'
exec(conn, 'SELECT * from "CATPROD"."dbo"."machLiebherr1" order by idx DESC')
Thanks!

回答(1 个)

Brendan Hamm
Brendan Hamm 2016-4-4
LIMIT is not available in SQL Server. There is a similar command TOP which you can use. Your other option is to set your database preferences for fetching in batches within MATLAB.
  3 个评论
Brendan Hamm
Brendan Hamm 2016-4-6
There were plenty of other suggestions on that forum which you may want to try. Without postgres on my machine I cannot try and replicate this behavior. I do point out that I thought this was SQL Server as you received the error:
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '10'
This may indicate that you need a different driver for this connection if you wish to use these other sql queries. I could not say for sure if this is an issue.
Another option you have here is to use the Database Datastore which will allow you to query in batches and then take 10 elements in order by using the Map Reduce functionality.
Brendan Hamm
Brendan Hamm 2016-4-6
Another thing you may want to try is to use the JDBC driver. I know there are some query limitations with ODBC, so this may also solve your problem. I would actually try this first as it is by far the easiest of suggestions here.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by