How To: Create spatial views using a Python batch script


Spatial views in an enterprise geodatabase can be created using the ArcSDE command line, as described in the following technical article, How To: Create a spatially enabled view. However, beginning with ArcGIS 10.3, ArcSDE command line tools are no longer available. This article demonstrates how to create spatial views with a Python batch script, instead of with the ArcSDE command line. 


The instructions below describe how to create spatial views in an enterprise geodatabase using a Python batch script. The script lists all the data in a feature class and creates a database view using the arcpy.CreateDatabaseView_management method. The code can be used in the ArcMap Python console (Geoprocessing > Python) or as a standalone script. 

import arcpy, os
arcpy.env.overwriteOutput = True

# Set the workspace
arcpy.env.workspace = r"Database Connections\Connection to gdb.sde"

# Start the Loop
newpath = r"Database Connections\Connection to gdb.sde"
fcs = arcpy.ListFeatureClasses()

for fc in fcs:
    outfc = fc[8:] + "_VW"
    query = "select * from " + fc
    arcpy.CreateDatabaseView_management(newpath, outfc, query)   #Run the rool
print "complete"


Related Information