I would like to know how (and by extension if it's possible) to put SQL Server 2005 in single user mode using SQL statements?
I found these instructions on the MSDN, but alas they require SSMS.
http://msdn.microsoft.com/en-us/library/ms345598(SQL.90,loband).aspx
*To set a database to single-user mode
The following should work:
ALTER DATABASE [MyDatabase] SET SINGLE_USER WITH NO_WAIT
GO
with
ALTER DATABASE [MyDatabase] SET MULTI_USER WITH NO_WAIT
GO
to set it back to multi-user
Try
alter database adventureWorks set SINGLE_USER with rollback immediate
Should you wish to provide ample time for already executing transactions to complete gracefully you can issue the following:
alter database adventureWorks set SINGLE_USER with rollback after 60 seconds
On a per-database basis, you can set single user mode with:
ALTER DATABASE database_name SET SINGLE_USER
If other users are logged in and using the database though, then you'll have to kill their spids first otherwise this will fail. You could write a script that basically SELECTs from sysprocesses
and issues a KILL command for each spid listed against your target dbid.
See msdn
alter database X
set single_user
i found these commands:
ALTER DATABASE [dbName] SET MULTI_USER WITH NO_WAIT
ALTER DATABASE [dbName] SET SINGLE_USER WITH NO_WAIT
or
EXEC sp_dboption 'dbName', 'single user', 'false'
EXEC sp_dboption 'dbName', 'single user', 'true'
at:
http://www.kodyaz.com/articles/alter-single-user-multi-user-mode.aspx
Give them a try.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.