1. 程式人生 > >sqlserver 資料庫設定單使用者訪問後轉換回多使用者(實用可行)

sqlserver 資料庫設定單使用者訪問後轉換回多使用者(實用可行)

declare @spid int ;
declare @ddlstring nvarchar(max);
declare @dbname varchar(200);
set @dbname='BaseComponents';#資料庫名
declare tmpcur cursor 
for select distinct spid as spid from sys.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 ;