--备份SQL SERVER数据库
BACKUP DATABASE book09 TO DISK ='c:abc.bak' WITH init
--恢复SQL SERVER数据库
USE MASTER
--开始循环,删除所有连接到数据库的连接
DECLARE @count int=0
SELECT @count=COUNT(spid) FROM master..sysprocesses where dbid=db_id('book09')
WHILE (CONVERT(int,@count)>0)
BEGIN
DECLARE @j int=0;
SELECT TOP 1 @j=spid FROM master..sysprocesses where dbid=db_id('book09');
IF(@j<>0) EXEC('kill '+@j)
SELECT @count=COUNT(spid) FROM master..sysprocesses where dbid=db_id('book09')
END
--开始恢复数据库
RESTORE DATABASE book09 FROM DISK='c:abc.bak' WITH REPLACE
--select spid from master..sysprocesses where dbid=db_id('book09')
--KILL 56
--exec sp_who