Cant drop db in single user mode? - sql

I have tried
exec sp_dboption 'hipp_website_uat', 'single user', 'FALSE'
but i get
Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'sp_dboption'.
I have also tried setting the db to multi user in the db properties but I got
Does anyone know how I can drop this db or how to set it to multi-user mode?

I think you need to just get the users off - there is a 'Close Active Connections' option.
Have you tried checking that and then trying again ?
If you have sysadmin you can
EXEC sp_who2
and then kill the spids that are connected to the database.

Related

How to resolve "...Error occurred during the execution of xp_cmdshell"?

The engineers uses this Access based app that takes data from SQL Server 2005.
The users download a file containing details of parts etc make amendments and upload it again. When the file is upload(checked-in) the information such as the modified date, userId etc is stored in SQL Server. When the user tries to check in they face this error:
The users that are working from home and use remote desktop connection to login into their account are the only one facing this error. The users that are on the office network do not get this error.
I tried making a proxy account and granting access to the user but that doesn't work.
It below query, I get the following errors:
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'GRANT'.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'SIDNEY\UsersWindowsLoginId'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'UsersWindowsLoginId'`
Query:
CREATE LOGIN UsersWindowsLoginId
GRANT EXECUTE ON xp_cmdshell TO 'SIDNEY\UsersWindowsLoginId';
EXEC sp_xp_cmdshell_proxy_account 'UsersWindowsLoginId'
USE master;
GRANT CONTROL SERVER TO 'UsersWindowsLoginId'
GO
Also, this seems to be a recent phenomenon as the users have been working from home for a while now.
Any help is appreciated. Thanks!
First enable advanced option for run xp_cmdshell, for this need to change 1, below query to enable.Try this
USE [DatabaseName]
GO
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
Thanks and Regards
Aravind

SQl 2012 dead lock process by SA

Have a SQL Server 2012 with a SCCM database.
Problem I'm having is we have index errors.
Trying to run
DBCC CHECKDB (CM_KRW, REPAIR_ALLOW_DATA_LOSS)
But getting errors so I had to switch to single user.
Then I ran the command again and it finally worked after several other error messages saying multiple users were connected to the DB.
Now I need to change the DB back to Multi User:
Using:
ALTER DATABASE CM_KRW
SET MULTI_USER
But am seeing the error
Msg 1205, Level 13, State 68, Line 2
Transaction (Process ID 53) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
Msg 5069, Level 16, State 1, Line 2
ALTER DATABASE statement failed.
I ran a SQL Server Profiler trace log for deadlocks and i see lots of them all comming from SA account, for which i dont have the password and account is set to deny login.
Any ideas what I can do now?

SQL Server 2008 R2 Stuck in Single User Mode

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

SQL server restore and user permission

I need to permission a user to a database right after a database restore.
I tried this:
Use [master]
go
restore database DBTest
from disk='E:\userTemp\DBTest1.bak'
WITH MOVE 'DBTest' TO 'E:\SQLData\DBTest1.mdf',
MOVE 'DBTest_log' TO 'F:\SQLData\DBTest1.ldf',
replace, recovery, stats=5, maxtransfersize=1048576
Print '---------------------------RESTORE COMPLETED ---------------------------'
-- Create the user.
CREATE USER [user_indi] FOR LOGIN [user_indi]
GO
USE [DBTest]
GO
EXEC sp_addrolemember N'db_owner', N'user_indi'
GO
When I do this, I get the following error:
Msg 15023, Level 16, State 1, Line 22
User, group, or role 'user_indi' already exists in the current database.
Msg 15410, Level 11, State 1, Procedure sp_addrolemember, Line 75
User or role 'user_indi' does not exist in this database.
So, what added the command to remove the user in the middle. Now it looks like this.
Use [master]
go
restore database DBTest
from disk='E:\userTemp\DBTest1.bak'
WITH MOVE 'DBTest' TO 'E:\SQLData\DBTest1.mdf',
MOVE 'DBTest_log' TO 'F:\SQLData\DBTest1.ldf',
replace, recovery, stats=5, maxtransfersize=1048576
Print '---------------------------RESTORE COMPLETED ---------------------------'
-- Remove the user
USE [DBTest]
GO
DROP USER [user_indi]
GO
-- Create the user.
CREATE USER [user_indi] FOR LOGIN [user_indi]
GO
USE [DBTest]
GO
EXEC sp_addrolemember N'db_owner', N'user_indi'
GO
Now, I get the error:
Msg 15151, Level 16, State 1, Line 2
Cannot drop the user 'user_indi', because it does not exist or you do not have permission.
I can't allow errors as I need to schedule this restore and permission job. Why am I getting this error and how can I workaround it?
RM
Your USE [DBTest] statements are in two different places.
In the first one, you're attempting to create the user in MASTER. In the second one, you're creating it in DBTest.

Database name change SQL Fails Sql Server 2008

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.