I have a sequence in my SQL Server database
CREATE SEQUENCE [dbo].[UserPKSequence]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE -2147483648
MAXVALUE 2147483647
NO CACHE
GO
I am using this sequence on a table (lets name the table Users) that has a Primary Key (lets name that UserId) which is not an IDENTITY column. I am using my sequence as the DEFAULT for UserId, as shown below:
CREATE TABLE [dbo].[Users]
(
[UserId] [INT] NOT NULL
CONSTRAINT [DF_User_UserId] DEFAULT (NEXT VALUE FOR [UserPKSequence]),
[Email] [VARCHAR](254) NULL
-- other columns defined for the table
CONSTRAINT [pkUsers]
PRIMARY KEY CLUSTERED ([UserId] ASC)
) ON [PRIMARY]
The database is used by 2 different applications where both applications can insert data into this table, using the mentioned sequence. Can it cause any concurrency issues? Can one application can get the UserId
generated by the other application - in case of concurrent requests ? if yes, then how? and what can I do to prevent ? I can not make UserId IDENTITY
.
Also when a new row is inserted in the table, a trigger is fired which returns the newly generated UserId
to one of the applications. Can that be problematic?