use master gosql
declare @dbname varchar ( 20) set @dbname = 'QADB' declare @sql nvarchar ( 500) declare @spid int --SPID 值是當用戶進行鏈接時指派給該鏈接的一個惟一的整數 set @sql = 'declare getspid cursor for select spid from sysprocesses where dbid=db_id(''' + @dbname + ''')' exec ( @sql ) open getspid fetch next from getspid into @spidfetch
while @@fetch_status <>- 1-- 若是 FETCH 語句沒有執行失敗或此行不在結果集中。rest
begin exec ( 'kill ' + @spid ) -- 終止正常鏈接 fetch next from getspid into @spid end close getspidget
deallocate getspid goast
restore database qadb from disk = 'D:\SqlData\QADB_2017-05-09.bak'select