Accidentally dropped SQL Server database connection - sql

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.

Related

Copy database to a new server

Is it possible to copy a database to a new server? I just want to have the database on a new server because I need to make some tests. I didn't have much experience with servers.
Follow this steps :
Run your SSMS
Right click on the database you want to copy
In the Popup menu , go to Taches , then click Offline
Right click on the Database again in the popup menu , Taches -> Detach
Open the folder ..\MSSQL\DATA , copy Yourdb.MDF and YourDB_log.LDF files to the folder of the new Instance.
Now you can Attach both Databases and work on it in both instance.
Another way:
Run Your SSMS
Right click on the Database you want to copy
In the popup menu , Taches -> Copy the database...
Follow the steps on the Wizard
You can select option if you want to keep the database ONLINE when it is being copied.
You can also select option of MOVE or COPY database as well.
When the process is over it will show the success message and database will be copied to another server.
Note:
1- The Copy Database Wizard is not available in the Express edition.
2- The Copy Database Wizard cannot be used to copy or move databases that are/have:
System.
marked for replication.
marked Inaccessible, Loading, Offline, Recovering, Suspect, or in Emergency Mode.
data or log files stored in Microsoft Azure storage.
Also , a database cannot be moved or copied to an earlier version of SQL Server.
Updtae:
If there is no Copy database option , then you can Copy Databases with Backup and Restore.
For more information about it MSDN.
No experience? No problem, backup the database.
Copy it into the target machine.
Restore DB from device >> open the DB backup file.
At this point you have the entire DB without missing any schema objects such as foreign keys.
Finally delete all sql logins that the DB came with and recreate them manually as you test access.
Once you are comfortable with this operation you can go ahead and get more experience using some of the techniques describe above which are recommended if you have a large DB to restore and so on.
How to restore the DB on SQL Server Management Studio
Steps:
1. On “Object Explorer” panel right-click on “Databases” and selected Restore Database
Select “Device” and browse database location.
note: Copy the .bak file locally before restoring.
The DB information will auto-populate the window, you can edit the name of the DB to be restored.
Go to the “Files” page and check the “Relocated all files…” box.
Select existing folder locations for Data and Log files.
HIT OK

MSSQL database on external hard drive shows Recovery Pending

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?

How to change database from Single user mode to multi user

I have a Database,Which is in Single user mode ,If i want to access a tables in the database i will be changing in the properties from single to multiuser.How can i make the Database multi user permenantly?.
ALTER DATABASE [MyDB] SET MULTI_USER
If it throws an error like user is already connected to it, select 'master' db in the dropdown and try it that way.
If that doesn't do it, use sp_who to find what spid is accessing the DB and kill it.
If you want to do it in SSMS object explorer.
Right click on your database.
Go to properties > options.
Scroll to the bottom and find "Restrict Access" and change it to multi_user.
Click ok.
Just an alternative to query window. Both do the same the same.
Other solution.
I have the same error as you, I have tried all the ways but it doesn't work. Manually below I have restored to the original state of the database.
Step 1: Stop Sql Server Services
Step 2: Cut Data and Log file to other folder.
Step 3: Start Sql Server Services again.
Step 4: In SQL Studio Management -> Delete error database.
Step 5: Stop Sql Server Services and Copy Data to old folder.
Step 6: Attach again and auto change from single user to multi user
This solution test on Sql Server 2017 by me.

Offline database can't be restored: Exclusive access could not be obtained

I'm practicing a SharePoint 2010 to 2013 migration and having trouble restoring a database. After backing up a database on a SP2010/SQL 2008 server, I'm trying to restore it to a database I've freshly created on my SP2013/SQL 2012 server.
Though the UI, I've taken the destination database offline. I select the .bak file, select my destination database, and attempt the restoration. I get an error: Exclusive access could not be obtained because the database is in use.
Why is this happening? I've taken the database offline, I don't see how it could possibly be in use.
In the restoration window, I've tried going to Options and selecting Overwrite the existing database (WITH REPLACE) as well as Close existing connections to destination database.
Still the same error message. How do I get past this error?
Try this, I had the same issue last week!
ALTER DATABASE [Test4] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE [Test4]
FROM DISK = 'c:\test4.BAK'
WITH MOVE 'Test4_Data' TO 'c:\data\Test4.mdf',
MOVE 'Test4_Log' TO 'c:\data\Test4_log.ldf'
detailed explanation can be found at http://www.mssqltips.com/sqlservertip/1407/getting-exclusive-access-to-restore-sql-server-databases/

Database is not accessible after 'taking it offline' process failed

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.