How do I copy SQL Server 2012 database to localdb instance? - sql

I'm looking to copy a SQL Server 2012 Standard database to my localdb instance. I've tried the wizard which complains that localdb isn't a SQL Server 2005 or later express instance. I also did a backup/restore but upon the restore in my localdb I get the following error...
Running this...
RESTORE DATABASE CSODev
FROM DISK = 'C:\MyBckDir\CSODev.bak'
WITH MOVE 'CSOdev_Data' TO 'C:\Users\cblair\CSOdev_Data.mdf',
MOVE 'CSOdev_Log' TO 'C:\Users\cblair\CSOdev_Log.ldf',
REPLACE
Error message I get...
Processed 8752 pages for database 'CSODev', file 'CSOdev_Data' on file 1.
Processed 5 pages for database 'CSODev', file 'CSOdev_Log' on file 1.
Msg 1853, Level 16, State 1, Line 1
The logical database file 'CSOdev_Log' cannot be found. Specify the full path for the file.
Msg 3167, Level 16, State 1, Line 1
RESTORE could not start database 'CSODev'.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
The database ends up in "Recovery Pending" mode. It seems like it has issues with the log file. I have tried 2 different backups in case one was just corrupted.

There is known limitation (a real bug, in fact) for localDB. It will fail any RESTORE with MOVE whenever your database files are located in different folders.
You have to restore in the original folders (no MOVE). Use cmd tool such as SUBST if you need to fake a drive:/path.

I had the same problem. What eventually did work was this:
Trying to restore the database (getting the error in the OP)
Detaching the database
Reattaching the database
What happened in the last step was that SSDT performed an upgrade of the data files, that apparently was in an older format. When that was finished, the database started working without any problem!

I had the same issue, and after doing a little online research I came across an ingenious way to get it to work (albeit quite hacky). Basically, you:
Create a SqlLocalDb instance (SqlLocalDb c tmp -s).
Restore the database as you did above (e.g., SqlCmd -E -S <localdb connection string> -Q "RESTORE DATABASE ...").
Stop the SqlLocalDb instance (SqlLocalDb p tmp).
Delete the SqlLocalDb instance (SqlLocalDb d tmp).
Create a new SqlLocalDb instance (SqlLocalDb c persistent -s).
Create the database in the new instance by attaching it (SqlCmd -E -S <persistent connection string> -Q "Create Database <dbname> On (Filename = '<Mdf file location'), (Filename = '<Ldf Filename'>) For Attach".
And hopefully it should work. See here for original idea.
Edit: Added Jason Brady's correction of the create command.

Try scripting your database as schema and data and then running the script locally.

RESTORE FILELISTONLY
FROM DISK = 'D:\SQLBackups\yourdatabase.BAK'
ALTER DATABASE yourdatabasename
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE yourdatabasename
FROM DISK = 'D:\SQLBackups\yourdatabase.BAK'
with replace,
move 'logical name from file stream' to
'C:\yourdatabase.mdf',
move 'logical name from file stream' to 'C:\Yourdatabase.ldf'
ALTER DATABASE Qatar_DB SET MULTI_USER

Same problem, thanks for the help.
My local database is MS SQL 2014.
Open "SQL Server 2014 Management Studio"
Right click the database, go to "Tasks", click "Take Offline"
Detach the database
Attach the database
It work for me.
After you backup the database, you can restore the database without error.
Thanks.

I had the same problem. Try running visual studio as Administrator and try the following command
RESTORE DATABASE CSODev
FROM DISK = 'C:\MyBckDir\CSODev.bak'
WITH NORECOVERY, MOVE 'CSOdev_Data' TO 'C:\Users\cblair\CSOdev_Data.mdf',
MOVE 'CSOdev_Log' TO 'C:\Users\cblair\CSOdev_Log.ldf',
UPDATE: This did not work exactly!
Although the above statement does not produce any errors and completes successfully, the database remains in "PENDING RECOVERY" state and cannot be accessed in any way. When I tried to 'RESTORE WITH RECOVER' to bring the database online I got the same error as in the question above.
So in my case I ended up restoring the backup to a DEV server I have running with MSSQL 2008 R2 and then chose: Tasks -> Generate Scripts -> chose objects to script & Next -> click on "Advanced" button -> select "types of data to script" : Schema & data.
Now run the generated script against the local db.

Try these scripts (example with adventureworks2012 that I personally tested):
RESTORE FILELISTONLY
FROM DISK = 'c:\temp\adv2012.bak'
This will bring up the filenames as:
AdventureWorks2012 C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012RTM\MSSQL\DATA\AdventureWorks2012.mdf
AdventureWorks2012_log C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012RTM\MSSQL\DATA\AdventureWorks2012_log.ldf
Use these filenames to cinstruct your final script as this:
RESTORE DATABASE AdventureWorks2012
FROM DISK = 'C:\temp\adv2012.bak'
WITH MOVE 'AdventureWorks2012' TO 'C:\cnom_WS\Local-Databases\AdventureWorks\AdventureWorks2012.mdf',
MOVE 'AdventureWorks2012_log' TO 'C:\cnom_WS\Local-Databases\AdventureWorks\AdventureWorks2012_log.ldf',
REPLACE;
BTW I run these through Visual Studio (SQL Server Object explorer), but I strongly suspect this could be run on SSMS easily ;-)

You can do it manually. this can be done by using dot net and opening two kinds of connections and forwarding data from one of them to the other. but this needs to create the same types of columns in the local one.
You can check the importing options of MS Access 2007

Related

How to add a database in SQL Server Management Studio?

I have been sent a database called StudentsDB.mdf and I want to enter it into my SQL Server Management Studio databases . How to do that ?
I want to know if I copy a .mdf file from the directory where are all my databases which is C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA
and sent it to another person will he be able to import this database in his SQL Server Management Studio and see the database?
Try this one
Step 1
Right-click “Databases” and click the “Attach” option to open the Attach Databases dialog box.
Step 2
Click the “Add” button to open the Locate Database Files dialog box.
Step 3
Type in the full name of the .MDF file, including the full device and directory path, as the following example illustrates:
c:\StudentsDB.mdf
Click the "OK" button. SQL Server Management Studio loads the database from the .MDF file.
OR
Step 1
Click “New Query” in the Management Studio’s main toolbar.
Step 2
Type a Create Database statement using the following Transact-SQL code:
CREATE DATABASE MyDatabase ON
(FILENAME = 'c:\StudentsDB.mdf'),
(FILENAME = ' c:\StudentsDB.ldf') FOR ATTACH;
Step 3
Click the “Execute” button in the Transact-SQL toolbar. SQL Server Management Studio restores the database.
OR
CREATE DATABASE StudentDB ON
(FILENAME = N'C:\StudentsDB.mdf')
FOR ATTACH_REBUILD_LOG
GO
Execute the following command from SSMS.
USE master;
GO
EXEC sp_attach_single_file_db #dbname = N'StudentsDB'
,#physname = N'D:\<path to mdf file>\StudentsDB.mdf'
GO
Now if you refresh the database list in SSMS it should show a database StudentsDB in the list.
I want to know if I copy a *.mdf file ... and [send] it to another person, will he be able to import this database?
You can do this, but there are a few considerations. The first is that you need to take the database offline, or use another command to ensure there are no pending transactions waiting to be written or locks or latches waiting to be closed.
The second consideration is that, once the database is imported, you may need to recreate (by hand or by script) a few items that aren't stored within the mdf file itself. This includes users and permissions, links to other databases, and other services that are provided by at the Server level rather than the Database level.

Backup/Restore from different database causing Restore failed exclusive access could not be obtained

I have a database A. I have taken a backup of database A called A.bak. I created a new database B. Now, I right click and Restore B from A.bak. In the Restore Dialog, I checked overwrite existing database and change the LogicalFileName from C:\Program Files\Microsoft SQL Server\MSSQL11.SQLSERVER2012\MSSQL\DATA\A.mdf to C:\Program Files\Microsoft SQL Server\MSSQL11.SQLSERVER2012\MSSQL\DATA\B.mdf and did the same with ldf file. But I am getting
Exclusive access could not be obtained because the database is in use.
Also tried,
ALTER DATABASE [B] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
Also sp_who2, there was no existing connection of [B]
A cause for the attempt to get exclusive access comes from the options page of the restore dialog in SQL Server 2012 Management Studio. It will turn on tail-log and leave in restoring state options for the SOURCE database. So, it will try to gain exclusive access to the source database (in this case A) in order to perform this action. If you turn off the tail log option, you will find that the operation works much more smoothly.
The answer was very simple,
Run this command to grab the LogicalNames,
RESTORE FILELISTONLY FROM DISK = 'C:\Users\MyUSer\Desktop\A.bak'
Then Just put the in LogicalName in below,
RESTORE DATABASE B
FROM DISK = 'C:\Users\MyUSer\Desktop\A.bak'
WITH
MOVE 'LogicalName' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLSERVER2012\MSSQL\Data\B.mdf',
MOVE 'LogicalName_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLSERVER2012\MSSQL\Data\B.ldf'
GO
Note you might need to change the path. Helpful links,
How to restore to a different database in sql server?
http://technet.microsoft.com/en-us/library/ms186390.aspx
B. ’Restore Database’ Dialog will be displayed on the General page
1. The name of the restoring database appears in the To database list box. To create a new database, enter its name in the list box.
Select ‘From device’
Click button to display ‘Specify Backup’ Dialog
Click ‘Add’ to browse the .bak file from the directory and click OK

SQL Server 2000 backup recovering on SQL Server 2005

I've got a SQL Server database backup (file extension .bak) from an project of 12 y/a which I tried to restore on SQL Server 2005.
But this gave me an error that it was not the right version..
So I'm trying to find a SQL Server 2000 version to see if I can restore it with this but I can't find any version that works.
Is there another program to save my backup?
Copied over from msdn forums:
1) Go to restore database
2) Select the database that you want to back up to
3) Locate the backup file on disk. You may have to put it into the MSSQL Server -> MSSQL.1-> MSSQL -> Backup Folder. It must be a .bak file.
4) Select the back that want to restore from the available backups.
5) Go to the top left "options" property and when you do that select "overrite existing database".
6) Now make sure that the path to the files on database to be restored are correct in this same dialog view. Look at the paths to the database file and the log file and make damn sure that they are the correct ones for the database to be restored. The problem here is that those paths are going to be for the filesystem that the backup came from, not the one you are goning to put the restore onto.
Just to add to that, if you are restoring a database to a new server that does not have the database already in it (to restore to), create a sham database with the same name, then restore to it with the overwrite settings mentioned above.

Restore database from database using script

I'm looking for a simple script that I can use to overwrite one database with another one. We have a master db with master schema and data and every so often a developer wants to blow away his messed up db with a complete overwrite from the master. I currently accomplish this with SQL Server Studio and the GUI controls but I want something similar to what we use when restoring from a backup file (just without the backup file step):
RESTORE DATABASE [SlaveDB]
FROM DISK = N'E:\Backup\MasterDB.bak'
WITH FILE = 1,
MOVE N'SlaveDB_Data' TO N'E:\Data\SlaveDB_Data.mdf',
MOVE N'SlaveDB_Log' TO N'E:\Log\SlaveDB_Log.ldf',
NOUNLOAD,
STATS = 10
GO
What's the syntax for getting the db from another db instead of a backup file?
if you want to restore database from a generated script file you can use windows command line.
open CDM and run the below command (database=NorthWind, script file C:\MyScript.sql)
sqlcmd -S localhost -d NorthWind -i "C:\MyScript.sql"
I use this script every day to restore production backup to test database.
db names on production and on test are the same, suppose its name is MyDb.
delete test database
Run the script
RESTORE FILELISTONLY
FROM DISK = 'E:\WorkCopy\BackUp.bak'
RESTORE DATABASE [MyDb]
FROM DISK = 'E:\WorkCopy\BackUp.bak'
WITH
MOVE 'MyDbPrimary' TO 'D:\data\MyDb\WorkCopy.mdf',
MOVE 'MyDbImp' TO 'D:\data\MyDb\WorkCopy_1.ndf',
MOVE 'MyDbCut' TO 'D:\data\MyDb\WorkCopy_2.ndf',
MOVE 'MyDbIX' TO 'D:\data\MyDb\WorkCopy_3.ndf',
MOVE 'MyDbAUD' TO 'D:\data\MyDb\WorkCopy_4.ndf',
MOVE 'MyDbLog' TO 'D:\data\MyDb\WorkCopy_5.ldf',
move 'sysft_FTIndexCatalog' TO 'D:\data\MyDb\FTIndexCatalog'
ALTER DATABASE MyDb
Set RECOVERY SIMPLE

Sql Server 2005 Restore Failing

Running sql server 2005 I have database A. I am trying to restore from a backup of A to database B. I want to retain the database A and create a new testing database B from a previous set of data.
I tried to create B and restore from the .bak AND restore database to B from management studio.
The error is...
TITLE: Microsoft SQL Server Management
Studio
Restore failed for Server
'195448-APP2'.
(Microsoft.SqlServer.Smo)
For help, click:
http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.1399.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476
------------------------------ ADDITIONAL INFORMATION:
System.Data.SqlClient.SqlError: The
backup set holds a backup of a
database other than the existing 'B'
database. (Microsoft.SqlServer.Smo)
For help, click:
http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.1399.00&LinkId=20476
------------------------------ BUTTONS:
OK
I found this snippet which I am hesitant to use and want to ask if it would solve my problem of changing the location of the mdf and ldf during the process of restoring the database or does it replace database A's items altogether.
ALTER DATABASE AdventureWorks
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE AdventureWorks
FROM DISK = 'C\:BackupAdventureworks.bak'
WITH MOVE 'AdventureWorks_Data' TO 'C:\Data\datafile.mdf',
MOVE 'AdventureWorks_Log' TO 'C:\Data\logfile.ldf',
REPLACE
[http://blog.sqlauthority.com/2007/04/30/sql-server-fix-error-msg-3159-level-16-state-1-line-1-msg-3013-level-16-state-1-line-1/][1]
and for me I would make it...
RESTORE DATABASE B
FROM DISK = 'C:\backupofA.bak'
WITH
MOVE 'B' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B.mdf',
MOVE 'B_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B_log.ldf',
REPLACE
What I don't know is if it will affect database A at all. I am hoping the replace refers files associated with B.
or if it should be
RESTORE DATABASE B
FROM DISK = 'C:\backupofA.bak'
WITH
MOVE 'A' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B.mdf',
MOVE 'A_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B_log.ldf',
REPLACE
If anyone could help me with the error and/or confirm this fix I would be very grateful as it is not my database I'm playing with.
Thanks.
You could simply use the Copy Database Wizard.
If you wanna do it like pros and use T-SQL the RESTORE .. MOVE ... REPLACE will do what you expected: move the two files at the locations you intend and replace database B with content from the backup. A will be unaffected.
I would use the wizard if I were you: In Sql Server Management Studio right click on "Databases" and select "Restore Database...". This dialog / wizard will do exactly what you are asking - simply select the source .bak file(s) / Database that you want to restore from, enter the name of the database you want to restore to and hit "Ok".
Some notes - if you enter the name of a database that doesnt yet exist (it sounds like this is what you want to do), it will create that database for you. If you enter the name of an existing database it will attempt to restore to that database. If you attempt to restore to an existing database from a backup made of a different database it will fail, however you can force the Sql Server to overwrite the existing database by going to "Options" and checking the "Overwrite the existing database" checkbox.
Also, if you are restoring a backup of an existing database to create a new second copy of that database you may find that the wizard fails as its attempting to create a database using the same database file paths as the ones currently in use by the source database. To fix this you need to click on "Options" and change all of the "Restore As" file paths to files that dont yet exist.
You can also get this wizard to generate an SQL script instead of actually performing the actual restore (click on the "script" button at the top), which is handy if you want learn how to do this sort of thing in raw SQL instead.
It is possible to restore a database from sql server 7, sql server 2000 to sql server 2005.
It can be achieved by using restore and with replace command
use master
restore database mydatabase from disk ='c:\mybackup.bak' with replace
It is easy to take a backup in sql server using this script without any external tools.
Check out Restore with Example.
drop database b first. then do the restore.