· 8 years ago · Apr 13, 2018, 12:46 AM
1CREATE TABLE dbo.Customer
2(
3 RowId bigint IDENTITY(1,1) NOT NULL,
4 CustomerId guid NOT NULL,
5 Name varchar(255) NOT NULL,
6 CONSTRAINT PK_RowId PRIMARY KEY CLUSTERED([RowId] ASC)
7)
8create unique nonclustered index [UN_CustomerId] ON [dbo].[Customer] ([CustomerId] ASC) include (Name)
9create nonclustered index [UN_Name] ON [dbo].[Customer] ([Name] ASC) include (CustomerId)
10
11
12IF NOT EXISTS
13(
14 SELECT top 1 customerid
15 FROM dbo.Customer WITH (UPDLOCK, SERIALIZABLE)
16 WHERE Name = @Name
17)
18BEGIN
19 INSERT INTO dbo.Customer(CustomerId, Name) VALUES (@CustomerId, @Name)
20 SELECT @CustomerId
21END
22ELSE
23BEGIN
24 SELECT CustomerId FROM dbo.Customer WHERE Name = @Name
25END