Having executed a DB deploy (from a VS SQL Server database project) on a local database, which failed, the database has been left in a state where it has single user mode left on (the deploy runs as single user mode).
When I connect to it from SSMS and try something like the following:
ALTER DATABASE MyDatabase
SET MULTI_USER;
GO
I get the error:
Changes to the state or options of database 'MyDatabase' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it.
I tried taking the database offline, which SSMS tells me succeeds, but it doesn't appear to actually do anything. So far, I've only been able to get around this by dropping and recreating the database (which is kind of okay, because it's only a local test database). However, I'd like to be able to reset the status.
How can I convince SQL Server to take this database out of single user mode?
In first run following query in master database
exec sp_who
If you can't find the culprit, try
SELECT request_session_id FROM sys.dm_tran_locks
WHERE resource_database_id = DB_ID('YourDatabase')
Then kill all process that use your database with following query:
KILL spid
Then run following query:
USE Master
ALTER DATABASE YourDatabase SET MULTI_USER
Try the below commands
First run these three commands
USE [master]
SET DEADLOCK_PRIORITY HIGH
exec sp_dboption MyDBName, 'single user', 'FALSE';
Second run these two commands
ALTER DATABASE MyDBName SET MULTI_USER WITH NO_WAIT
ALTER DATABASE MyDBName SET MULTI_USER WITH ROLLBACK IMMEDIATE
This was answered here, the code is:
use master
ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
--do you stuff here
ALTER DATABASE YourDatabase SET MULTI_USER
Use DAC (Dedicated Admin Connection). Make sure you have enabled it first
In SSMS type in admin: for Server Name
after connecting to master ALTER DATABASE SET MULTI_USER
To force the update use " with rollback immediate"
ALTER DATABASE [DATABASE_NAME] SET MULTI_USER with rollback immediate
Related
I need help with setting a database in Single User Mode, Every time i run below code, But it's taking endless process
USE {Database_Name}
ALTER DATABASE {Database_Name} SET SINGLE_USER;
GO
DBCC CHECKDB({Database_Name},REPAIR_REBUILD)
GO
ALTER DATABASE {Database_Name} SET MULTI_USER;
GO
Below SQL set the database to SINGLE_USER mode to obtain exclusive access. Initially It sets database to READ_ONLY and returns access to the database to all users.
The termination option WITH ROLLBACK IMMEDIATE is specified in the first ALTER DATABASE statement. This will cause all incomplete transactions to be rolled back and any other connections to the database to be immediately disconnected.
Example SQL:
USE master
GO
ALTER DATABASE DB_Name
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
DBCC CHECKDB('database_name', REPAIR_REBUILD)
GO
ALTER DATABASE DB_Name
SET READ_ONLY;
GO
If you want to set Database back to Multi_suer, use below SQL:
ALTER DATABASE DB_name
SET MULTI_USER;
GO
For more details check this link: How to set a Database to Single-user Mode
I am using this query to rename the database:
ALTER DATABASE BOSEVIKRAM MODIFY NAME = [BOSEVIKRAM_Deleted]
But it shows an error when excuting:
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.
Is anything wrong with my query?
You could try setting the database to single user mode.
https://stackoverflow.com/a/11624/2408095
use master
ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE BOSEVIKRAM MODIFY NAME = [BOSEVIKRAM_Deleted]
ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER
Set the database to single mode:
ALTER DATABASE dbName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Try to rename the database:
ALTER DATABASE dbName MODIFY NAME = NewName
Set the database to Multiuser mode:
ALTER DATABASE NewName
SET MULTI_USER WITH ROLLBACK IMMEDIATE
In SQL Server Management Studio (SSMS):
You can also right click your database in the Object Explorer and go to Properties. From there, go to Options. Scroll all the way down and set Restrict Access to SINGLE_USER. Change your database name, then go back in and set it back to MULTI_USER.
Try to close all connections to your database first:
use master
ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE BOSEVIKRAM MODIFY NAME = [BOSEVIKRAM_Deleted]
ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER
Taken from here
This did it for me:
USE [master];
GO
ALTER DATABASE [OldDataBaseName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
EXEC sp_renamedb N'OldDataBaseName', N'NewDataBaseName';
-- Add users again
ALTER DATABASE [NewDataBaseName] SET MULTI_USER
GO
1.database set 1st single user mode
ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE
2.RENAME THE DATABASE
ALTER DATABASE BOSEVIKRAM MODIFY NAME = [BOSEVIKRAM_Deleted]
3.DATABAE SET MULIUSER MODE
ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER WITH ROLLBACK IMMEDIATE
That's because someone else is accessing the database. Put the database into single user mode then rename it.
This link might help:
http://msdn.microsoft.com/en-IN/library/ms345378(v=sql.105).aspx
and also:
http://msdn.microsoft.com/en-us/library/ms345378.aspx
Change database to single user mode as shown in the other answers
Sometimes, even after converting to single user mode, the only connection allowed to the database may be in use.
To close a connection even after converting to single user mode try:
select * from master.sys.sysprocesses
where spid>50 -- don't want system sessions
and dbid = DB_ID('BOSEVIKRAM')
Look at the results and see the ID of the connection to the database in question.
Then use the command below to close this connection (there should only be one since the database is now in single user mode)
KILL connection_ID
Replace connection_id with the ID in the results of the 1st query
For me the reason why I could not rename a database is because there are active connections.
I just take the database offline first, ticking the Drop All Active Connections.
Then bring it online again and I can rename the database already.
Another way to close all connections:
Administrative Tools > View Local Services
Stop/Start the "SQL Server (MSSQLSERVER)" service
use master
ALTER DATABASE BOSEVIKRAM SET SINGLE_USER WITH ROLLBACK IMMEDIATE
exec sp_renamedb 'BOSEVIKRAM','BOSEVIKRAM_Deleted'
ALTER DATABASE BOSEVIKRAM_Deleted SET MULTI_USER
I have a database 'My Database' which I would like to rename so that there is no white space. I tried to rename it using
use master
exec sp_renamedb 'I 3 SCI Study','I3SciStudy'
and was greeted with the error
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.
This server is my local machine and I have no other query windows open but the window in which I ran the rename query. Is there some sort of close connection command that I need to run before I can rename the database?
Try this command, but caution is advised:
USE master;
ALTER DATABASE [dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE [dbname] SET MULTI_USER;
GO
Also you can interrogate information about currently active lock manager resources.
SELECT *
FROM sys.dm_tran_locks DTL
WHERE DTL.[resource_database_id] = DB_ID()
Each row represents a currently active request to the lock manager for a lock that has been granted or is waiting to be granted. You will see not only your request on current database(most likely with resoure_type DATABASE). It is impossible to change a database name while these resources are locked
Use SSMS to rename the database, you shouldn't have any problem doing it that way.
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
In Object Explorer, connect to an instance of the SQL Server 2005 Database Engine, and then expand that instance.
Right-click the database to change, and then click Properties.
In the Database Properties dialog box, click the Options page.
From the Restrict Access option, select Single.
If other users are connected to the database, an Open Connections message will appear. To change the property and close all other connections, click Yes.*
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.
I have a Microsoft SQL server 2005 and I tried to enable Broker for my database with those T-SQL:
SELECT name, is_broker_enabled FROM sys.databases
-- checking its status 0 in my case
ALTER DATABASE myDatabase SET ENABLE_BROKER
The Alter Database takes long time to process. It is now over half hour and it is still running. Not sure if it is waiting for something else or I have to clean up anything first, such as delete all the messages, contract, queue and services under service broker?
http://rusanu.com/2006/01/30/how-long-should-i-expect-alter-databse-set-enable_broker-to-run/
alter database [<dbname>] set enable_broker with rollback immediate;
USE master;
GO
ALTER DATABASE Database_Name
SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE;
GO
USE Database_Name;
GO
Enabling SQL Server Service Broker requires a database lock. Stop the SQL Server Agent and then execute the following:
USE master ;
GO
ALTER DATABASE [MyDatabase] SET ENABLE_BROKER ;
GO
Change [MyDatabase] with the name of your database in question and then start SQL Server Agent.
If you want to see all the databases that have Service Broker enabled or disabled, then query sys.databases, for instance:
SELECT
name, database_id, is_broker_enabled
FROM sys.databases
Actually I am preferring to use NEW_BROKER ,it is working fine on all cases:
ALTER DATABASE [dbname] SET NEW_BROKER WITH ROLLBACK IMMEDIATE;