我找到了这个例子,以避免在SP仍在运行时运行SP:
CREATE PROCEDURE some_sp AS
DECLARE @ret int,
@lockresource sysname
SELECT @lockresource = object_name(@@procid)
BEGIN TRY
EXEC @ret = sp_getapplock @lockresource, @LockMode = 'Exclusive',
@LockOwner = 'Session', @LockTimeout = 5
IF @ret <> 0
BEGIN
RAISERROR ('Another instance of the procedure is already running, 16, 1)
RETURN
END
-- Do stuff
EXEC sp_releaseapplock @lockresource, 'Session'
END TRY
BEGIN CATCH
IF @@trancount > 0 ROLLBACK TRANSACTIOn
EXEC sp_releaseapplock @lockresource, 'Session'
END CATCH
但我不确定这是不是我要找的。 p>
如果SP正在运行,那么@ret的价值是什么?
EXEC @ret = sp_getapplock @lockresource, @LockMode = 'Exclusive',
@LockOwner = 'Session', @LockTimeout = 5
如果它没有运行,它的价值是什么?
我已查看此page,但我不理解。
答案 0 :(得分:2)
如果没有授予锁定,它将返回-1,等待5毫秒后锁定请求超时