Make Query Table (Data Management)
This tool applies an SQL query to a database and the results are represented in a layer or table view. The query can be used to join several tables or return a subset of columns or rows from the original data in the database.
This tool accepts data from an ArcSDE geodatabase, a file geodatabase, a personal geodatabase, or an OLE DB connection.
The layer that is created by the tool is temporary and will not persist after the session ends unless the document is saved.
All input feature classes or tables must be from the same input workspace.
If a Shape column is added to the field list, the result is a layer; otherwise, it is a table view.
The order of the fields in the field list indicates the order the fields will appear in the output layer or table view.
The tool allows you to provide a key field option and key fields list. This information defines how rows are uniquely identified and is used to add a dynamically generated ObjectID column to the data. Without an ObjectID column, selections will not be supported.
The key fields list lets you choose several columns if the combination of these columns is needed to define unique values.
If an SQL expression is used but returns no matching records, the output feature class will be empty.
Feature classes can be joined, but the fields list must contain at most one field of type geometry. If you add more than one geometry column, the tool will display an error when you click OK and execution will stop.
The Fields and Key Fields parameters' Add Field button is used only in ModelBuilder. In ModelBuilder, where the preceding tool has not been run, or its derived data does not exist, the Fields and Key Fields parameters may not be populated with field names. The Add Field button allows you to add expected field(s) so you can complete the Make Query Table dialog box and continue to build your model.
When input tables are from a file geodatabase, tables generally join in the order listed in the Input Tables parameter. For example, if Table1 is listed before Table2, Table2 will be joined by getting a row from Table1, then getting matching rows from Table2. However, if this would result in querying Table2 on an nonindexed field, and reversing the order would result in querying Table1 on an indexed field, the order will be reversed in an attempt to maximize performance. This is the sole query optimization logic at work when you're using file geodatabase data with this tool. In general, joins in file geodatabases perform best when they are one-to-many and one-to-one.
The name of the table or tables to be used in the query. If several tables are listed, the Expression parameter can be used to define how they are to be joined.
The input table can be from an ArcSDE geodatabase, a file geodatabase, a personal geodatabase, or an OLE DB connection.
|Table View; Raster Layer|
The name of the layer or table view that will be created by the tool.
|Table View;Raster Layer|
Indicates how an ObjectID field will be generated, if at all, for the query. The default is USE_KEY_FIELDS.
Specifies a field or combination of fields that can be used to uniquely identify a row in the query. This parameter is used only when the USE_KEY_FIELDS option is set.
The Add Field button, which is used only in ModelBuilder, allows you to add expected field(s) so you can complete the dialog and continue to build your model.
The fields to include in the layer or table view. If an alias is set for a field, this is the name that appears. If no fields are specified, all fields from all tables are included.
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, or dBASE or INFO tables, enclose field names in double quotes:
If you're querying personal geodatabases, enclose fields in square brackets:
In Python, strings are enclosed in matching single or double quotes. To create a string that contains quotes (as is common with a WHERE clause in SQL expressions), you can escape the quotes (using a backslash) or triple quote the string. For example, if the intended WHERE clause is
"CITY_NAME" = 'Chicago'
you could enclose the entire string in double quotes, then escape the interior double quotes like this:
" \"CITY_NAME\" = 'Chicago' "
Or you could enclose the entire string in single quotes, then escape the interior single quotes like this:
' "CITY_NAME" = \'Chicago\' '
Or you could enclose the entire string in triple quotes without escaping:
""" "CITY_NAME" = 'Chicago' """
For more information on SQL syntax and how it differs between data sources, see the help topic SQL reference for query expressions used in ArcGIS.
The following Python window script demonstrates how to use the MakeQueryTable function in immediate mode.
import arcpy from arcpy import env env.workspace = "C:/data/data.gdb" arcpy.MakeQueryTable_management (["Counties","codemog"], "queryout","ADD_VIRTUAL_KEY_FIELD", "", [["Counties.OBJECTID", 'ObjectID'],["Counties.NAME", 'Name'], ["codemog.Males", 'Males'], ["codemog.Females", 'Females']], "Counties.FIPS = codemog.Fips and Counties.STATE_NAME = 'California'")
The following script is an example of how to use the MakeQueryTable tool in the Python scripting environment.
# MakeQueryTableOLEDB.py # Description: Create a query table from two OLE DB tables using a limited set of # fields and establishing an equal join. # Author: ESRI # Import system modules import arcpy try: # Local variables... tableList = ["Database Connections/balrog.odc/vtest.COUNTIES",\ "Database Connections/balrog.odc/vtest.CODEMOG"] fieldList = [["vtest.COUNTIES.OBJECTID", 'ObjectID'],["vtest.COUNTIES.NAME", 'Name']\ ["vtest.CODEMOG.Males", 'Males'],["vtest.CODEMOG.Females", 'Females']] whereClause = "vtest.COUNTIES.FIPS = vtest.CODEMOG.Fips" +\ "and vtest.COUNTIES.STATE_NAME = 'California'" keyField = "vtest.COUNTIES.OBJECTID" lyrName = "CountyCombined" # Make Query Table... arcpy.MakeQueryTable_management(tableList, lyrName,"USE_KEY_FIELDS", keyField, fieldList, whereClause) # Print the total rows print arcpy.GetCount_management(lyrName) # Print the fields Fields = arcpy.ListFields(lyrName) for field in Fields: print Field.name # Save as a dBASE file arcpy.CopyRows_management(lyrName, "C:/temp/calinfo.dbf") except Exception, e: # If an error occurred, print line number and error message import traceback, sys tb = sys.exc_info() print "Line %i" % tb.tb_lineno print e.message