TSQL存储过程applock应用实例

-- 实际应用中,如果需要某个存储过程一次只能被一个进程调用执行,可以使用applock的功能.如下DEMO实例.
CREATE proc spapl  
as  
begin  
 set nocount on  
 declare @x bit  
 select @x=applock_test('public','spapl','Exclusive','Session')  
 if @x=0  
 begin  
  print 'SP is running.'  
  return  
 end  
   
exec sp_getapplock @Resource = 'spapl', @LockMode = 'Exclusive', @LockOwner = 'Session', @DbPrincipal = 'public'  
  
print 'AP0405140'  
waitfor delay '00:00:10'  
print 'AP0406541'  
   
exec sys.sp_releaseapplock @Resource = 'spapl', @LockOwner = 'Session', @DbPrincipal = 'public'     
end

你可能感兴趣的:(TSQL存储过程applock应用实例)