<?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 Can't join/export feature layer with definition expression set on related table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/can-t-join-export-feature-layer-with-definition/m-p/492327#M38578</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a polygon feature layer with a related table that holds each polygon's associated records. The two tables are related based on the MgmtTractID field. In my script, I want to be able to pull just records and their associated polygons for a certain year. So, if the year was 2010, I would set the definition expression on the table to be "FocalRefID = '{...}' and YearTreated = 2010 or FocalRefID = '{...}' and YearTreated = 2012" and then after that has been applied, I would join the result to my polygon layer to limit the polygons shown to only those that were active in 2010. When I do this manually in Desktop, it works just how I would expect. When I try to do this in python, the join results in zero polygon features and I get a DBMS error that says my feature layer doesn't exist which is perplexing...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code that doesn't work:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="comment token"&gt;#Create feature layers/get table&lt;/SPAN&gt;
tractFC &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; hbMgmt_db_con &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'\HabitatManagement.DBO.MgmtTracts'&lt;/SPAN&gt;
tractLyr &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;MakeFeatureLayer_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractFC&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTracts"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
tractAttrbTbl &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; hbMgmt_db_con &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'\HabitatManagement.DBO.MgmtAttrb'&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;#Do a bunch of stuff&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTracts"&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; layers&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#Generate the query expression&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#...&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#...&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#Get the appropriate records in the related table and export them to the geodatabase&lt;/SPAN&gt;
    tractAttrbTV &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;MakeTableView_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractAttrbTbl&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'tractAttrbTV'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; expression&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;TableToTable_conversion&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractAttrbTV&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; exportPath&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTractAttrb"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#Use the new table view to join to the tract layer and select the polygon tracts to export&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddJoin_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractLyr&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTractID"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; tractAttrbTV&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTractID"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;FeatureClassToFeatureClass_conversion&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractLyr&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; exportPath&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTracts"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I comment out the "AddJoin" line, the script runs normally and the resulting feature layer that gets exported contains all the features with no limitations put on it. If I leave the AddJoin line as is and comment out the FeatureClassToFeatureClass line, the join carries out normally, but the result is empty. If I leave both lines uncommented I get this error. Given what I just explained, I'm 99% sure this error is bogus and it does not have anything to do with my DBMS since it obviously can find the tractLyr just fine otherwise the FC2FC line would fail, even when the AddJoin line was commented out. I do not know where the 'oHabitatManagement' is coming from though.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;File "D:\NET_Projects\HabitatMapGPServices\ShipHabitatData\ShipHabitatData.py", line 175, in &amp;lt;module&amp;gt;&lt;BR /&gt; arcpy.FeatureClassToFeatureClass_conversion(tractLyr, exportPath, "MgmtTracts")&lt;BR /&gt; File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\conversion.py", line 1910, in FeatureClassToFeatureClass&lt;BR /&gt; raise e&lt;BR /&gt;arcgisscripting.ExecuteError: ERROR 999999: Error executing function.&lt;BR /&gt;Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'oHabitatManagement'.] [DBJoin1]&lt;BR /&gt;Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'oHabitatManagement'.] [DBJoin1]&lt;BR /&gt;Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'oHabitatManagement'.] [DBJoin1]&lt;BR /&gt;Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'oHabitatManagement'.] [DBJoin1]&lt;BR /&gt;The table was not found. [MgmtTracts]&lt;BR /&gt;Failed to execute (FeatureClassToFeatureClass).&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;It apparently has something to do with the query expression I create. When I changed the expression to be something much simpler (e.g. YearTreated = 2010), the code ran without issue. This is what I do to create the definition query to be applied to the table:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTracts"&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; layers&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Get FocalReferenceIDs of interest and popuplate dictionary&lt;/SPAN&gt;
frIDs &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;
states &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;allCIPs&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'FocalRefID'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'StateID'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; sCursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; sCursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        frIDs&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; years
        states&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;append&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Initiate new list to hold expressions for final query&lt;/SPAN&gt;
expSegements &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Create query expression with FocalReferenceIDs and years of interest&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; k&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;v &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; frIDs&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;items&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; value &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; v&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"FocalRefID = '"&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; k &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"' and YearTreated = {0}"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;value&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
        expSegements&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;append&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;expression&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Convert expression segments to one long string&lt;/SPAN&gt;
expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;" or "&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;join&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;expSegements&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The above results in a definition query being generated that looks like this:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;FocalRefID = '{5B61BFBB-A99A-4EB5-ABAC-89A0D143DBDD}' and YearTreated = 2009 or
 FocalRefID = '{5B61BFBB-A99A-4EB5-ABAC-89A0D143DBDD}' and YearTreated = 2015 or
 FocalRefID = '{A31CF0B8-68A1-4927-9A19-6232626588DD}' and YearTreated = 2009 or
 FocalRefID = '{A31CF0B8-68A1-4927-9A19-6232626588DD}' and YearTreated = 2015‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not seeing anything wrong with that. It applied to the table just fine, it only becomes a problem when trying to join the table result to the feature layer.&amp;nbsp;From what I can deduce, the problem comes at the "or" part of the statement. If I use an expression like this:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;FocalRefID = '{...}' and YearTreated = 2009&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It'll do the job. If I extend it with an 'or' statement like this:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;FocalRefID = '{...}' and YearTreated = 2009 or FocalRefID = '{...}' and YearTreated = 2015&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It'll fail. Is there some other way I should be building the SQL expression?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 21:39:51 GMT</pubDate>
    <dc:creator>MKF62</dc:creator>
    <dc:date>2021-12-11T21:39:51Z</dc:date>
    <item>
      <title>Can't join/export feature layer with definition expression set on related table</title>
      <link>https://community.esri.com/t5/python-questions/can-t-join-export-feature-layer-with-definition/m-p/492327#M38578</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a polygon feature layer with a related table that holds each polygon's associated records. The two tables are related based on the MgmtTractID field. In my script, I want to be able to pull just records and their associated polygons for a certain year. So, if the year was 2010, I would set the definition expression on the table to be "FocalRefID = '{...}' and YearTreated = 2010 or FocalRefID = '{...}' and YearTreated = 2012" and then after that has been applied, I would join the result to my polygon layer to limit the polygons shown to only those that were active in 2010. When I do this manually in Desktop, it works just how I would expect. When I try to do this in python, the join results in zero polygon features and I get a DBMS error that says my feature layer doesn't exist which is perplexing...&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code that doesn't work:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="comment token"&gt;#Create feature layers/get table&lt;/SPAN&gt;
tractFC &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; hbMgmt_db_con &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'\HabitatManagement.DBO.MgmtTracts'&lt;/SPAN&gt;
tractLyr &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;MakeFeatureLayer_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractFC&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTracts"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
tractAttrbTbl &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; hbMgmt_db_con &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'\HabitatManagement.DBO.MgmtAttrb'&lt;/SPAN&gt;

&lt;SPAN class="comment token"&gt;#Do a bunch of stuff&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTracts"&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; layers&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#Generate the query expression&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#...&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#...&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#Get the appropriate records in the related table and export them to the geodatabase&lt;/SPAN&gt;
    tractAttrbTV &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;MakeTableView_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractAttrbTbl&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'tractAttrbTV'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; expression&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;TableToTable_conversion&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractAttrbTV&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; exportPath&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTractAttrb"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    &lt;SPAN class="comment token"&gt;#Use the new table view to join to the tract layer and select the polygon tracts to export&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;AddJoin_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractLyr&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTractID"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; tractAttrbTV&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTractID"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
    arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;FeatureClassToFeatureClass_conversion&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;tractLyr&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; exportPath&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTracts"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I comment out the "AddJoin" line, the script runs normally and the resulting feature layer that gets exported contains all the features with no limitations put on it. If I leave the AddJoin line as is and comment out the FeatureClassToFeatureClass line, the join carries out normally, but the result is empty. If I leave both lines uncommented I get this error. Given what I just explained, I'm 99% sure this error is bogus and it does not have anything to do with my DBMS since it obviously can find the tractLyr just fine otherwise the FC2FC line would fail, even when the AddJoin line was commented out. I do not know where the 'oHabitatManagement' is coming from though.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;File "D:\NET_Projects\HabitatMapGPServices\ShipHabitatData\ShipHabitatData.py", line 175, in &amp;lt;module&amp;gt;&lt;BR /&gt; arcpy.FeatureClassToFeatureClass_conversion(tractLyr, exportPath, "MgmtTracts")&lt;BR /&gt; File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\conversion.py", line 1910, in FeatureClassToFeatureClass&lt;BR /&gt; raise e&lt;BR /&gt;arcgisscripting.ExecuteError: ERROR 999999: Error executing function.&lt;BR /&gt;Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'oHabitatManagement'.] [DBJoin1]&lt;BR /&gt;Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'oHabitatManagement'.] [DBJoin1]&lt;BR /&gt;Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'oHabitatManagement'.] [DBJoin1]&lt;BR /&gt;Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'oHabitatManagement'.] [DBJoin1]&lt;BR /&gt;The table was not found. [MgmtTracts]&lt;BR /&gt;Failed to execute (FeatureClassToFeatureClass).&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;It apparently has something to do with the query expression I create. When I changed the expression to be something much simpler (e.g. YearTreated = 2010), the code ran without issue. This is what I do to create the definition query to be applied to the table:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"MgmtTracts"&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; layers&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Get FocalReferenceIDs of interest and popuplate dictionary&lt;/SPAN&gt;
frIDs &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;{&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;}&lt;/SPAN&gt;
states &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;with&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;SearchCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;allCIPs&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'FocalRefID'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'StateID'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; sCursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; row &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; sCursor&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        frIDs&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; years
        states&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;append&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;row&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Initiate new list to hold expressions for final query&lt;/SPAN&gt;
expSegements &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Create query expression with FocalReferenceIDs and years of interest&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; k&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;v &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; frIDs&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;items&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
    &lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; value &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; v&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
        expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"FocalRefID = '"&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; k &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"' and YearTreated = {0}"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;value&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
        expSegements&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;append&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;expression&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="comment token"&gt;#Convert expression segments to one long string&lt;/SPAN&gt;
expression &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;" or "&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;join&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;expSegements&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The above results in a definition query being generated that looks like this:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;FocalRefID = '{5B61BFBB-A99A-4EB5-ABAC-89A0D143DBDD}' and YearTreated = 2009 or
 FocalRefID = '{5B61BFBB-A99A-4EB5-ABAC-89A0D143DBDD}' and YearTreated = 2015 or
 FocalRefID = '{A31CF0B8-68A1-4927-9A19-6232626588DD}' and YearTreated = 2009 or
 FocalRefID = '{A31CF0B8-68A1-4927-9A19-6232626588DD}' and YearTreated = 2015‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm not seeing anything wrong with that. It applied to the table just fine, it only becomes a problem when trying to join the table result to the feature layer.&amp;nbsp;From what I can deduce, the problem comes at the "or" part of the statement. If I use an expression like this:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;FocalRefID = '{...}' and YearTreated = 2009&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It'll do the job. If I extend it with an 'or' statement like this:&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;FocalRefID = '{...}' and YearTreated = 2009 or FocalRefID = '{...}' and YearTreated = 2015&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It'll fail. Is there some other way I should be building the SQL expression?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:39:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/can-t-join-export-feature-layer-with-definition/m-p/492327#M38578</guid>
      <dc:creator>MKF62</dc:creator>
      <dc:date>2021-12-11T21:39:51Z</dc:date>
    </item>
  </channel>
</rss>

