[GIS] Create new geodatabase in modelbuilder and place queried data in new database

arcgis-desktopmodelbuilder

Query existing database and place queried data into newly created geodatabase

I have built a model that will take an existing database and query all feature classes in that dataset using a SQL Expression and I then want all the queried data from each feature class to be exported by feature class to a newly created database. I know I can manually create a new geodatabase and then set the output to that the geodatabase in the "Select" tool but I want the model to create the database automatically. The model here shows how I have tried to do it but by doing it the way illustrated here it creates a new GDB for each iteration, exports the feature to the new GDB and then moves on the next iteration, overwriting the previous feature class.

How can I create the geodatabase first (once and only once), then run the iteration and place each each iterated feature class into the newly created database?

Any advice?

Best Answer

I recently deleted my post regarding adding a precondition between the creation of the Personal GDB and the iteration step. As @Hornbydd points out, anything within the model when an iteration is also present will run every time. This is noted in the online help: Iterate Feature Classes (ModelBuilder) (last bullet point).

I created a quick script within a toolbox that accepts three model parameters. I did not add the Select (Analysis) tool to the script...but I am sure that it can be done. I have tested this script and it creates the new GBD and adds the feature classes present in the input workspace.

import arcpy
import os

GDB_name = arcpy.GetParameterAsText(0)
GDB_Location = arcpy.GetParameterAsText(1)

arcpy.CreatePersonalGDB_management(GDB_Location, GDB_name)

arcpy.env.workspace = arcpy.GetParameterAsText(2)

fcs = arcpy.ListFeatureClasses()

feature = []

for fc in fcs:
    feature.append(fc)

arcpy.FeatureClassToGeodatabase_conversion(feature, os.path.join(GDB_Location,  GDB_name) + ".mdb")

Parameters of tool

Related Question