Importing data from Microsoft Access w/ SQL query

2 次查看(过去 30 天)
I have the database toolbox and have had no issues exporting data from MATLAB into Access using the ODBC connection process. However, it's taken me awhile to figure out how to import data into MATLAB from my Access database using SQL queries in MATLAB. I'm trying to understand why this code doesn't work (I don't have the connection code below but that's not the issue...MATLAB establishes the connection just fine):
A='MKT';
curs = exec(conn,['select Period,Ticker from MonthlyReturns where Ticker = ',A]);
curs = fetch(curs)
While this code does:
A='MKT';
curs = exec(conn, ['select Period,Return from MonthlyReturns where Ticker = ''', A, ''''])
curs = fetch(curs)
Just not clear to me exactly why all the additional apostrophes are needed.
Thanks,
JK

采纳的回答

Geoff Hayes
Geoff Hayes 2014-11-29
Jared - look at the two queries (as strings)
>> A = 'MKT';
>> query1 = ['select Period,Ticker from MonthlyReturns where Ticker = ',A];
>> query2 = ['select Period,Return from MonthlyReturns where Ticker = ''', A, ''''];
query1 =
select Period,Ticker from MonthlyReturns where Ticker = MKT
query2 =
select Period,Return from MonthlyReturns where Ticker = 'MKT'
Your Ticker field data type is char/string, so you need to wrap the condition for it (in your where clause) in single quotes. This is just usual SQL syntax (and is not MATLAB related).

更多回答(0 个)

类别

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