Generate Definition Query From Selection (Data Management)

Summary

Creates a definition query (in SQL format) from the selected features or rows of the layer or table.

Usage

  • An active selection on the input is required.

  • The input layer or table view must be present in the map or scene.

  • A definition query clause has a limit of 1,000 values. If the selection includes more than 1,000 values, the resulting definition query will include multiple clauses.

  • When a layer has a GlobalID field, and the Generation Method parameter is set to Match selection, the GlobalID field will be used in place of the ObjectID field. The GlobalID field will not be listed in the parameter if the Generation Method parameter is set to Use field values.

  • The Append The Active Query parameter is only available if the Generation Method parameter is set to Use field values and the input layer or table has an active definition query. You can determine the active query by examining the layer or table properties.

  • When the Overwrite Where Clause parameter is checked, but the Where Clause parameter value has not been modified or specified, the tool will use the default generated definition query.

Parameters

LabelExplanationData Type
Input Table

The layer or table view from which the definition query will be generated.

Table View
Generation Method
(Optional)

Specifies the method that will be used to generate the definition query.

  • Match selectionThe input table's ObjectID field (or GlobalID field if present) will be used to generate the query with values present in the selection. This is the default.
  • Use field valuesThe Field parameter value will be used to generate the query with values present in the selection.
String
Field
(Optional)

The field in the table or table view that will be used to generate values for the query. This parameter is required when the Generation Method parameter is set to Use field values.

Field
Query Name
(Optional)

The unique name of the query that will be generated.

String
Invert Where Clause
(Optional)

Specifies whether the generated definition query (where clause) will be inverted and include all unselected values or include all selected values of the input table.

  • Checked—The where clause will be inverted.
  • Unchecked—The where clause will not be inverted. This is the default.

Boolean
Append The Active Query
(Optional)

Specifies whether the generated definition query will be appended to the active query. This parameter is available when the layer or table has an active definition query and the Generation Method parameter is set to Use field values.

  • Checked—The generated definition query will be appended to the active query.
  • Unchecked—The generated definition query will not be appended to the active query. This is the default.

Boolean
Overwrite Where Clause
(Optional)

Specifies whether the definition query (where clause) will be displayed and can be modified using the Where Clause parameter before it is generated.

  • Checked—The where clause will be displayed and can be modified before it is generated.
  • Unchecked—The where clause cannot be modified. This is the default.

Boolean
Where Clause
(Optional)

The generated definition query SQL expression from the other parameter values. For more information about SQL syntax, see SQL reference for query expressions used in ArcGIS.

SQL Expression

Derived Output

LabelExplanationData Type
Updated Table

The updated input with the generated definition query.

Feature Layer, Table View

arcpy.management.GenerateDefinitionQueryFromSelection(in_table, {method}, {field}, {query_name}, {invert_where_clause}, {append_active_query}, {overwrite_where_clause}, {where_clause})
NameExplanationData Type
in_table

The layer or table view from which the definition query will be generated.

Table View
method
(Optional)

Specifies the method that will be used to generate the definition query.

  • MATCH_SELECTIONThe input table's ObjectID field (or GlobalID field if present) will be used to generate the query with values present in the selection. This is the default.
  • USE_FIELD_VALUESThe field parameter value will be used to generate the query with values present in the selection.
String
field
(Optional)

The field in the table or table view that will be used to generate values for the query. This parameter is required when the method parameter is set to USE_FIELD_VALUES.

Field
query_name
(Optional)

The unique name of the query that will be generated.

String
invert_where_clause
(Optional)

Specifies whether the generated definition query (where clause) will be inverted and include all unselected values or include all selected values of the input table.

  • INVERTThe where clause will be inverted.
  • NON_INVERTThe where clause will not be inverted. This is the default.
Boolean
append_active_query
(Optional)

Specifies whether the generated definition query will be appended to the active query. This parameter is available when a layer or table has an active definition query and the method parameter is set to USE_FIELD_VALUES.

  • APPENDThe generated definition query will be appended to the active query.
  • NOT_APPENDThe generated definition query will not be appended to the active query. This is the default.
Boolean
overwrite_where_clause
(Optional)

Specifies whether the definition query (where clause) can be modified using the where_clause parameter before it is generated.

  • OVERWRITEThe where clause can be modified before it is generated.
  • NOT_OVERWRITEThe where clause cannot be modified. This is the default.
Boolean
where_clause
(Optional)

The definition query that will override the other parameter values. For more information about SQL syntax, see SQL reference for query expressions used in ArcGIS.

SQL Expression

Derived Output

NameExplanationData Type
out_table

The updated input with the generated definition query.

Feature Layer, Table View

Code sample

GenerateDefinitionQueryFromSelection example (Python window)

The following Python window script demonstrates how to use the GenerateDefinitionQueryFromSelection function in immediate mode.

import arcpy
arcpy.env.workspace = "C:/data"

# Set local variables
in_table = "WisconsinAirports.lyrx"
where_clause = '"TRAFFICCOUNT" >= \'100\''

# Run the tool
arcpy.management.GenerateDefinitionQueryFromSelection(
    in_table,
    "MATCH_SELECTION",
    "MyPythonQueryName",
    "NON_INVERT",
    "NOT_APPEND",
    "OVERWRITE",
    where_clause)

Environments

This tool does not use any geoprocessing environments.

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes

Related topics