How to attach a database by mdf file that has not been detached - sql-server

Issue:
OS hard disk was broke before DB backing up, the original .mdf and .ldf files can be transferred but those .mdf and .ldf files can not be attached by SQL Server Management Studio directly.

Solution:
Assuming the DB name is ‘Sample’ and DB files path is ‘D:\DB’
Step 1: Open Microsoft SQL Server Management Studio to create a New Database with
Database name: Sample
Path: D:\DB
Step2: Stop the DB Server
Step3: Delete Sample.mdf and Sample.ldf files from folder D:\DB
Step4: Copy the old Sample.mdf to the folder D:\DB
Step5: Start the DB Server
Step6: execute the following sql to detach DB
exec sp_detach_db Sample,'true'
Step7: execute the following sql to attach DB and then refresh
exec sp_attach_single_file_db 'Sample','D:/DB/Sample.mdf'

Related

DBeaver restore SQL Server .bak file

I am just trying to restore a SQL Server .bak file in my DBeaver UI. But I have no idea how to do this - can someone help please?
I created a database, but when I right click on it, there are no restore options.
I do not see that option either in my version of DBeaver (6.1.3 - community). What you could do is initiate a restore with TSQL (by clicking on SQL Editor while you are connected to your database). Then in the Script area, use the below, modified for your own environment/database name.
USE [master] RESTORE DATABASE [DatabaseName] FROM
DISK = N'C:\FolderName\BackupName.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 10
For full Tsql options to restore a database, see this: RESTORE Statements (Transact-SQL)
In my case the database migration was done on windows environment so there was some issue restoring backup in Ubuntu 18.04 which was fixed from below command.
backup file path: /var/opt/mssql/data/
move file path: /var/opt/mssql/data/
Restoring database backup in Ubuntu using DBeaver
RESTORE DATABASE MYDB_API FROM DISK = N'/var/opt/mssql/data/mydb.bak'
WITH MOVE 'MYDB_API' TO '/var/opt/mssql/data/mydb.mdf'
, MOVE 'MYDB_API_log' TO '/var/opt/mssql/data/mydb_log.ldf'
REPLACE
Note: if above does not work for you then remove REPLACE from above and make sure MYDB_API does not exist on SQL Server.
It looks like you are using a macOS which is a system not supported by SQL Server Management Studio (SSMS).
So I assume you need to restore SQL Server database with .bak file under macOS GUI.
There is no GUI option to retore .bak file with DBeaver.
If you want absolutely a restoration with GUI and not TSQL, an alternative on macOS is Azure Data Studio (from Microsoft) which is an open source data management tool that runs on Windows, macOS, and Linux.
You can easily restore a bak file in GUI with Azure Data Studio.
Here is the tutorial:
https://learn.microsoft.com/en-us/sql/azure-data-studio/tutorial-backup-restore-sql-server
Restore a database from a backup file (With Azure Data Studio)
Open the SERVERS sidebar (CTRL+G), right-click your server, and select Manage.
Open the Restore database dialog (click Restore on the Tasks widget).
Select Backup file in the Restore from field.
Click the ellipses (...) in the Backup file path field, and select the latest backup file for TutorialDB.
Type TutorialDB_Restored in the Target database field in the Destination section to restore the backup file to a new database.
Click Restore
To view the status of the restore operation, press CTRL+T to open
the Task History sidebar.

What is the SQL Loader equivalent in SQL Server for extracting a database from SQL Server to the same server?

I am trying to replicate or copy a database in SQL Server 2014 in order to create a sandbox. I know SQL Loader does that for Oracle. Is there an equivalent tool in SQL Server?
We use bcp.exe as Loader program. It is located in C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn
You might do this in following ways:
1.Stop the sql server, copy the *.mdf file locating it right clicking on existing database -> properties from the server to the destination sever and attach it to the destination server using SSMS.
2.Create SSIS package and execute that using DTExec.exe.You can create a .bat file ,configure and call the DTexec.exe from that .bat file.This will be alternatve to oracle SQL loader for ETL tasks.
One method would be to create a database backup. Once the backup is completed, restore that backup under a different name.
Steps:
right click database from SSMS
Tasks -> Backup
Follow wizard
Once backup is complete, right click databases folder from the server and select restore database
Follow wizard, ensuring you're restoring the database to a new name.
Here are more detailed tutorials:
https://msdn.microsoft.com/en-us/library/ms187510.aspx
https://msdn.microsoft.com/en-us/library/ms177429.aspx
Note that if your intention is to test an application with the copied DB, ensure you're updating connection strings and whatnot to point to the copy.

Copying .mdf file from app_data folder to default localhost folder

My friend gave me a database file: record.mdf. I copied that .mdf file to my app_data folder and I can access it.
However, the connection string contains absolute path:
AttachDbFilename="C:\Users\Dell\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\App_Data\record.mdf"
But I want it to connect using:
Data Source=localhost\SQLEXPRESS;
How do I copy .mdf file to SQL Server's local folder, so that the connection string does not use an absolute path to the database?
I am using Visual Studio 2010. I do not have SQL Server Management Studio.
Step 1: you need to find out your SQL Server's data directory. This will be something like
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data
by default (for SQL Server 2008 R2 Express) - it might be slightly different in your case, depending on how you installed your SQL Server Express (and which version you have).
Step 2: copy that record.mdf file to that directory
Step 3: attach it to your SQL Server Express instance - using sqlcmd if you don't have Mgmt Studio at hand:
c:\> sqlcmd -S .\SQLExpress
Then at the sqlcmd prompt, type in:
USE [master]
GO
CREATE DATABASE record ON
(FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\record.mdf' )
FOR ATTACH_REBUILD_LOG;
GO
This will attach the .mdf file as your new "logical" database record to your SQL Server Express instance, and it will rebuild the missing transaction log file (.ldf) in the process.
From now on, you can use
server=.\SQLEXPRESS;Database=record;Integrated Security=SSPI;
as your connection string to connect to your database
Rather than copying it to the SQL server local folder, you can access it from the App_Data directory using |DataDirectory|\record.mdf
Documentation: http://msdn.microsoft.com/en-us/library/ms247257(v=vs.80).aspx

Can't attach northwind database to sql server 2008 R2

When I try to I get the following error in SQL Server Management Studio:
TITLE: Microsoft SQL Server Management Studio
Attach database failed for Server 'AHAKEEM'.
(Microsoft.SqlServer.Smo)
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or
batch. (Microsoft.SqlServer.ConnectionInfo)
Unable to open the physical file "C:\SQL Server 2000 Sample
Databases\northwnd.mdf". Operating system error 5: "5(failed to
retrieve text for this error. Reason: 15105)". (Microsoft SQL Server,
Error: 5120)
This is a fresh version of Northwinds mdf which just came from Microsoft's installer.
Error 5120 is a sharing violation on the file you're opening. Try starting SQL Management Studio as Administrator and make sure that the db isn't attached already.
The error occurs when the mdf or ldf file is missing, if its an ldf we can recreate the same using the below listed scripts:
Method 1: To recreate all the log files
EXECUTE sp_attach_single_file_db #dbname = 'SAMPLEDB',
#physname = N'D:\MSSQL\DATA\SAMPLEDB.mdf'
GO
Method 2: If one or more log files are missing, they are recreated again.
CREATE DATABASE SAMPLEDB ON
(FILENAME = N'D:\MSSQL\DATA\SAMPLEDB.mdf')
FOR ATTACH_REBUILD_LOG
GO
Method 3: If only one file is missing, they are recreated again.
CREATE DATABASE SAMPLEDB ON
( FILENAME = N'D:\MSSQL\DATA\SAMPLEDB.mdf')
FOR ATTACH
GO
I tried to install Northwind and pubs Sample Databases for SQL Server 2000 and attach both databases in SQL Server 2014, and gave me an error because they were compatible version.
These are the steps to successful install the Sample Database in your SQL Server 2014:
After you download the Sample Databases in your PC, then open SQL Server 2014 Management Studio. After successful connection, your SQL Server instance should be listed in the Object Explorer.
Now under File Menu select Open > File.
Find the instnwnd.sql in your computer and select it. Click Open
SQL script should open in the main window.
Do not click on Execute yet. If you do, you will get the following error:
Could not find stored procedure ‘sp_dboption’.
Around the line 20, remove the following two lines:
exec sp_dboption 'Northwind','trunc. log on chkpt.','true' exec
sp_dboption 'Northwind','select into/bulkcopy','true'
Replace them with this line as shown below:
alter database Northwind set recovery simple
To attach 'pubs' database, repeat the step 2 above and this time open instpubs.sql file. Replace the sp_dboption as you did before with:
alter database Pubs set recovery simple
All that is left is to execute the script, so click on Execute in both scripts.
Note: the folder location for your sample database by default is "C:\SQL Server 2000 Sample Databases"

SQL - How to backup a database and export as a MDF file with MS SQL 2008 R2

I have created a database "test" with some tables in MS SQL Server 2008 R2 (i.e. MS SQL Server Management Studio).
Now, I need to export this database as a MDF file.
What should I do?
If you mean that you want to be able to attach the database on another server, then this is what you can do:
detach the database (right click the database and click Detach)
copy the mdf and ldf files to your backup location
attach the database (right click Databases and click Attach)
This is the path where you will find MDF file:
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\
right-click on database from Management Studio -> Detach. Then you have the MDF file you can export to wherever you want :)
I think the best way is to create a .bak file from SQL Server .
Right click on the database => Task => Back Up -> Choose the database in Source , and hit OK .
If what you want is a replica of your SQLEXPRESS/MDF local file- on a remote server (SQL Server 2005)
You can right click in the "Server Explorer" on your db.mdf file and press "Publish to provider..."
You can choose a variety of compatibilities with SQL Server 2005, 2008, etc.
The output is a .sql query file ..
Came across this question and thought this would be a good alternative answer, since I got here by searching for the exact same thing!
C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\
This is the path where you will find MDF file
If you are in Visual Studio, go to SQL Server Object Explorer. Find the database you want, right click and select properties. In the properties tab, look for Data File, next to it will be the location for your mdf file.
In my experience, its easier to use the commands sp_detach_db (MSDN) and sp_attach_db (MSDN). I was trying to attach a database that was offline when detached and SQL Server Management Studio (2014) kept crashing when I used the Attach option from the right-click context menu. The commands worked - hope this saves someone some time.
NOTE: Run SSMS as an administrator if you see an access denied while trying to execute the attach command
Go to the path C:\Users\your pc name
then you will find mdf , ldf file here

Resources