create proc [dbo].[Name_Add]
@Name varchar(50)
as
begin
begin tran
insert Names (Name)
select (@Name) where not exists (select NameId from Names with(HOLDLOCK) where Name = @Name)
commit tran
select NameId,Name from Names with(nolock) where Name = @Name
end
要点:检查,加锁,插入值在一句sql中完成.这样再大的并发也不怕了.