Home >Database >Mysql Tutorial >找到活动的SQL连接,并杀掉它!【MSSQL】

找到活动的SQL连接,并杀掉它!【MSSQL】

WBOY
WBOYOriginal
2016-06-07 14:56:211127browse

你想知道 SQL Server 正在执行什么 SQL 语句吗?然后顺手把这些正在执行的 SQL 语句停掉吗? 请看下面代码 出处: codeproject SQL Server select db_name(dbid) as [Database Name], count(dbid) as [No Of Connections], loginame as [Login Name]from sys.

你想知道 SQL Server 正在执行什么 SQL 语句吗?然后顺手把这些正在执行的 SQL 语句停掉吗?
请看下面代码
出处: codeproject
SQL Server
select 
    db_name(dbid) as [Database Name], 
    count(dbid) as [No Of Connections],
    loginame as [Login Name]
from
    sys.sysprocesses
where 
    dbid > 0
group by 
    dbid, loginame
set nocount on
declare @databasename varchar(100)
declare @query varchar(max)
set @query = ''

set @databasename = 'xxx'
if db_id(@databasename) < 4
begin
	print 'system database connection cannot be killeed'
return
end

select @query=coalesce(@query,',' )+'kill '+convert(varchar, spid)+ '; '
from master..sysprocesses where dbid=db_id(@databasename)

if len(@query) > 0
begin
print @query
	exec(@query)
end
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn