<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Changing Query Layer's SQL Expression with Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165147#M12685</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks! Unfortunately, in my case, for certain reasons, use of the Definition Query is not an option.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 29 Apr 2016 16:53:06 GMT</pubDate>
    <dc:creator>AlexZhuk</dc:creator>
    <dc:date>2016-04-29T16:53:06Z</dc:date>
    <item>
      <title>Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165143#M12681</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I use query layers in ArcMap 10.4. The maps need to be done/repeated periodically. The easiest way is to copy the map, change the source's SQL expression (e.g. from &lt;STRONG style="color: #2873ee;"&gt;select * from &amp;lt;tbl&amp;gt; where Period = '2016-March' &lt;/STRONG&gt;&lt;SPAN style="color: #303030;"&gt;to &lt;/SPAN&gt;&lt;STRONG style="color: #e23d39;"&gt;select * from &amp;lt;tbl&amp;gt; where Period = '2016-April'&lt;/STRONG&gt;) and that's it. But the expressions are long and hard to edit. Then I have many similar maps, where I have to make edits manually. &lt;STRONG&gt;Is there a way to do this with Python?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;P.S. Using the combination of query layer and definition query is not doable in my case.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Apr 2016 18:55:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165143#M12681</guid>
      <dc:creator>AlexZhuk</dc:creator>
      <dc:date>2016-04-28T18:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165144#M12682</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Have you looked at this tool &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/make-query-layer.htm" title="http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/make-query-layer.htm"&gt;Make Query Layer—Help | ArcGIS for Desktop&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Apr 2016 15:14:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165144#M12682</guid>
      <dc:creator>WesMiller</dc:creator>
      <dc:date>2016-04-29T15:14:47Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165145#M12683</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Could you use the &lt;SPAN style="font-family: 'courier new', courier;"&gt;definitionQuery&lt;/SPAN&gt; property of &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/layer-class.htm"&gt;Layer&lt;/A&gt;​?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Apr 2016 16:05:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165145#M12683</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2016-04-29T16:05:03Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165146#M12684</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;This is the way I handled it - maybe someone can streamline this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#
# Modify definition query to specific layers in map document and
# save the modified document to disk.
#


import arcpy


# map document to modify
mapdoc = r"\\hcgis2\e\Arc\Projects\GIS\basemap - All districts for selected year\basemap.mxd"


oldyr = "2014"&amp;nbsp; # modify this...
newyr = "2015"&amp;nbsp; # ...to this...


# ...within these layers - make all lower case
lyrs = {"airport",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "community college",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "city",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "fire",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "school",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "hospital",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "library",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "mass transit",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "multi twp assess",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "park",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "road district",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "sanitary",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "special",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "tiff"}


# open map document to modify
mxd = arcpy.mapping.MapDocument(mapdoc)


# scan through all map layers
for maplyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; ml = maplyr.name.lower()


&amp;nbsp;&amp;nbsp;&amp;nbsp; # if layer to modify...
&amp;nbsp;&amp;nbsp;&amp;nbsp; if ml in lyrs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # ...then update the year within the definition query
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing: ", ml
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; maplyr.definitionQuery = maplyr.definitionQuery.replace(oldyr, newyr)


# save changes to map document
mxd.save()
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
# clean up
print "Finished"
del mxd, oldyr, newyr, lyrs, ml&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 08:37:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165146#M12684</guid>
      <dc:creator>BruceLang</dc:creator>
      <dc:date>2021-12-11T08:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165147#M12685</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks! Unfortunately, in my case, for certain reasons, use of the Definition Query is not an option.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Apr 2016 16:53:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165147#M12685</guid>
      <dc:creator>AlexZhuk</dc:creator>
      <dc:date>2016-04-29T16:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165148#M12686</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you! I will look into this.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Apr 2016 16:53:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165148#M12686</guid>
      <dc:creator>AlexZhuk</dc:creator>
      <dc:date>2016-04-29T16:53:41Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165149#M12687</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;Thank you for the thorough post! Do I&amp;nbsp; understand correctly that you use the regular layer (based on a shapefile or File GDB)? I, however, use the Query Layer, and it has the SQL expression as a part of its source. Unfortunately, in my case, for certain reasons, use of a Definition Query is not an option.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Apr 2016 16:56:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165149#M12687</guid>
      <dc:creator>AlexZhuk</dc:creator>
      <dc:date>2016-04-29T16:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165150#M12688</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, using the layer's "Definition Query" property where each layer is joined to a SQL View of "Taxing Districts" containing data for multiple years.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;All layers "point" to the same parcel polygon feature and the "Definition Query" selects the appropriate "year" for that particular layer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So, in my case, a parcel (a polygon) can only be in one fire district so the definition query is;&lt;STRONG style="font-family: 'courier new', courier;"&gt; parcel_year =2015 AND unit_type_code = 'FRD'&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My code changes the year for each layer while keeping the district type unchanged.&amp;nbsp; I hope that helps someone!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Apr 2016 17:54:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165150#M12688</guid>
      <dc:creator>BruceLang</dc:creator>
      <dc:date>2016-04-29T17:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165151#M12689</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, I wish it had been so simple/easy/straightforward in my case. And maybe it is. I just need to look more into it. Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Apr 2016 17:59:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165151#M12689</guid>
      <dc:creator>AlexZhuk</dc:creator>
      <dc:date>2016-04-29T17:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Query Layer's SQL Expression with Python</title>
      <link>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165152#M12690</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Alex,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We have the same problem. I would like to adapt the query layer within the data source tab of a layer. i need to do this for more than 500 layers, and woul like to do this by script, because otherwise it would take me about 2 days. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Did you find a way to do this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards,&lt;/P&gt;&lt;P&gt;Liesbeth&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Aug 2017 08:50:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-query-layer-s-sql-expression-with-python/m-p/165152#M12690</guid>
      <dc:creator>LiesbethDe_Groote</dc:creator>
      <dc:date>2017-08-18T08:50:08Z</dc:date>
    </item>
  </channel>
</rss>

