only My site

Wednesday, June 27, 2012

Database ID and Kill the process

During resore the database if some process or a connection exists, the SQL Server will not allow us to do the Restore or other tasks. So we have to know the process ID and Database ID to close (kill) the running process.

The below are the code to identify the DB ID, SP ID and demonstrated the KILL syntax.

--SELECT SPID,DBID FROM SYSPROCESSES WHERE DBID NOT IN (1,2,3,4)
--To Know the DB ID
SELECT DB_ID(N'DATABASE_NAME_HERE') AS [Database ID];
SELECT SPID,DBID FROM SYSPROCESSES WHERE DBID = (SELECT DB_ID(N'DATABASE_NAME_HERE'))
KILL 58


No comments: