07-31-2023, 09:54 AM
What I do:
First I kill all database process, to certificate that database isn't in use.
After I alter the user mode.
Sometimes a process is quickly grabed again from an aplication, and I need run this process one more time...
**Replace the expression DATABASE_NAME below, to use your database name.**
**--Kill process**
USE Master
GO
declare @sql as varchar(20), @spid as int
select @spid = min(spid) from master..sysprocesses where dbid = db_id('DATABASE_NAME') and spid != @@spid
while (@spid is not null)
begin
print 'Killing process ' + cast(@spid as varchar) + ' ...'
set @sql = 'kill ' + cast(@spid as varchar)
exec (@sql)
select
@spid = min(spid)
from
master..sysprocesses
where
spid > 50 AND dbid = db_id('DATABASE_NAME')
and spid != @@spid
end
print 'Killing Process completed...'
GO
**--Alter user mode**
ALTER DATABASE DATABASE_NAME SET MULTI_USER;
GO
print 'Alter Database Process completed...'
First I kill all database process, to certificate that database isn't in use.
After I alter the user mode.
Sometimes a process is quickly grabed again from an aplication, and I need run this process one more time...
**Replace the expression DATABASE_NAME below, to use your database name.**
**--Kill process**
USE Master
GO
declare @sql as varchar(20), @spid as int
select @spid = min(spid) from master..sysprocesses where dbid = db_id('DATABASE_NAME') and spid != @@spid
while (@spid is not null)
begin
print 'Killing process ' + cast(@spid as varchar) + ' ...'
set @sql = 'kill ' + cast(@spid as varchar)
exec (@sql)
select
@spid = min(spid)
from
master..sysprocesses
where
spid > 50 AND dbid = db_id('DATABASE_NAME')
and spid != @@spid
end
print 'Killing Process completed...'
GO
**--Alter user mode**
ALTER DATABASE DATABASE_NAME SET MULTI_USER;
GO
print 'Alter Database Process completed...'