# SQL SERVE数据库性能查询命令

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/10443.html | 发布：2022-09-23 | 更新：2022-09-23 | 分类：建站教程、投稿分享

1、查看SQL SERVER数据库的连接数

 

SELECT *

 

FROM master.dbo.sysprocesses

 

where dbid=DB_ID('OnlineExam')

 

![SQL SERVE数据库性能查询命令](https://www.mfisp.com/wp-content/uploads/2022/09/10443-vLfJ2p.png) OnlineExam是自己的数据库名称

 

2、查询某个数据库用户的连接情况

 

sp_who 'sa'

 

![SQL SERVE数据库性能查询命令](https://www.mfisp.com/wp-content/uploads/2022/09/10443-1Wse8i.png)

 

3、查看数据库允许的最大连接

 

select @@MAX_CONNECTIONS

 

![SQL SERVE数据库性能查询命令](https://www.mfisp.com/wp-content/uploads/2022/09/10443-r9sPyV.png)

 

4、查看数据库自上次启动以来的连接次数

 

SELECT @@CONNECTIONS

 

![SQL SERVE数据库性能查询命令](https://www.mfisp.com/wp-content/uploads/2022/09/10443-34GTkY.png)

 

5、关闭连接

 

上面的查询可以得到spid，根据spid，关闭进程就可以了。

 

Kill 54

 

6、SQL SERVER 2008 删除某个数据库的所有连接进程

 

declare @spid int ;

 

declare @ddlstring nvarchar(max);

 

declare @dbname varchar(200);

 

set @dbname='OnLineExam';

 

declare tmpcur cursor

 

for select distinct spid as spid fromsys.sysprocesses

 

where dbid=db_id(@dbname) ;

 

OPEN tmpcur;

 

fetch tmpcur into @spid ;

 

while (@@FETCH_STATUS=0)

 

begin

 

set @ddlstring=N'Kill '+CONVERT(nvarchar,@spid) ;

 

execute sp_executesql @ddlstring ;

 

fetch tmpcur into @spid ;

 

end ;

  

close tmpcur ;

 

deallocate tmpcur ;

 

![SQL SERVE数据库性能查询命令](https://www.mfisp.com/wp-content/uploads/2022/09/10443-BNbvi7.png)

 

![SQL SERVE数据库性能查询命令](https://www.mfisp.com/wp-content/uploads/2022/09/10443-AFpdq8.png)

 

7、查询当前数据库连接并清空所有的数据库连接

 

查询数据库实例DBName的所有连接

 

select * from master.dbo.sysprocesses where dbid = DB_ID(‘DBName’)

 

清除指定实例DBName的所有连接

 

USE master  
go

 

declare @programName nvarchar(200),  
@spid nvarchar(20)

 

declare cDblogin cursor for  
select cast(spid as varchar(20)) AS spid from master..sysprocesses where dbid=db_id(‘db_id_demo’)  
open cDblogin  
fetch next from cDblogin into @spid  
while @@fetch_status=0  
begin  
–防止自己终止自己的进程  
–否则会报错不能用KILL 来终止您自己的进程  
IF @spid <> @@SPID  
exec( ‘kill ‘+@spid)  
fetch next from cDblogin into @spid  
end  
close cDblogin  
deallocate cDblogin

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
