I have a database trigger on a feature class where if a new feature is inserted, it will also copy that feature into a different feature class. Archiving is enabled on both, so there are GDB_ARCHIVE_OIDs (here forth GAO) associated with each record in the feature classes. The first feature class is smaller than the one it is copying to, so the OID (which becomes the GAO) is a lower number than the feature class it is being copied to. When I attempt to copy the feature to the larger feature class, the GAO prevents the feature being inserted because it's GAO is already in the second feature classes GAOs. How can I find the largest GAO in the second feature class to alter my insert statement to make sure I can insert the feature into the larger table?
I was thinking that anytime a feature is inserted into the table, a GAO would automatically be generated so I wouldn't have to include the GAO in my SQL INSERT statement, but that does not appear to be true when using a database trigger to do this. It errors out saying the GAO field can't be null.
Database trigger:
<SPAN class="keyword token">USE</SPAN> <SPAN class="punctuation token">[</SPAN>FocalAreasTest1<SPAN class="punctuation token">]</SPAN>
GO
<SPAN class="comment token">/****** Object: Trigger [dbo].[copy_point] Script Date: 2/24/2020 1:40:21 PM ******/</SPAN>
<SPAN class="keyword token">SET</SPAN> ANSI_NULLS <SPAN class="keyword token">ON</SPAN>
GO
<SPAN class="keyword token">SET</SPAN> QUOTED_IDENTIFIER <SPAN class="keyword token">ON</SPAN>
GO
<SPAN class="keyword token">ALTER</SPAN> <SPAN class="keyword token">TRIGGER</SPAN> <SPAN class="punctuation token">[</SPAN>dbo<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">[</SPAN>copy_point<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">ON</SPAN> <SPAN class="punctuation token">[</SPAN>FocalAreasTest1<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">[</SPAN>dbo<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">[</SPAN>MONITORINGPOINT_1<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">AFTER</SPAN> <SPAN class="keyword token">INSERT</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">UPDATE</SPAN>
<SPAN class="operator token">NOT</SPAN> <SPAN class="keyword token">FOR</SPAN> <SPAN class="keyword token">REPLICATION</SPAN>
<SPAN class="keyword token">AS</SPAN> <SPAN class="keyword token">DECLARE</SPAN>
<SPAN class="variable token">@MPID</SPAN> uniqueidentifier<SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@State</SPAN> nvarchar<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@Point</SPAN> nvarchar<SPAN class="punctuation token">(</SPAN><SPAN class="number token">100</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@StateObjectID</SPAN> <SPAN class="keyword token">int</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@Route</SPAN> nvarchar<SPAN class="punctuation token">(</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@x_coord</SPAN> <SPAN class="keyword token">numeric</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">38</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@y_coord</SPAN> <SPAN class="keyword token">numeric</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">38</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@notes</SPAN> nvarchar<SPAN class="punctuation token">(</SPAN><SPAN class="number token">150</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@RouteOrder</SPAN> <SPAN class="keyword token">smallint</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@Spring</SPAN> <SPAN class="keyword token">smallint</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@Fall</SPAN> <SPAN class="keyword token">smallint</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@DateAdded</SPAN> datetime2<SPAN class="punctuation token">(</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@DateModified</SPAN> datetime2<SPAN class="punctuation token">(</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@Archive</SPAN> <SPAN class="keyword token">int</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@FRID</SPAN> uniqueidentifier<SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@long</SPAN> <SPAN class="keyword token">numeric</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">11</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@lat</SPAN> <SPAN class="keyword token">numeric</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">11</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">8</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@global</SPAN> uniqueidentifier<SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@SHAPE</SPAN> <SPAN class="keyword token">geometry</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@GDB</SPAN> <SPAN class="keyword token">varbinary</SPAN><SPAN class="punctuation token">(</SPAN>max<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@from</SPAN> datetime2<SPAN class="punctuation token">(</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@to</SPAN> datetime2<SPAN class="punctuation token">(</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@OID</SPAN> <SPAN class="keyword token">int</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@StatePoint</SPAN> nvarchar<SPAN class="punctuation token">(</SPAN><SPAN class="number token">50</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">IF</SPAN> <SPAN class="keyword token">EXISTS</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">Select</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="keyword token">FROM</SPAN> DELETED<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">BEGIN</SPAN>
<SPAN class="comment token">--Do update stuff here</SPAN>
<SPAN class="keyword token">END</SPAN>
<SPAN class="keyword token">ELSE</SPAN>
<SPAN class="keyword token">BEGIN</SPAN>
<SPAN class="keyword token">DECLARE</SPAN> <SPAN class="variable token">@GAO</SPAN> <SPAN class="keyword token">int</SPAN>
<SPAN class="comment token">--This gets the largest GDB_ARCHIVE_OID from the larger</SPAN>
<SPAN class="comment token">--feature class and adds one to it</SPAN>
<SPAN class="keyword token">Select</SPAN> <SPAN class="variable token">@GAO</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">max</SPAN><SPAN class="punctuation token">(</SPAN>mp<SPAN class="punctuation token">.</SPAN>GDB_ARCHIVE_OID<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">From</SPAN> CollectorSpatialData<SPAN class="punctuation token">.</SPAN>dbo<SPAN class="punctuation token">.</SPAN>MONITORINGPOINT mp
<SPAN class="keyword token">INSERT</SPAN> <SPAN class="keyword token">INTO</SPAN> CollectorSpatialData<SPAN class="punctuation token">.</SPAN>dbo<SPAN class="punctuation token">.</SPAN>MONITORINGPOINT <SPAN class="punctuation token">(</SPAN>
MonitoringPointID<SPAN class="punctuation token">,</SPAN>
StateID<SPAN class="punctuation token">,</SPAN>
<SPAN class="keyword token">Point</SPAN><SPAN class="punctuation token">,</SPAN>
StateObjectID<SPAN class="punctuation token">,</SPAN>
Route<SPAN class="punctuation token">,</SPAN>
X_Coord<SPAN class="punctuation token">,</SPAN>
Y_Coord<SPAN class="punctuation token">,</SPAN>
Notes<SPAN class="punctuation token">,</SPAN>
RouteOrder<SPAN class="punctuation token">,</SPAN>
Spring<SPAN class="punctuation token">,</SPAN>
Fall<SPAN class="punctuation token">,</SPAN>
DateAdded<SPAN class="punctuation token">,</SPAN>
DateModified<SPAN class="punctuation token">,</SPAN>
GDB_ARCHIVE_OID<SPAN class="punctuation token">,</SPAN>
FocalRefID<SPAN class="punctuation token">,</SPAN>
long<SPAN class="punctuation token">,</SPAN>
lat<SPAN class="punctuation token">,</SPAN>
StatePoint<SPAN class="punctuation token">,</SPAN>
GlobalID<SPAN class="punctuation token">,</SPAN>
Shape<SPAN class="punctuation token">,</SPAN>
GDB_GEOMATTR_DATA<SPAN class="punctuation token">,</SPAN>
GDB_FROM_DATE<SPAN class="punctuation token">,</SPAN>
GDB_TO_DATE<SPAN class="punctuation token">,</SPAN>
OBJECTID<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">SELECT</SPAN>
MonitoringPointID <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>MonitoringPointID<SPAN class="punctuation token">,</SPAN>
StateID <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>StateID<SPAN class="punctuation token">,</SPAN>
<SPAN class="keyword token">Point</SPAN> <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN><SPAN class="keyword token">Point</SPAN><SPAN class="punctuation token">,</SPAN>
StateObjectID <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>StateObjectID<SPAN class="punctuation token">,</SPAN>
Route <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>Route<SPAN class="punctuation token">,</SPAN>
X_coord <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>X_Coord<SPAN class="punctuation token">,</SPAN>
Y_coord <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>Y_Coord<SPAN class="punctuation token">,</SPAN>
Notes <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>Notes<SPAN class="punctuation token">,</SPAN>
RouteOrder <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>RouteOrder<SPAN class="punctuation token">,</SPAN>
Spring <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>Spring<SPAN class="punctuation token">,</SPAN>
Fall <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>Fall<SPAN class="punctuation token">,</SPAN>
DateAdded <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>DateAdded<SPAN class="punctuation token">,</SPAN>
DateModified <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>DateModified<SPAN class="punctuation token">,</SPAN>
GDB_ARCHIVE_OID <SPAN class="operator token">=</SPAN> <SPAN class="variable token">@GAO</SPAN><SPAN class="punctuation token">,</SPAN>
FocalRefID <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>FocalRefID<SPAN class="punctuation token">,</SPAN>
long <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>long<SPAN class="punctuation token">,</SPAN>
lat <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>lat<SPAN class="punctuation token">,</SPAN>
StatePoint <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>StatePoint<SPAN class="punctuation token">,</SPAN>
GlobalID <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>GlobalID<SPAN class="punctuation token">,</SPAN>
Shape <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>Shape<SPAN class="punctuation token">,</SPAN>
GDB_GEOMATTR_DATA <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>GDB_GEOMATTR_DATA<SPAN class="punctuation token">,</SPAN>
GDB_FROM_DATE <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>GDB_FROM_DATE<SPAN class="punctuation token">,</SPAN>
GDB_TO_DATE <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>GDB_TO_DATE<SPAN class="punctuation token">,</SPAN>
OBJECTID <SPAN class="operator token">=</SPAN> i<SPAN class="punctuation token">.</SPAN>ObjectID
<SPAN class="keyword token">FROM</SPAN> inserted i
<SPAN class="keyword token">END</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>This logic doesn't seem to work because it grabs the smaller feature classes' GAO instead of the larger one. It also does not add 1 to it. This is the error I get where the index it's talking about refers to the primary key GAO in the larger feature class (CollectorSpatialData.dbo.MonitoringPoint). The next GAO in the larger feature class should be 3506.

Is what I'm trying to do impossible?