Use the New Query Layer dialog box to compose an SQL query to define a query layer in your map.
The following is a summary of how to create a query layer using the New Query Layer dialog box:
- Connect to the database.
The first step in creating a query layer in ArcGIS is to make a connection to the database you want to query. You can create a database connection in the Catalog tree, as explained in Database connections in ArcMap, or you can create the connection from the New Query Layer dialog box.
- Choose a table.
After a connection to the database is established, a list of tables and views found in that database appears on the New Query Layer dialog box. When you select one of the tables, the columns for that table are displayed.
Each column in a database has a specific data type. ArcGIS can work with most common database types. However, some less common database types are not supported. If the attribute column type is unknown, this indicates that ArcGIS does not support that data type. When you specify a query, all columns that have an unknown data type must either be excluded or changed in the query to a data type that ArcGIS supports.
See DBMS data types supported in ArcGIS for more information.
- Define an SQL query.
You specify an SQL query in the Query text box.
When building a query, a whole table can be added to the Query text box by either double-clicking it or dragging it from the List of Tables window into the Query text box. Likewise, you can add specific columns in a table to the query by double-clicking them or dragging them from the Columns window into the Query text box. You can also type specific queries or cut and paste a query from an external application into the Query text box.
SQL syntax specific to the database should be used when building a query layer. A common example would be as follows: SELECT * FROM Test.myuser.US_States. This would result in a query layer containing all rows from the US_States table. In the map, this would display all the United States.
For more information on building SQL queries, see Building a query expression.
- Validate the SQL statement.
Once the query is created, it must be validated. During validation, ArcGIS attempts to determine the properties of the query layer based on the first row returned in the table.
During validation, ArcGIS attempts to determine the properties of the query layer based on the first row returned in the table.
The properties of a query layer are used to filter the rows returned to ArcGIS from the database. For example, if the features in your feature class use different SRIDs, the SRID property set on the query layer will be used to prevent any rows in the database table that don't match that SRID from being displayed on the map.
- Specify a unique identifier or use the identifier found by ArcGIS during validation.
The unique identifier is one or many fields used by ArcGIS to identify each row in the table.
- For feature classes or views that contain a spatial column, you can define the following or use the defaults detected by ArcGIS when you validate:
- Dimensionality—This determines whether a layer's coordinates will include m-values to store route data and z-values to store 3D data.
- Geometry type—This determines whether the layer stores point, multipoint, line, or polygon features.
- Spatial reference—This is the coordinate system and other related spatial properties for the layer.
Sometimes, ArcGIS cannot accurately determine the spatial reference based on the current SRID value set on the features in the database. When that happens, the query layer will have an unknown spatial reference and you must manually define one. It is important to note that defining a spatial reference for the query layer does not reproject the data; it just defines the spatial reference that should be used when mapping the results of the query within ArcGIS.
- SRID—This is the layer's spatial reference identifier and is used to ensure that only geometries with the same spatial reference identifier are returned by the query. The SRID value will be blank if the query layer does not have a spatial field. If this value is set, all features that do not have the specified geometry SRID value will be excluded from the result set.
Follow these steps to create a query layer in the map:
- On the New Query Layer dialog box, specify a name in the Name text box for the query layer that will be created. This is the name that will appear in the ArcMap table of contents.
- Click Connections to connect to the database that contains the table you want to query.
The Manage Connection(s) dialog box appears. From here, you can choose existing database connections, create connections, and edit or delete existing connections.
- Choose an existing connection or create a connection to a database.
- To create a database connection, click New. The Database Connection dialog box appears. Provide connection information as described in Database connections in ArcMap. When the new connection is added to the Existing Connection(s) list, type a descriptive name for the connection and click OK.
- To use an existing connection, choose it from the Existing Connection(s) list and click OK.
- Enter an SQL query in the Query text box.
- Click Validate to make sure the query syntax is correct and returns data that can be used by ArcGIS. The validation process executes the query in the database and verifies whether the result set from the query meets the data modeling standards enforced by ArcGIS. A query layer is not added to the map until the layer definition is valid.
Rules for validation are as follows:
- The result set must have, at most, one spatial field.
- The result set must have, at most, one spatial reference.
- The result set must have only one shape type.
- The result set cannot have any field types not supported by ArcGIS.
If the validation fails for any reason, an error message is returned so you can modify the SQL query.
Validation is especially important when working with data in spatial databases that do not enforce the same standards as ArcGIS.
During the validation process, ArcGIS sets the dimensionality, geometry type, spatial reference, SRID, and unique identifier properties on the query layer. These values are based on the first row returned in the query. If you need to change these settings, check the Show Advanced Properties check box to display the Advanced Options dialog box and set any of the following:
- Choose a field or fields that contains values that uniquely identify each row in the layer. See Unique identifier fields for more information.
- Choose the geometry type from the drop-down menu. Only features of the geometry type you choose will appear in the query result set.
- Click Select to choose the coordinate system to be used for the spatial reference or import the spatial reference from an existing dataset by clicking Import.
- If your query is successfully validated and you've set any advanced options you require, click Finish to add the result set to the map as a query layer.
For information on changing query layer properties, see Modify a query layer.