Database Toolbox    

Creating, Running, and Saving a Query

Topics covered in this section are

Building and Executing a Query

Build and run a query to import data from your database into MATLAB. Then save the query for use again later.

Before You Start.   Before using the VQB, set up a data source--see Setting Up a Data Source. The examples here use the dbtoolboxdemo data source.

To Start.   To open the VQB, in the Command Window type

In the VQB, perform these steps to create and run a query:

  1. From the Data source list box, select the data source from which you want to import data. For this example, select dbtoolboxdemo, which is the data source for the tutorial database.
  1. The list includes all data sources you set up. If you do not see the data source you want to use, you need to add it--see Setting Up a Data Source.

    After selecting a data source, the Tables in that data source appears.

  1. From the Tables list box, select the table that contains the data you want to import. For this example, select salesVolume.
  1. After selecting a table, the Fields (column names) in that table appear.

    Note: Using the Visual Query Builder, you cannot access tables whose names include spaces. You can instead use Database Toolbox functions.

  1. From the Fields list box, select the fields containing the data you want to import. To select more than one field, hold down the Ctrl key or Shift key while selecting. For this example, select the fields StockNumber, January, February, and March.
  1. As you select items from the Fields list, the query appears in the SQL statement field.

  1. In the MATLAB workspace variable field, assign a name for the data returned by the query. For this example, use A.
  2. Click Execute to run the query and retrieve the data.
  1. The query runs, retrieves data, and stores it in MATLAB, which in this example is a cell array assigned to the variable A. In the Data area, information about the query result appears.

  1. Double-click A in the Data section. The contents of A is displayed in the Command Window. Another way to see the contents of A is to type A in the Command Window.

  1. As an example of how to read the results, sales for item 400876 are 3000 in January, 2400 in February, and 1500 in March.


  Online Help for the Visual Query Builder Saving a Query