Select Layer By Attribute (Data Management)

Adds, updates, or removes a selection on the layer or table view using an attribute query.


Usage tips

Syntax

SelectLayerByAttribute_management (in_layer_or_view, selection_type, where_clause)
Parameter Explanation Datatype
Input Layer or Table View (Required)

The feature layer or table view to which the selection will be applied. This can be a layer or table in ArcMap or a layer or table view created by Make Layer or Make Table View in ArcCatalog or in a script.

Table View
Selection type (Optional)

Determines how the selection will be applied and what to do if a selection already exists.

  • NEW_SELECTION—The resulting selection replaces any existing selection. This is the default.
  • ADD_TO_SELECTION—The resulting selection is added to an existing selection if one exists. If no selection exists, this is the same as the NEW_SELECTION option.
  • REMOVE_FROM_SELECTION—The resulting selection is removed from an existing selection. If no selection exists, this option has no effect.
  • SUBSET_SELECTION—The resulting selection is combined with the existing selection. Only records that are common to both remain selected.
  • SWITCH_SELECTION—Switches the selection. All records that were selected are removed from the selection; all records that were not selected are added to the selection. The expression is ignored when this option is specified.
  • CLEAR_SELECTION—Clears or removes any selection. The expression is ignored when this option is specified.

String
Expression (Optional)

An SQL expression used to select a subset of records.

The syntax for the expression differs slightly depending on the data source. For example, if you're querying file or ArcSDE geodatabases, shapefiles, coverages, dBASE or INFO tables, enclose field names in double quotes:

"MY_FIELD"

If you're querying personal geodatabases, enclose fields in square brackets:

[MY_FIELD].

For more information on SQL syntax and how it differs between data sources, see SQL Reference.

SQL Expression
Data types for geoprocessing tool parameters

Script Example

# ExtactFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a Location and an attribute query
# Requirements: Python and the Python win32all extension
# Author: ESRI
# Data 1/1/2004

# Create the Geoprocessor
import arcgisscripting
gp = arcgisscripting.create()

# Put in error trapping in case an error occurs when running tool
try:
    # Make a layer from the feature class
    gp.MakeFeatureLayer("c:/mexico.mdb/cities","lyr") 

    # Select all cities which overlap the chihuahua polygon
    gp.SelectLayerByLocation("lyr", "intersect", "c:/mexico.mdb/chihuahua", 0, "new_selection")

    # Within the selection (done above) further select only those cities which have a population > 10,000   
    gp.SelectLayerByAttribute("lyr", "SUBSET_SELECTION", " [population] > 10000 ")

    # Write the selected features to a new featureclass
    gp.CopyFeatures("lyr", "c:/mexico.mdb/chihuahua_10000plus")

except:
    # If an error occurred print the message to the screen
    print gp.GetMessages()

See Also

  • Make Feature Layer (Data Management)
  • Make Table View (Data Management)
  • Select Layer By Location (Data Management)