Limit in SQL query not working

조회 수: 7 (최근 30일)
George Mathai
George Mathai 2016년 4월 4일
댓글: Brendan Hamm 2016년 4월 6일
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