How to run large SQL statements on new lines

조회 수: 4 (최근 30일)
Dan Howard
Dan Howard 2018년 10월 25일
답변: Dan Howard 2018년 10월 26일
Good afternoon all...
Little questions regarding SQL statements in Matlab. I am currently using 2018 version and have produced a script that connects to a database and extracts data using a simple SQL statement.
I am trying to understand how you can tidy up the script by using larger SQL queries to extract more data by using a new line e.g ...
if true
% %Open the connection string- connect to Database
conn.Open(connection_string1);
SQL = 'select table.leg_no', ...
'from table');
end
This will only work if I place the whole statement on one line. I have tried [] and ().
Eventually I would like to create larger SQL queries that can be stored into variables to use in a GUI.
Really appreciate some help with this..Many thanks :-)
Please note: I am not running with the DataBase Toolbox.

채택된 답변

Guillaume
Guillaume 2018년 10월 25일
편집: Guillaume 2018년 10월 25일
Your question is really about how to input a char vector using more than one line:
SQL = ['select table.leg_no ', ...
'from table ', ...
'where table.col1 = 12345'];
See also this
edit: don't forget the spaces as I did originally, matlab won't insert them between each line for you.

추가 답변 (2개)

Dan Howard
Dan Howard 2018년 10월 26일
Many thanks for clearing that up for me. The only problem I seem to be having is that..
1.
if true
% SQL = [['select table.leg_no ', ...
'from table '];
end
This executes the SQl query ok
2.
if true
% SQL = ['select table.leg_no ', ...
'from table ', ...
'where table.col1 = 12345'];
end
This throws an Error which is...
Source: Microsoft OLE DB Provider for SQL Server Description: Invalid column name '12345'.
Error in Database (line 29) myrecordset=conn.Execute(SQL); % execute sql statement
Little confused with this. Does that mean the variable (SQL) wont execute a char vector?

Dan Howard
Dan Howard 2018년 10월 26일
Managed to find the solution.
For the line...
if true
% where table_no = ''1234'''
end
Forgot you have to use double quotes.

카테고리

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