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