Adding SQL Trigger to update field on INSERT (multiple rows)

المشرف العام

Administrator
طاقم الإدارة
I have an address point Feature Class that has an addressID field. I am trying to write a trigger that will in effect auto increment this field when a new address is added to the map. I cannot alter the table and I cannot rely on users to populate this field when they edit.

So far, I have written a trigger that works great as long as only 1 edit at a time is made to the address feature class.

The working trigger does a count on the number of records already in the feature class (this includes the inserted record) and then updates the addressID field of the newly inserted record with the count.

However, if there are multiple records inserted (as in the user creates several new addresses and then saves edits) all of those records are given the same addressID.

So what I need is a modified trigger that correctly handles multiple row inserts. Problem is I don't really know how to do this and after much searching and reading many posts regarding the same on stack overflow and other forums, I still can't seem to figure it out!

The trigger that works for single row inserts is below...can anyone offer suggestions that may resolve my issue? Your help is much appreciated!

CREATE TRIGGER trgNewAddressPointTestInsert ON [dbo].[AddressPointTest] FOR INSERT AS UPDATE [dbo].[AddressPointTest] SET addressID = (SELECT COUNT(*) FROM [dbo].[AddressPointTest]) WHERE OBJECTID in (SELECT OBJECTID FROM inserted)

أكثر...
 
أعلى