So here is my situation. I have data located in a SQL Server 2008 R2 versioned ESRI SDE database which I want to have auto-populate a unique ID for. I have found a script that works well on a single record insert (it works also for multiple records) which will pull the max ID from the tables and add one number to it for the insert.
The problem I have run into is due to the way ESRI works for inserting records. When you create data in your mapping software (ArcMap) every time you hit save it acts as an insert on the table. So if you were to hit save after one record and the max ID is 27 the ID for the new record/data would be 28. Take this case again but this time you created two features before hitting save, this inserts both records into the table at the same time and each record gets an ID of 28 instead of 28 and 29 respectively.
All this being said, I have pasted the code I am using below which works on both the original dataset and the delta table created from it being versioned, but what I need help figuring out is how to use the max ID from the current dataset to populate the next record ID for each record inserted without duplicating it on a multiple record insert. This code is focused on a parcel id for a parcel boundary which in many cases I may have to split a parcel which would result in two features needing a unique ID. Any and all advice is welcomed and appreciated
CREATE TRIGGER [dbo].[PARCEL_AUTO_ID]
ON [dbo].[a405]
FOR INSERT, UPDATE
AS
BEGIN
DECLARE @Max_Value int
SET @Max_Value = (SELECT MAX(PARCEL_ID) FROM [dbo].[TOWN_PARCELS])+1
UPDATE [dbo].[a405]
set PARCEL_ID = @Max_Value
WHERE PARCEL_ID IS NULL
UPDATE [dbo].[TOWN_BUILDING_FOOTPRINTS]
set PARCEL_ID = @Max_Value
WHERE PARCEL_ID IS NULL
END
I figured it out finally. It should look like this.
CREATE TRIGGER [dbo].[PARCEL_AUTO_ID]
ON [dbo].[a405]
FOR INSERT, UPDATE
AS
BEGIN
DECLARE @Max_Value int
SET @Max_Value = (SELECT MAX(PARCEL_ID) FROM [dbo].[a405])+1
UPDATE [dbo].[a405]
set PARCEL_ID = @Max_Value
WHERE PARCEL_ID IS NULL
UPDATE [dbo].[TOWN_BUILDING_FOOTPRINTS]
set PARCEL_ID = @Max_Value
WHERE PARCEL_ID IS NULL
END