User`s guide
Creating and Running SQL Queries
1-9
Creating and Running SQL Queries
You can select data from your database and import it into MATLAB by doing any of the
following:
• Use Database Explorer or the command line.
• Write queries using SQL.
• Use MATLAB to generate the SQL.
Then, if you want to repeat your tasks, then automate them by generating a MATLAB
script.
Writing a query requires knowledge of SQL and experience using the command line. Use
the exec function to write SQL if you have short or simple SQL queries that are easy
to write as a string. Also, use the exec function to add MATLAB variables to your SQL
query string. If you have a long SQL query or multiple SQL queries that you want to
run sequentially, then create an SQL script file containing your SQL queries and use the
runsqlscript function.
If you are unfamiliar with writing SQL code, then you can use Database Explorer to
create SQL queries. For details, see “Define Query Criteria to Refine Results”. After
creating the query using Database Explorer, you can generate the SQL for this query.
For details, see “Save Queries as SQL Code”. You can embed the generated SQL into
the exec function SQL string. Or, you can create an SQL script file to use with the
runsqlscript function.
If you want to automate the current task after the SQL is created, then generate a
MATLAB script. For details, see “Generate MATLAB Code”.