Font size

(Legacy) Query element

This article describes how to construct queries to interact with an external database connected via the deprecated Database element.
To optimize the data processing in your model, consider using the built-in model database.

With the Query connectivity tool, you construct SQL queries to databases graphically. These databases must be associated with your model via the legacy Database element. The Query element represents the general SQL operation of retrieving data from database tables.

Using Query, you can feed up a model with data returned by the specified query. Query result represents a set of rows. Each row contains a set of values from the table’s columns you have specified in the query. You can use the query result to construct:

  • Agent population. Each row of the query result will be used to add one more agent into a population. Values will be used to setup the parameters of the agent. This option is very useful in agent based modeling when you want to populate your model with agents, who have characteristics defined by parameters stored in some database.
  • Collection. Each row of the query result will be used to add one more element to a collection. Values will be stored in the fields of the collection variable.

Creating a query

To define a query

  1. Drag the Query  element from the Professional section of the Connectivity palette to the graphical editor.
  2. Open the Properties view and modify the Name of the element. This name will be used to identify and access this query.
  3. In the Database edit box, specify the database this query will access.
  4. If you need to retrieve all data from some table of the specified database, just select the Select all from the table option in the Query section and specify the name of the table in the edit box to the right.
    If you are working with Access 2007 database, specify the name of the table in the square brackets, e.g.: [Table].
  5. Otherwise, if you need to retrieve some specific data only, select the SQL option and type your custom query in the edit box below.

Previewing the query results

You can preview the query result any time.

To preview query results

  1. Select the Query object you want to examine.
  2. Go to the Properties view, Data feeding section.
  3. You will see the table listing the result this query will return. The table contains columns you have specified in the query.

Using the preview, you check the query. Table fields should match the ones defined in the query.

How can we improve this article?