Skip to content

Instantly share code, notes, and snippets.

@dflima
Created November 12, 2012 18:37
Show Gist options
  • Save dflima/4061064 to your computer and use it in GitHub Desktop.
Save dflima/4061064 to your computer and use it in GitHub Desktop.
Matando processos no SQL Server.
/*
* Como matar processos no SQL Server
* e ter exclusividade na base
*
* Fonte: http://sqldicas.com.br/dicas/matando-processos-no-sql/
*/
declare @spid int
declare @db_name varchar(100)
set @db_name = 'NomeDaBase' -- coloque o nome da base aqui
declare spid cursor for
select spid
from master.dbo.sysprocesses(nolock)
where dbid = db_id(@db_name) and spid > 50
union
select distinct request_session_id
from sys.dm_tran_locks (nolock)
where resource_database_id = db_id(@db_name) and request_session_id > 50
open spid
fetch next from spid into @spid
while @@fetch_status = 0
begin
exec ('kill ' + @spid)
fetch next from spid into @spid
end
close spid
deallocate spid
USE @db_name
GO
ALTER DATABASE @db_name SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE @db_name SET MULTI_USER
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment