Select to view content in your preferred language

Can new records be added to a related table programmatically?

1754
3
09-29-2010 11:55 AM
CarolDoering
Occasional Contributor
I have a feature class with a relationship class that points to a related table in an SDE geodatabase. There are currently over 1000 features in the feature class but no records in the related table. I would like to create one record in the related table for each feature, to which I can then go and bulk update the table field values. The only way I've found to do this is to start an edit session in ArcMap and manually "Add New" to each feature in the feature class one by one. Is anyone aware of a way I can do this programmatically? I've found a tool for deleting rows in a script but not for adding rows to a related table. Since I have several feature classes with this problem (over 3500 features in total) I'd really rather not do them all manually.
0 Kudos
3 Replies
JudsonOldford
Deactivated User
I would use SQL on directly on the underlying database. Try a statement like:

INSERT INTO targettablenonspatial (idfield)
SELECT idfield
FROM Spatialtable;

Select * from targettablenonspatial;


I'd also advise caution if your database is versioned as changing records on the database directly can cause some problems.
0 Kudos
KennethRenner
Emerging Contributor
Does anyone have any information on how to do this through ArcObjects.

I want to do the same thing but I add features programmatically during an import process from an xml file. Depending on the information in the xml file I may or may not have to add a record to the related table.
0 Kudos
LeonScott
Deactivated User
Does anyone have any information on how to do this through ArcObjects.


Here's some mostly complete code that should show you what you are looking for.  It basically creates some rows in the table, then relates eac row to a feature.


'********************************
'** Create Rows in Condo Table **
'********************************
Dim pFeatSel As IFeatureSelection
Dim pSelSet As ISelectionSet
Dim pRowBuff As IRowBuffer
Dim pCondoCursor As ICursor

m_Editor.StartOperation()

pFeatSel = CType(m_FeatureLayer, IFeatureSelection)
pSelSet = pFeatSel.SelectionSet
pSelSet.Search(Nothing, True, pCursor)
pRow = pCursor.NextRow

pRowBuff = m_Table.CreateRowBuffer
pCursor = m_Table.Insert(True)

Do While Not pRow Is Nothing
   pRowBuff.Value(7) = dblValue
   pCursor.InsertRow(pRowBuff)
   pRow = pCursor.NextRow
Loop

m_Editor.StopOperation("Create Table Rows")
pCursor = Nothing
pRowBuff = Nothing
pRow = Nothing

'**************************
'** Create Relationships **
'**************************
Dim pRelateClass As IRelationshipClass
dim pFeatWS as iFeatureWorkspace
dim pOriginFeat as IFeature

'** To do: need to get feature to relate rows in table to
'** To do: need to get Workspace for relationship class
pRelateClass = pFeatWS.OpenRelationshipClass("RelationshipClassName")


Dim pRelate As IRelationship
m_Editor.StartOperation()
pQF = New QueryFilter
pQF.WhereClause = "Fieldname = " & value
pCondoCursor = m_Table.Search(pQF, False)
pRow = pCursor.NextRow

Do While Not pRow Is Nothing
  pRelate = pRelateClass.CreateRelationship(pOriginFeat, pRow)
  pRow = pCursor.NextRow
Loop

pQF = Nothing
m_Editor.StopOperation("Create Relationships")
0 Kudos