Friday, March 30, 2012

mssql 2000 kill all user connection for restore

i need a sql statment or procedure to kill all user connection to one database.
And Deny new connection for a little time.
So i can a restore this database from a dump.

I can this do only in exclusiv modus. without any other connection.

Can anybody help me??

thanks and best regards from Berlin, Germanydeclare @.l_spid varchar(4)
,@.l_hostname varchar(20)
,@.dbname varchar(256)

select @.dbname = 'YOUR DATABASE NAME HERE'
declare kill_cursor scroll cursor
for
select convert(varchar(4), spid), hostname from master..sysprocesses with (nolock)
where db_name(dbid) = @.dbname

open kill_cursor
select @.@.cursor_rows

fetch next from kill_cursor into
@.l_spid
,@.l_hostname
while (@.@.fetch_status = 0 )
begin
select @.l_hostname Killed
exec ( 'kill ' + @.l_spid)
fetch next from kill_cursor into
@.l_spid
,@.l_hostname
end
close kill_cursor
deallocate kill_cursor
RESTORE STATEMENT HERE|||thanks i can use the code in my procedure.

Cu ... trapezz|||alter database DBName
set SINGLE_USER with rollback immediate|||I prefer

ALTER DATABASE dbName SET SINGLE_USER WITH ROLLBACK IMMEDIATE

No comments:

Post a Comment