SQL Server view process blocking and processing

Source: Internet
Author: User

Back up before modifying or deleting data, back up first, back up (important things say three times)!

1, first, look at the thread, analyze whether there is a blocking process, blocked>0 are currently blocked processes
SELECT * from sysprocesses where blocked >0 order by blocked;

2. After locating the blocked thread, you want to continue to see who is blocking the process and analyze the source that caused the blocking

SELECT * from sysprocesses WHERE spid =spid_no (This is the process ID you want to parse)

3. See which SQL this process is executing, find the cause of the problem
DBCC INPUTBUFFER (SPID);

4. If you want to end the current process, kill the SQL that currently causes blocking
Kill spid

The above is the manual analysis, the current production of what is blocking and automatic analysis of the source, I will provide a self-organized automated analysis process, pro-test effective, can be directly used:

Use master
Go
declare @spid int, @bl int
DECLARE S_cur CURSOR for
Select 0, blocked
From (SELECT * from sysprocesses where blocked>0) a
Where NOT EXISTS (SELECT * from sysprocesses where blocked>0) b
where A.blocked=spid)
Union select spid,blocked from sysprocesses where blocked>0
OPEN S_cur
FETCH NEXT from S_cur to @spid, @bl
While @ @FETCH_STATUS = 0
Begin
If @spid =0
Select ' Causes the database deadlock to be:
' + CAST (@bl as VARCHAR (10)) + ' process number, which executes the SQL syntax as follows '
Else
Select ' Process number spid: ' + CAST (@spid as VARCHAR (10)) + ' by ' + '
Process number spid: ' + CAST (@bl as VARCHAR (10)) + ' block, whose current process executes the SQL syntax as follows '
DBCC InputBuffer (@bl)
FETCH NEXT from S_cur to @spid, @bl
End
CLOSE S_cur
Deallocate s_cur

SQL Server view process blocking and processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.