<?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 Compare two tables and copy over missing records in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/compare-two-tables-and-copy-over-missing-records/m-p/1083580#M61880</link>
    <description>&lt;P&gt;Try this again -&lt;/P&gt;&lt;P&gt;Workflow is similar to my original solution -&amp;nbsp;&lt;A href="https://community.esri.com/t5/data-management-questions/comparing-two-feature-classes-and-updating-one-of-them/m-p/247407#M14075" target="_blank"&gt;https://community.esri.com/t5/data-management-questions/comparing-two-feature-classes-and-updating-one-of-them/m-p/247407#M14075&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This time it is do an end around the versioning issue with a Survey123 table (backend in SDE).&amp;nbsp; To my understanding based on this article Survey123 based feature services (tables) in SDE cannot be versioned-&amp;nbsp;&lt;A href="https://doc.arcgis.com/en/survey123/desktop/create-surveys/survey123withexistingfeatureservices.htm#:~:text=The%20data%20cannot%20be%20versioned,before%20enabling%20attachments%20on%20either" target="_blank"&gt;https://doc.arcgis.com/en/survey123/desktop/create-surveys/survey123withexistingfeatureservices.htm#:~:text=The%20data%20cannot%20be%20versioned,before%20enabling%20attachments%20on%20either&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Let's say I have a related table to versioned sanitary lines, the related table has to be versioned as well.&amp;nbsp; Since Survey123 cannot handle versioned data, I cannot push to that table directly.&amp;nbsp; I think I need one table to service the Survey123 form, and the the other to fulfill the relationship.&amp;nbsp; My thinking was compare the two tables and move the new records from Survey123 to the related table, based on globalIDs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have come up with thus far.&amp;nbsp; Not sure if I have to do an edit session (I think I do since one of them is versioned).&amp;nbsp; I also get this error:&amp;nbsp; Message File Name Line Position&lt;BR /&gt;Traceback&lt;BR /&gt;&amp;lt;module&amp;gt; C:\Users\Dbuehler\Desktop\scripts\TryUpdatingWW.py 74&lt;BR /&gt;RuntimeError: Underlying DBMS error [[Microsoft][ODBC Driver 17 for SQL Server]Syntax error, permission violation, or other nonspecific error] [COMGIS3.GIS.SanCleanings_Test]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help will be much appreciated,&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import sys
import os


print "Looping through and adding and deleting"

#Looping through each feature class and building a list of comparsions. Then adds or deletes them to the SDE feature class.
def createMatchingFieldList(fc1, fc2):
    lst1 = [fld.name for fld in arcpy.ListFields(fc1)]
    lst2 = [fld.name for fld in arcpy.ListFields(fc2)]
    return list(set(lst1) &amp;amp; set(lst2))

def createWhereClause(fc, fld_name, value):
    if len(arcpy.ListFields(fc, fld_name)) == 1:
        fld = arcpy.ListFields(fc, fld_name)[0]
        if fld.type == "String":
            where = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(fc, fld_name), value)
        else:
            where = "{0} = {1}".format(arcpy.AddFieldDelimiters(fc, fld_name), value)
    return where

def getPrimaryFieldValues(fc, field):
    return [r[0] for r in arcpy.da.SearchCursor(fc, [field])]

def getSelectCursor(fc, flds, whereClause):
    return arcpy.da.SearchCursor(fc, flds, whereClause)

def diff(a, b):
    return list(set(a) - set(b))

#source = arcpy.GetParameterAsText(0)
#destination = arcpy.GetParameterAsText(1)
#fieldName = arcpy.GetParameterAsText(2)

source = 'Database Connections/GIS Editor to COMGIS3.sde/COMGIS3.GIS.SanCleanings_Test'
destination = 'Database Connections/GIS Editor to COMGIS3.sde/COMGIS3.GIS.SanCleanings_Test2'
fieldName = "globalid"

# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(r'C:\Users\Dbuehler\AppData\Roaming\ESRI\Desktop10.7\ArcCatalog\GIS Editor to COMGIS3.sde')

# Edit session is started without an undo/redo stack for versioned data
#  (for second argument, use False for unversioned data)
edit.startEditing(False,False)

# Start an edit operation
edit.startOperation()


# create a list of field names which are in both featureclasses
flds = createMatchingFieldList(source, destination)

sourceValues = getPrimaryFieldValues(source, fieldName)
destinationValues = getPrimaryFieldValues(destination, fieldName)

additions = diff(sourceValues, destinationValues)
#deletions = diff(destinationValues, sourceValues)

with arcpy.da.InsertCursor(destination, flds) as insertCursor:
    for a in additions:
        where = createWhereClause(source, fieldName, a)
        insertRows = getSelectCursor(source, flds, where)
        for r in insertRows:
            insertCursor.insertRow(r)

#for d in deletions:
#    where = createWhereClause(destination, fieldName, d)
#    with arcpy.da.UpdateCursor(destination, flds, where) as deleteCursor:
#        for d in deleteCursor:
#            deleteCursor.deleteRow()

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)

print "Success"&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 28 Jul 2021 16:28:34 GMT</pubDate>
    <dc:creator>DavidBuehler</dc:creator>
    <dc:date>2021-07-28T16:28:34Z</dc:date>
    <item>
      <title>Compare two tables and copy over missing records</title>
      <link>https://community.esri.com/t5/python-questions/compare-two-tables-and-copy-over-missing-records/m-p/1083580#M61880</link>
      <description>&lt;P&gt;Try this again -&lt;/P&gt;&lt;P&gt;Workflow is similar to my original solution -&amp;nbsp;&lt;A href="https://community.esri.com/t5/data-management-questions/comparing-two-feature-classes-and-updating-one-of-them/m-p/247407#M14075" target="_blank"&gt;https://community.esri.com/t5/data-management-questions/comparing-two-feature-classes-and-updating-one-of-them/m-p/247407#M14075&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This time it is do an end around the versioning issue with a Survey123 table (backend in SDE).&amp;nbsp; To my understanding based on this article Survey123 based feature services (tables) in SDE cannot be versioned-&amp;nbsp;&lt;A href="https://doc.arcgis.com/en/survey123/desktop/create-surveys/survey123withexistingfeatureservices.htm#:~:text=The%20data%20cannot%20be%20versioned,before%20enabling%20attachments%20on%20either" target="_blank"&gt;https://doc.arcgis.com/en/survey123/desktop/create-surveys/survey123withexistingfeatureservices.htm#:~:text=The%20data%20cannot%20be%20versioned,before%20enabling%20attachments%20on%20either&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;Let's say I have a related table to versioned sanitary lines, the related table has to be versioned as well.&amp;nbsp; Since Survey123 cannot handle versioned data, I cannot push to that table directly.&amp;nbsp; I think I need one table to service the Survey123 form, and the the other to fulfill the relationship.&amp;nbsp; My thinking was compare the two tables and move the new records from Survey123 to the related table, based on globalIDs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I have come up with thus far.&amp;nbsp; Not sure if I have to do an edit session (I think I do since one of them is versioned).&amp;nbsp; I also get this error:&amp;nbsp; Message File Name Line Position&lt;BR /&gt;Traceback&lt;BR /&gt;&amp;lt;module&amp;gt; C:\Users\Dbuehler\Desktop\scripts\TryUpdatingWW.py 74&lt;BR /&gt;RuntimeError: Underlying DBMS error [[Microsoft][ODBC Driver 17 for SQL Server]Syntax error, permission violation, or other nonspecific error] [COMGIS3.GIS.SanCleanings_Test]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help will be much appreciated,&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import sys
import os


print "Looping through and adding and deleting"

#Looping through each feature class and building a list of comparsions. Then adds or deletes them to the SDE feature class.
def createMatchingFieldList(fc1, fc2):
    lst1 = [fld.name for fld in arcpy.ListFields(fc1)]
    lst2 = [fld.name for fld in arcpy.ListFields(fc2)]
    return list(set(lst1) &amp;amp; set(lst2))

def createWhereClause(fc, fld_name, value):
    if len(arcpy.ListFields(fc, fld_name)) == 1:
        fld = arcpy.ListFields(fc, fld_name)[0]
        if fld.type == "String":
            where = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(fc, fld_name), value)
        else:
            where = "{0} = {1}".format(arcpy.AddFieldDelimiters(fc, fld_name), value)
    return where

def getPrimaryFieldValues(fc, field):
    return [r[0] for r in arcpy.da.SearchCursor(fc, [field])]

def getSelectCursor(fc, flds, whereClause):
    return arcpy.da.SearchCursor(fc, flds, whereClause)

def diff(a, b):
    return list(set(a) - set(b))

#source = arcpy.GetParameterAsText(0)
#destination = arcpy.GetParameterAsText(1)
#fieldName = arcpy.GetParameterAsText(2)

source = 'Database Connections/GIS Editor to COMGIS3.sde/COMGIS3.GIS.SanCleanings_Test'
destination = 'Database Connections/GIS Editor to COMGIS3.sde/COMGIS3.GIS.SanCleanings_Test2'
fieldName = "globalid"

# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(r'C:\Users\Dbuehler\AppData\Roaming\ESRI\Desktop10.7\ArcCatalog\GIS Editor to COMGIS3.sde')

# Edit session is started without an undo/redo stack for versioned data
#  (for second argument, use False for unversioned data)
edit.startEditing(False,False)

# Start an edit operation
edit.startOperation()


# create a list of field names which are in both featureclasses
flds = createMatchingFieldList(source, destination)

sourceValues = getPrimaryFieldValues(source, fieldName)
destinationValues = getPrimaryFieldValues(destination, fieldName)

additions = diff(sourceValues, destinationValues)
#deletions = diff(destinationValues, sourceValues)

with arcpy.da.InsertCursor(destination, flds) as insertCursor:
    for a in additions:
        where = createWhereClause(source, fieldName, a)
        insertRows = getSelectCursor(source, flds, where)
        for r in insertRows:
            insertCursor.insertRow(r)

#for d in deletions:
#    where = createWhereClause(destination, fieldName, d)
#    with arcpy.da.UpdateCursor(destination, flds, where) as deleteCursor:
#        for d in deleteCursor:
#            deleteCursor.deleteRow()

# Stop the edit operation.
edit.stopOperation()

# Stop the edit session and save the changes
edit.stopEditing(True)

print "Success"&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Jul 2021 16:28:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/compare-two-tables-and-copy-over-missing-records/m-p/1083580#M61880</guid>
      <dc:creator>DavidBuehler</dc:creator>
      <dc:date>2021-07-28T16:28:34Z</dc:date>
    </item>
  </channel>
</rss>

