Generate SQL Query andMATLABScript
You can generate SQL code from an SQL query or create a MATLAB®script by using theDatabase Explorerapp. After defining an SQL query in theSQL Querypane, you can generate the SQL code for running an SQL script. You can also generate a MATLAB script to automate connecting to a database, running an SQL query, and performing data analysis on the imported data.
Generate SQL Query
To generate SQL code from an SQL query, follow these steps:
Connect to a data source and create an SQL query. For details about creating SQL queries, seeCreate SQL Queries Using Database Explorer App.
In theImportsection, selectImport Data>Generate SQL Query.
Save the SQL code to a
.txt
or.sql
file. The MATLAB Editor opens the saved SQL code file.You can use the SQL code to rebuild a query by using theSQL Querypane by entering the SQL code manually.
Alternatively, you can use the
.sql
file to import data programmatically into MATLAB by using theexecuteSQLScript
function.
GenerateMATLABScript
To generate a MATLAB script that automates connecting to a data source, running an SQL query, and importing the SQL query results, follow these steps:
Connect to a data source and create an SQL query. For details about creating SQL queries, seeCreate SQL Queries Using Database Explorer App.
In theImportsection, selectImport Data>Generate MATLAB Scriptto display a MATLAB script in the MATLAB Editor. To customize import options before importing data, seeCustomize Import Options Using Database Explorer App.
Save the MATLAB script. You can run this script at the command line.
Note
To run the script, enter the database password as an input argument of the
database
function in the script.
For details about editing and running scripts, seeScripts.