You use the Add Data button on the Map ribbon to create query layers. Before you can create a query layer, you must first make a connection to the database.
After a connection to the database is established, a list of tables and views found in that database populates the left window of the dialog box. When you select one of the tables, the columns for that table are displayed in the right window.
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.dbo.US_States. This would result in a query layer containing all rows from the US_States table. In ArcMap, this would display all the United States. For more information on building SQL queries, see Building a query expression.
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.
The properties of a query layer include the following:
- Unique identifier field—This is one or many fields used by ArcGIS to uniquely identify the layer.
- 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 is a point, multipoint, line, or polygon.
- Spatial reference—This is the coordinate system and other related spatial properties for the layer.
- 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.
You can view and modify these properties after a query layer has been validated by clicking Next.
The properties of a query layer are used to filter the rows returned to ArcGIS from the database. For example, if you have geometries in a table that have multiple 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 in the map.
- Query layers can be added to the map by clicking the Add Data button on the Map ribbon and selecting the Query Layer button from the drop-down list. This will open the query layer window.
- On the New Query Layer dialog box, specify a name in the Name text box for the query that will be created. This is the name that will appear in the Contents pane.
- When the new query layer window opens, you must select a database connection where your data is stored. If the connection already exists in the project, select the connection from the drop-down list. If the connection does not already exist in the project, select the new database connection button and create a new connection.
- Enter an SQL query into the Query text box.
After a connection to the database is established, a list of tables and views found in that database populates the left window of the dialog box. You can select a table to work with by single-clicking the table name; the table columns will be displayed in the right window.
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.
When you select one of the tables, the columns for that table are displayed in the right window. You can add specific columns in a table to the query by double-clicking the column name or by dragging them from the Columns window into the Query text box.
- Next, choose whether you want to define the spatial properties for the layer, or use the default option to let ArcGIS Pro do it for you.
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 want to manually define these properties, select the Define spatial properties for the layer option.
- Once the query is created, it must be validated. Click Validate to make sure that 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 it 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 query.
Validation is especially important when working with data in spatial databases that do not enforce the same standards as ArcGIS.
- When you have successfully validated your query, click Next.
- Select the unique identifier field for the query layer.
All layers in ArcMap need a unique identifier. Therefore, your query layer must contain a unique identifier. See more about choosing a unique identifier field for a query layer.
- Define the spatial properties of the query layer by selecting the geometry type and spatial reference. These parameters are already set if you chose to let ArcGIS Pro determine the spatial properties for you.
See more about defining the spatial reference of the query layer.
- If you have chosen to manually define the spatial properties of the layer, you will need to select an option from the Layer Extent Properties. You can choose to manually enter the extent, use the map extent, or use the spatial reference extent. If you choose to manually enter the extent, you must type a valid extent that includes all the features in the table. Click Next to enter the values if you selected to input the extent, or to review the map or spatial reference extent values if you did not choose to enter your own values.
- Click Finish to add the query layer to the map.