I have a table access by multiple applications with multiple transactions and I want these multiple applications/transactions to get each Primary Key / Identity inserted accordingly by application who execute that transaction. For some reason I can't use SCOPE_IDENTITY that is why my last sort is to use @@IDENTITY. For transactional query I implemented IsolationLevel.Snapshot for each transaction to avoid blocking.
Now my question is, Will each transaction return the @@IDENTITY correctly and accordingly with this scenario.
Ex. I have three query execute simultaneously:
- Transactional1 Query Insert with expected return Identity 100;
- Transactional2 Query Insert with expected return Identity 102;
- Transactional3 Query Insert with expected return Identity 103;
Will it return the Identity accordingly like this? this is my aim
- Transaction1 - 100
- Transaction2 - 102
- Transaction3 - 103
Or it might happen like this? this is I'm afraid to happen
- Transaction1 - 102
- Transaction2 - 103
- Transaction3 - 101
This existing trigger is the reason why SCOPE_IDENTITY does return identity even though I have SCOPE_IDENTITY in my code and query.
ALTER trigger [dbo].[CustomerAddressesInsertVIds] on [dbo].[CustomerAddresses]
instead of insert
as
begin
set nocount on
insert into [dbo].[CustomerAddresses]
([CustomerID], [AddressTypeID], [CustomerAddressID], [AddressNameType], [Name], [ContactID], [Address1], [Address2], [Address3], [City], [County], [State], [Country], [Zip], [Phone1], [Phone2], [Fax1], [Fax2], [CreateDate], [CreateUser], [MaintenanceDate], [MaintenanceUser], [LastOrderDate], [DeleteOnDate], [SyncStatus], [SyncDate], [SyncUser], [ERPID], [CreateCustomerID], [CreateContactID], [MaintenanceCustomerID], [MaintenanceContactID], [Active], [Deleted], [LockUser], [LockSessionID], [LockDate], [InUse], [AddressTypeVId], [CustomerVId])
select
coalesce([CustomerID], (select [CustomerID] from [dbo].[Customers] where [CustomerVId]=inserted.[CustomerVId])), coalesce([AddressTypeID], (select [AddressTypeID] from [dbo].[AddressTypes] where [AddressTypeVId]=inserted.[AddressTypeVId])), [CustomerAddressID], [AddressNameType], [Name], [ContactID], [Address1], [Address2], [Address3], [City], [County], [State], [Country], [Zip], [Phone1], [Phone2], [Fax1], [Fax2], [CreateDate], [CreateUser], [MaintenanceDate], [MaintenanceUser], [LastOrderDate], [DeleteOnDate], [SyncStatus], [SyncDate], [SyncUser], [ERPID], [CreateCustomerID], [CreateContactID], [MaintenanceCustomerID], [MaintenanceContactID], [Active], [Deleted], [LockUser], [LockSessionID], [LockDate], [InUse], coalesce([AddressTypeVId], (select [AddressTypeVId] from [dbo].[AddressTypes] where [AddressTypeID]=inserted.[AddressTypeID])), coalesce([CustomerVId], (select [CustomerVId] from [dbo].[Customers] where [CustomerID]=inserted.[CustomerID]))
from inserted
end