SELECT *
? FROM master.dbo.sysprocesses
where dbid=DB_ID('OnlineExam')
?OnlineExam是自己的數據庫名稱
?2、查詢某個數據庫用戶的連接情況
sp_who 'sa'
?3、查看數據庫允許的最大連接
select @@MAX_CONNECTIONS
?4、查看數據庫自上次啟動以來的連接次數
SELECT @@CONNECTIONS
?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 ;
?
?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