我有多个应用程序通过多个事务访问表,我希望这些多个应用程序/事务能够让执行该事务的应用程序相应地插入每个主键/身份。出于某种原因,我不能使用SCOPE_IDENTITY这就是为什么我最后一种是使用@@IDENTITY。对于事务查询,我为每个事务实现了IsolationLevel.Snapshot以避免阻塞。
现在我的问题是,在这种情况下,每个事务是否会正确并相应地返回@@IDENTITY 。
前任。我同时执行三个查询:
- Transactional1 查询插入,预期返回标识 100;
- Transactional2 查询插入,预期返回标识 102;
- Transactional3 查询插入,预期返回标识 103;
它会像这样相应地返回身份吗?这是我的目标
- 交易 1 - 100
- 交易 2 - 102
- 交易 3 - 103
或者它可能会发生这样的事情?这是我害怕发生
- 交易 1 - 102
- 交易 2 - 103
- 交易 3 - 101
这个现有的触发器是 SCOPE_IDENTITY 确实返回身份的原因,即使我的代码和查询中有 SCOPE_IDENTITY。
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