How to kill connections when restoring from command line. What is the KILL command to gain exclusive use of a database when restoring via cmdline?
It is not possible to kill the SQL connections since SQL processes are not visible outside of SQL Server.
Exclusive use can be accomplished when restoring via script:
---- Kill all current connections
DECLARE@cmdKill VARCHAR(50)
DECLAREkillCursor CURSORFOR
SELECT'KILL '+Convert(VARCHAR(5), p.spid)
FROMmaster.dbo.sysprocesses ASp
WHEREp.dbid =db_id('AdventureWorks')
OPENkillCursor
FETCHkillCursor INTO@cmdKill
WHILE0=@@fetch_status
BEGIN
EXECUTE(@cmdKill)
FETCHkillCursor INTO@cmdKill
END
CLOSEkillCursor
DEALLOCATEkillCursor