防止用户同时使用一个存储过程

create proc mytest
as
begin
 DECLARE @res INT
    EXEC @res = sp_getapplock
                @Resource = 'mytest',
                @LockMode = 'Exclusive',
    @LockOwner = 'Session'
 if @res = 1
 begin
  EXEC @res = sp_releaseapplock
                            @Resource = 'mytest',
       @LockOwner = 'Session'
   return;
 end
 PRINT 'get lock'
 waitfor delay '00:01'
        EXEC @res = sp_releaseapplock
                            @Resource = 'mytest',
       @LockOwner = 'Session'
        PRINT 'release lock'
end

原文地址:https://www.cnblogs.com/accumulater/p/6146805.html