Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add a query layer to an arcmap document so that whenever i have a db data update, it is reflected in my ma and I can update my service through python script. This is my script so far...

import arcpy
from arcpy import env
arcpy.env.overWriteOutput = 1

# define local variables
wrkspc = 'C:/ArcGIS_Workspace/'
mxd = arcpy.mapping.MapDocument(wrkspc + 'scripts/Trial.mxd')

input_db_name = "Database Connections/xxx.sde"
outLayer = "DynamicQueryLayer"
out_layer_name = wrkspc +"/shapefiles/DynamicQueryLayer.lyr"

#unique key for the table
oid_field = "schema.TEST_TABLE.TEST_ID"


    # Create a query layer
    arcpy.MakeQueryLayer_management (input_db_name, outLayer, query, oid_field)

    # save the created query layer
    arcpy.SaveToLayerFile_management(outLayer, out_layer_name, "ABSOLUTE")

    # Get the created layer
    lyr = arcpy.mapping.Layer(out_layer_name)

    # get the data frame of the given mxd
    data_frame = arcpy.mapping.ListDataFrames(mxd)[0]

    # Switch to data view
    mxd.activeView =

    arcpy.mapping.AddLayer(data_frame, lyr, 'TOP')
    print "Added layer to the MXD ..."
    print "MXD saved..."    
except Exception, e:
    # If an error occurred, print line number and error message
    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

I get this error. ERROR 000732: Input Features: Dataset DynamicQueryLayer does not exist or is not supported.

I am sure there is a way to save the created query layer as a layer file.Any help is greatly appreciated.

share|improve this question

Use the GP tool Save To Layer File (Data Management):

Creates an output layer file (.lyr) that references geographic data stored on disk.

Alternatively, you could use the .save/.saveACopy methods on the Layer object

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.