欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

检查锁定SQL Server数据库的Process ID

程序员文章站 2022-06-04 20:42:41
...

检查锁定SQLServer数据库的ProcessID 无 CREATE PROCEDURE #sp_who_lockASBEGINDECLARE @spid INTDECLARE @blk INTDECLARE @count INTDECLARE @index INTDECLARE @lock TINYINT ??SET @lock = 0 ??DECLARE @temp_who_lock AS TABLE (id INT identity(1, 1),sp

检查锁定SQL Server数据库的Process ID
CREATE PROCEDURE #sp_who_lock
AS
BEGIN
	DECLARE @spid INT
	DECLARE @blk INT
	DECLARE @count INT
	DECLARE @index INT
	DECLARE @lock TINYINT ?
?
	SET @lock = 0 ?
?
	DECLARE @temp_who_lock AS TABLE (
		id INT identity(1, 1),
		spid INT,
		blk INT
		) ?
?
	IF @@error  0
		RETURN @@error ?
?
	INSERT INTO @temp_who_lock (
		spid,
		blk
		)
	SELECT 0,
		blocked
	FROM (
		SELECT *
		FROM master..sysprocesses
		WHERE blocked > 0
		) a
	WHERE NOT EXISTS (
			SELECT TOP 1 1
			FROM master..sysprocesses
			WHERE a.blocked = spid
				AND blocked > 0
			)
	UNION
	SELECT spid,
		blocked
	FROM master..sysprocesses
	WHERE blocked > 0 ?
?
	IF @@error  0
		RETURN @@error ?
?
	SELECT @count = count(1),
		@index = 1
	FROM @temp_who_lock ?
?
	IF @@error  0
		RETURN @@error ?
?
	IF @count = 0
	BEGIN
		SELECT N'没有阻塞和死锁信息' ?
?
		RETURN 0
	END ?
?
	WHILE @index  @index
					AND EXISTS (
						SELECT TOP 1 1
						FROM @temp_who_lock
						WHERE id