I was trying to detach the DB when it gave me an error that it is currently in use. Hence, I tried to take my DB offline but it failed saying
'an exception occured while executing a transact SQL statement or batch
-> ALTER DATABASE failed because a lock could not be placed on database 'myDB'. Try again later.
ALTER DATABASE statement failed. (Microsoft SQL Server, Error: 5061)'
Now if I try and access the DB it says it is not accessible. What can I do to make my DB accessible again?
My aim was to detach the DB, relocate its secondary database file to a new drive and reattach it (simply because of space issues).
Try following steps.
Restart the SQL server service using services.msc console.
Now connect to your server using SQL Server Management Studio.
Run following command in query analyzer
ALTER DATABASE `YOURDATABASE_NAME`
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Now Right-click the database name, point to Tasks, and then click Detach. The Detach Database dialog box appears
OR
5. Run your command to Relocate the secondary database.
Set the database mode to multi user again
ALTER DATABASE `YOURDATABASE_NAME` SET MULTI_USER
Hope this helps.
As an alternative to step one in Furqan's answer, you might not need to restart the SQL Server instance, but only the SQL Server Management Studio instance, which was used to initiate the "Take Offline" task.
Related
I accidentally dropped SQL Server database connection in SQL Server Management Studio. After i dropped the Connection and pushed "OK" the database was put into single user mode and then the database was taken offline and not shown in the object-explorer.
Is there any Point to rettach the database? I dont find any .BAK Datas or the Databasename in the Restore Options.
I tryed already, but get errors
USE master;
GO
ALTER DATABASE MyDB SET MULTI_USER
GO
I hope there is something i can do. Thx
EDIT:
Steps i did right Click on the Database -> Task -> Detach , than i was in the following Windows and clicked on "Drop Conections" and clicked "OK" and than she went into single user mode and went offline. But after a refresh i didnt saw her in my object Explorer. I followed this steps:
Getting exclusive access to restore SQL Server databases
so any way to get into single user mode to get her back?
See if any *.mdf files exist in file system for the dropped database. If you find, restore.
I'm new to CDC so I started investigating the stored procedures that enable CDC in an SQL Server database. Examining sys.sp_cdc_enable_db and I get the message Could not find stored procedure sys.sp_cdc_enable_db_internal. I've looked all over my database and in the master and msdb databases. Please could anyone tell me why SSMS can't find this stored procedure?
The feature is available only in SQL Server Enterprise and Developer editions, starting with. It can be enabled only using system stored procedures. SQL Server Management Studio provides a wide range of code templates for various feature related actions
To open the templates:
In the SQL Server Management Studio menu, open View
Click Template
Open SQL Server Templates
Open the Change Data Capture sub-folder. The T-SQL templates for administration, configuration, enumeration and meta data querying are available
To set up the feature:
Make sure SQL Server Agent is running. If not, right-click it in Object Explorer and click Start
To enable the feature on the database, open the Enable Database for CDC template in the Configuration sub-folder, and replace the database name with the name of the database you want to track
USE AdventureWorks2012
GO
EXEC sys.sp_cdc_enable_db
GO
The login used must have SQL Server sysadmin privileges and must be a db_owner of the database. Otherwise, you’ll get the following error
sg 22830, Level 16, State 1, Procedure sp_cdc_enable_db_internal, Line
193 The failure occurred when executing the command
‘SetCDCTracked(Value = 1)’. The error returned was 15517: ‘Cannot
execute as the database principal because the principal “dbo” does not
exist, this type of principal cannot be impersonated, or you do not
have permission.’. Use the action and error to determine the cause of
the failure and resubmit the request.
Vist For More Information
https://solutioncenter.apexsql.com/enable-use-sql-server-change-data-capture/
I'm following this guide illustrating a code-first approach with Entity Framework core. Migrations were functioning correctly until, at some point, I deleted my .mdf file. Since then, executing Update-Database (to apply my migration) throws the following error:
Database 'DatabaseName' already exists. Choose a different database name.
Where exactly is this database? How can I remove it permanently?
According to this answer, I need to detach my database from Sql Server, but I'm not sure how to do that now. In Sql Server Management studio, If I execute sp_detach_db DatabaseName it throws error The database 'DatabaseName' does not exist. Supply a valid database name.
Thanks in advance.
UPDATE
I see I can also reproduce this database already exists error if I have the database attached in SQL Server Management Studio, and execute the Update-Database command. After I close the management studio, the migration applies without this error. Very confusing to me.
Seems like an instance of LocalDB is still running in the background. Execute the following commands in the console to stop and delete the instance.
sqllocaldb stop
sqllocaldb delete
The Update-Database command should now have no problems.
Please look at the SQL Server Object Explorer (Visual Studio -> View -> SQL Server Object Explorer). If it contains the 'DatabaseName' database then please try to delete it.
If you create your database with SQL (meaning DB already exists), put the line DROP DATABASE [databaseName] at the beginning of the file databaseName.sql
This will delete the whole DB and its definition (schema), now you can start with creating your new DB.
Another guess.. if you have restored the database then your user login will not work anymore because of the orphan. Try to remove user and create it again. you will get the same error if your migration cannot access the database.
It might be a local DB. Try connecting to your local DB
(LocalDb)\MSSQLLocalDB
And seeing if the DB is located there. You may need to delete it from SSMS
Try to modify the number code of 'xxxxx-20170604092712' in Web.config file,
and then 'update-database' again. This worked for me.
I restore my database to another server, To fix this I had to remove from the DB under Security the previous users and create them again
I am using MSSQLLocalDB. I've encountered this issue after restoring the backup of a DB using the Azure Data Studio.
When I connected using Microsoft SQL Management Studio, I've noticed that my DB was in a SINGLE_USER mode (I have not noticed it in the Azure Data Studio).
Changing the mode to MULTIPLE_USER fixed the issue for me:
I have created a database in SQL Server 2012 with mdf and ldf pointing to a external hard drive attached to my machine. I created tables, stored procedures, populated tables, etc. etc.
I removed the hard drive at the end of the day.
Today, when I attached the hard drive and tried to access the DB in Management Studio, I see the name of the database with (Recovery Pending).
What does this mean? I see the mdf and ldf files in the D drive.
What worked for me was to take the database offline*, then back online - no RESTORE DATABASE was necessary in this case, so far as I can tell.
In SQL Server Management Studio:
right-click on the database
select Tasks / Take Offline ... breathe deeply, cross fingers...
right-click on the database again
select Tasks / Take Online
When you removed the drive, you forcefully disconnected the database from the SQL Server service. SQL Server does not like that.
SQL Server is designed by default so that any database created is automatically kept open until either the computer shuts down, or the SQL Server service is stopped. Prior to removing the drive, you should have "Detached" the database, or stopped the SQL Server service.
You "may" be able to get the database running by executing the following command in a query window: RESTORE DATABASE [xxx] WITH RECOVERY;
You could, although I would not normally recommend this, alter the database to automatically close after there are no active connections.
To accomplish this, you would execute the following query:
ALTER DATABASE [xxx] SET AUTO_CLOSE ON WITH NO_WAIT;
Another way that works is to "Restart" the Database Engine. If feasible and/or practical for this server, it may be faster whenever you have several DB in the external drive.
In SQL Server Management Studio:
Attach the external drive
right-click on the database engine : Server Name(SQL Server
12.0.2000 ... etc)
Select "Restart"
Answer Yes when asked if you want to proceed
Below worked for me:
Run SQL Management Studio as Administrator (right click on SQL
Management Studio icon and select 'Run As')
Take database offline
Detach the database using DROP option
Attach the database
If you were using this database with a Web App running on IIS then you may need to restart the IIS Server
Hope this helps someone
If the SQL Server knows that database recovery needs to be run but something is preventing it from starting, the Server marks the db in ‘Recovery Pending’ state. This is different from the SUSPECT state because it cannot be said that recovery is going to fail – it just hasn’t started yet.
Check this thread: How to fix Recovery Pending State in SQL Server Database?
I'm trying to drop a SQL Server database from .NET code. I've tried using the SMO classes but get an exception saying the database is in use.
Then I tried executing a query (opening a SqlConnection, executing a SqlCommand), along the lines of:
ALTER DATABASE foo SET SINGLE_USER WITH ROLLBACK IMMEDIATE
(pause)
DROP DATABASE foo
But still I get an exception saying the database is in use.
How do I do this? (Or, how does SQL Server Management Studio implement the Drop database and close existing connections?)
You need to make sure you're doing it from the master database:
USE master
GO
ALTER DATABASE foo SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
DROP DATABASE foo
GO
Should work. By the way, SQL Server Management Studio allows you to view the script it generates for all command in the GUI. For example, in the "Delete" window, there's a "script" menu item at the top that'll show you the script you can use.
This may be a stupid question, but in your connection string, are you connecting to the database you're trying to drop, or to the Master database on the same server? If you're trying to drop the database you're actively connected to, it would certainly explain the error.
Also, keep in mind that ADO.NET keeps connections open using connection pooling. So even though you aren't actively running queries using a given connection string, connections may still be open.