一剑飞虹

道可道非常道,名可名非常名
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

数据库大并发下的检查唯一值插入.

Posted on 2007-02-07 08:40  greatqn  阅读(765)  评论(3编辑  收藏  举报

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中完成.这样再大的并发也不怕了.