This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Create SQL Queries Using Database Explorer App

Using the Database Explorer app, you can open one or multiple database connections simultaneously by clicking New Query in the toolstrip. The Database Explorer app creates a data source tab for each connection.

On each data source tab, you can write an SQL query in one of two ways. If you are unfamiliar with the SQL query language or want to explore data in your database, then use the Data Browser pane along with the buttons in the toolstrip. Or, if you are already familiar with SQL, then enter an SQL query manually. When you enter a query, you can use more advanced SQL statements (for example, AS, GROUP BY, HAVING). You can also enter SQL code that is proprietary to the database and does not comply with the ANSI standard.

Create SQL Query Using Toolstrip Buttons

Use these steps as a general workflow for creating an SQL query by using buttons in the toolstrip.

  • Connect to a data source in the Database Explorer app. For an example, see MySQL ODBC for Windows.

  • Click a table in the Data Browser pane. The SQL Query pane in the data source tab updates with an SQL query that selects all columns and rows of the table. The Data Preview pane updates with a preview of the first 10 rows of data in the table.

  • In the Join section, click Join to display the Join tab in the toolstrip. In the Add section, the name of the table selected in the Data Browser pane appears in the left Table list. From the left Column list, select the name of the shared column. Then, select the name of the table to join in the right Table list and select the name of the shared column in the right Column list. Click Add Join. The Database Explorer app creates an inner join by default. Close the Join tab. For details about joining tables, see Join Tables Using Database Explorer App.

  • In the Data Browser pane, expand the table name node of the joined table and select specific check boxes to choose the table columns. The SQL Query and Data Preview panes update with the chosen columns.

  • In the Criteria section, click Where to display the Where tab in the toolstrip. In the Add section, select an operator and value to enter an SQL WHERE condition. Click Add Filter. To represent strings in values, enclose text in single quotes. Close the Where tab.

  • In the Criteria section, click Order By to display the Order By tab in the toolstrip. In the Add section, select the column to sort and click Add Sort. Close the Order By tab.

  • In the Import section, click to import all SQL query results into the MATLAB® workspace as a table.

  • In the Edit section, click Clear Query to clear the current SQL query and create a new one.

  • To close the database connection, close all tabs whose names contain the data source name.

For detailed examples, see the Database Explorer app.

Enter SQL Query Manually

Use these steps as a general workflow for entering an SQL query manually.

  • Connect to a data source in the Database Explorer app. For an example, see MySQL ODBC for Windows.

  • In the Edit section, click Manual to open a new data source tab. The tab has the same data source name as the prior active tab, but the Database Explorer app appends the suffix _manual to the tab name. The manual data source tab keeps the same database connection as the prior active tab.

  • Enter or paste an SQL query into the SQL Query pane.

    Note

    If you click Manual when the active data source tab contains an SQL query in the SQL Query pane, then you can modify the existing SQL query manually. Or, you can click Clear Query to clear the existing SQL query in the new tab and enter a new query.

    For each subsequent time you click Manual, the new tab contains a numbered suffix.

  • In the Preview section, click Preview Query. The Database Explorer app executes the SQL query and updates the Data Preview pane with the results. If the SQL query is valid, the Data Preview pane displays the first 10 rows of data by default. To see more rows, adjust the value in the Preview Size box.

  • Modify the SQL query and click Preview Query. The Data Preview pane shows the updated results.

  • In the Import section, click to import all SQL query results into the MATLAB workspace as a table.

  • To close the database connection, close all tabs whose names contain the data source name.

For a detailed example of entering an SQL query manually, see the Database Explorer app.

Work with Multiple SQL Queries

To create different SQL queries using the same database, follow these steps:

  1. Click New Query in the toolstrip. The Connect to a Data Source dialog box opens.

  2. Select the data source name from the Data Source list. Enter the user name and password for your database, and click Connect.

  3. Select the catalog and schema in the Catalog and Schema dialog box, and click OK. If only one catalog or schema is available in the database, the Catalog and Schema dialog box does not open.

    The Database Explorer app opens a new tab with the data source name as the tab name.

Repeat these steps to connect to the same database again and create a different SQL query. The Database Explorer app appends a numbered suffix to the data source tab name. The number increases by one for each subsequent database connection to the same data source.

Note

The Microsoft® Access™ database does not support multiple database connections.

Or, you can connect to different catalogs or schemas. Repeat these steps and select a different catalog or schema in the Catalog and Schema dialog box.

The database server settings control the maximum number of database connections you can create at a time. To increase the maximum number of database connections, contact your database administrator.

You can connect to different databases by repeating these steps and selecting a different data source name from the Data Source list.

SQL Query Limitations

The Database Explorer app has these limitations, which you can avoid by using the command line instead.

  • You can connect to relational databases only.

  • You can enter a single SQL SELECT statement only. You cannot enter other SQL statements or multiple SQL statements in the SQL Query pane.

  • You cannot modify the database structure or the database data.

  • You cannot execute stored procedures.

See Also

Functions

Apps

Related Topics

External Websites