Merging all tables in a File Geodatabase

06-15-2012 01:34 PM
New Contributor
I have a model that creates multiple tables in a file geodatabase that are the results of an OD cost matrix. I need to merge all of the tables in to one. I want to be able to do this automatically as the number of tables and their names will change. How can I add all of the tables into the Merge Tool automatically? I would like to stay in the model environment as I am not very experienced in python. Any ideas on how to do this?

0 Kudos
4 Replies
Esri Regular Contributor
Are you iterating this model?
Please can you share a snapshot?
Can you try using the Append tool to merge all the tables into a target dataset (yes, u will have to create a target dataset with the schema of all possible fields that your tables could contain). Read tool help.
0 Kudos
New Contributor
Thank you for you reply. There is an Iterator in a nested model that performs the OD Cost Matrix. I tried using Collect Values to add the tables created from the copy rows within the Iterator as the input for the Merge. The Merge Tool shows the Tables I want to Merge in the Input Dataset section, and shows the fields in the Field Map section, but the Merge fails with:
-ERROR 000622: Failed to execute (Merge). Parameters are not valid.
-ERROR 000734: Inputs Datasets: 'all Tables created by Copy Rows(5 of them)'  does not exist or is not supported. Failed to execute (Merge).
The Tables do exsist and the paths to them are shown correct in the Merge's Input Dataset section.

I get the same errors with the Append Tool. All 5 Tables I want to Merge are from the Lines of the OD Cost Matrix and all have the same fields. All 5 exsist in the same GDB and are the only things in it.


0 Kudos
New Contributor
For anyone interested, I found a script here that does what I need. I updated it to work with arcpy and added parameters to make it a script tool.

import arcpy
from arcpy import env
import os.path

in_dir = arcpy.GetParameterAsText(0)
out_dir = arcpy.GetParameterAsText(1)
env.workspace = in_dir
input_tables = arcpy.ListTables()

vtab = arcpy.ValueTable()
for table in input_tables:
    vtab.addRow(os.path.join(in_dir, table))
merged_table = os.path.join(out_dir)
arcpy.Merge_management(vtab, merged_table, '')
0 Kudos
New Contributor II
Glad you found a solution, but the errors you cite sound like the problem I had with Iterate and Collect Values, which I also mentioned here:

ArcGIS may be deleting the last table being created during the iteration, because it considers it intermediate data.  Yet the last named output is still included in the Collect Values dataset and is passed into the next tool (Merge), which causes the tool to crash, because one element that was "collected" no longer exists.  (And even though the error description lists all of the files feeding into the Merge tool, it did the same in my case, but the only missing one was the last one, which caused the Merge to crash.  You should at least check whether this might be happening for you.)

In my case, unchecking Intermediate for the Derived Data (your equivalent = %Name%) solved the problem.
0 Kudos