I have wanted tools in ArcMap that let me do more things with Standalone Tables. For example, there is no tool that will let me duplicate records in a Standalone Table or insert a row directly into an existing Standalone Table that came from the tabular portion of a feature. I cannot Copy/Paste rows or features into a Standalone Table at all like I can with features in a Feature Class. Even for features using Copy/Paste is slow and can only be done in an edit session. The Append tool works for table to table inserts or feature class to feature inserts, but only if the input and output are different from each other. The Append tool also will not let me take the tabular data of a feature and append it into an existing Standalone Table.
I wanted a tool that would quickly do inserts and work for Feature Class to Feature Class, Feature Class to Table, or Table to Table inserts. I also wanted the selection behavior to be similar to the Copy/Paste behavior. I wanted the tool to only work when records are first selected, and I wanted the selected records of the input to be cleared and the newly inserted records in the target to be selected when the tool finishes. That is especially useful when the input and the target are the same table or feature class. I also wanted the tool to automatically match fields like Copy and Paste does.
The field matching behavior is similar to using the default field map that appears in the Append tool when the NO TEST option is chosen, so the schemas don't have to match exactly. However, unlike the Append tool, the Insert Selected Features or Rows tool does not support multiple inputs and does not support customized field mapping.
An edit session should not be active if the data is versionsed, since arcpy cannot connect to an active edit session. The tool will internally start and stop an edit session when versioned data is involved. If data in not versioned an edit session may be active, but is not necessary. When an edit session is active for unversioned data, if the edit session is stopped before saving the inserts then the inserts will disappear without warning and will not be saved.
The tool lets you specify the number of copies of each feature or row you want to insert into the target layer or table view. The minimum number of copies is 1, and 1 is the default value.
NOTE: If the table view of the target is open when the tool is run, then the Append tool and my tool are affected by an Esri bug. The records are inserted, but the total record count of the table view does not update and the newly inserted records cannot be accessed or seen when Show all records view is active. However, since the inserted records are selected by my tool, after the tool completes the selected records count of the table view is updated and the new records do appear when the Show selected records view is active. To fully refresh the table view to show the new records in the Show all records view mode either the table view has to be closed and reopened or a field has to have its values sorted.
The attached python toolbox includes the code for the tool and the tool help files. I recommend that you save the attached toolbox in your "%APPDATA%\ESRI\Desktop10.2\ArcToolbox\My Toolboxes" directory. The toolbox also Includes my Multiple Field Key to Single Field Key tool for 10.2. For more on that tool see my blog here.
The code for the tool is shown below:
# Author: Richard Fairhurst
# Date: July 2015
"""Define the toolbox (the name of the toolbox is the name of the
self.label = "Field Match Tools"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [InsertSelectedFeaturesOrRows]
"""Define the tool (tool name is the name of the class)."""
self.label = "Insert Selected Features or Rows"
self.description = ""
self.canRunInBackground = False
"""Define parameter definitions"""
# First parameter
param0 = arcpy.Parameter(
displayName="Source Layer or Table View",
# Second parameter
param1 = arcpy.Parameter(
displayName="Target Layer or Table View",
# Third parameter
param2 = arcpy.Parameter(
displayName="Number of Copies to Insert",
param2.value = 1
# Fourth parameter
param3 = arcpy.Parameter(
displayName="Derived Layer or Table View",
param3.parameterDependencies = [param1.name]
param3.schema.clone = True
params = [param0, param1, param2, param3]
"""Set whether tool is licensed to execute."""
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
insertFC = parameters.value
strInsertFC = str(insertFC)
if parameters.value and '<geoprocessing Layer object' in strInsertFC:
FC = parameters.value
strFC = str(FC)
if not '<geoprocessing Layer object' in strFC:
print("Input FC must be a layer if output is a layer")
parameters.setErrorMessage("Input must be a feature layer if the Output is a feature layer!")
dscFCLyr = arcpy.Describe(FC)
dscinsertFCLyr = arcpy.Describe(insertFC)
# add the SHAPE@ field if the shapetypes match
if dscFCLyr.featureclass.shapetype != dscinsertFCLyr.featureclass.shapetype:
print("Input and Output have different geometry types! Geometry must match!")
parameters.setErrorMessage("Input and Output do not have the same geometry")
if dscFCLyr.featureclass.spatialReference.name != dscinsertFCLyr.featureclass.spatialReference.name:
print("Input and Output have different Spatial References! Spatial References must match!")
parameters.setErrorMessage("Input and Output do not have the same Spatial References! Spatial References must match!")
if parameters.value <= 0:
parameters.setErrorMessage("The Number of Row Copies must be 1 or greater")