Convert a MS SQL 2008 .mdf file to .bak file - sql

I try to convert a .mdf file to a .bak file, I have been reading this guide: http://forums.asp.net/p/1448416/3870094.aspx
BACKUP DATABASE [NameOfDatabase] TO DISK = N'D:\path\filename.bak' WITH NOFORMAT, NOINIT, NAME = N'NameOfDatabase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
Replace NameOfDatabase with.... name of your database.
Replace D:\path\filename.bak with place you want to backup with
Replace Name = N database name for cosmetic indexing reasons...
But I get an error message when I run it.
In Visual Studio 2010 in Server Explorer I select Data Connection/myDatabase.mdf and rightclick Stored Procedure and choose "add new stored Procedure". There I write:
BACKUP DATABASE [myDatabase] TO DISK = N'C:\db\dbCopy.bak' WITH NOFORMAT, NOINIT, NAME = N'dbCopy-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
When I select the text and run selection I get this error message:
Executing selected script from mssql:://Home\e1d5110c-dd6f-4d/C:\USERS\myHome\DESKTOP\myPage\APP_DATA\MYDATABASE.MDF/dbo/Stored Procedure/dbo/StoredProcedure1
Database 'myDatabase' does not exist. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.
No rows affected.
(0 row(s) returned)
What am I doing wrong? If I change 'myDatabase' to 'C:\USERS\myHome\DESKTOP\myPage\APP_DATA\MYDATABASE.MDF' I get this message:
Database 'C:\USERS\myHome\DESKTOP\myPage\APP_DATA\MYDATABASE.MDF' does not exist. Make sure that the name is entered correctly.
BACKUP DATABASE is terminating abnormally.
No rows affected.
(0 row(s) returned)

You need to attach the database first before you can back it up. Do you have an LDF file as well? You might need one, or it might be happy to build its own afresh - I'm not sure.
The simplest way to do this all is through SQL Server Management Studio. If you don't have it (e.g. you're using SQL Server Express) then you should definitely download it and install it. Log in to your SQL Server instance in Management Studio then
right-click on 'user databases', 'attach' then select your MDF file
right-click on the attached database and 'tasks', 'back up'; use 'remove' and 'add' in the destination box if you want to change the filename.

I know this post has been for a long time but i thought also someone might still need an answer that worked for me, so The bak file is just a backup file of your SQL Server database.
You can download the MS SQL Server Express Management Console (for your version of SQL Server or SQL Server Express) and do this
http://msdn.microsoft.com/en-us/library/ms187510.aspx

Related

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.

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

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

Restoring SQL Server database using ADODB retained the database restoring state

I'm restoring a database using ASP code (ADODB). The .bak file is in local system. The SQL is local with SQL Authentication.
If I tried the Restore Query in SSMS the restore successful. But the same query in ASP code is not successful. The db will be in Restoring... state.
The Query is
RESTORE DATABASE [myDB] FROM
DISK = N'D:\DB\myDB_20120611_190339.bak' WITH FILE = 1,
MOVE N'myDB' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.WEB2K8\MSSQL\DATA\myDB.mdf',
MOVE N'myDB_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.WEB2K8\MSSQL\DATA\myDB_1.ldf',
NOUNLOAD, REPLACE, STATS = 10
GO
I dont know whats the issue.
Any help would be appreciated.
Thanks
Ganesh.
You need to use the WITH RECOVERY option, with your database RESTORE command, to bring your database online as part of the restore process.

copying sqlserver2000 database from one computer to sqlserver 2008 of another computer

i have a database named SAHS in my desktop having sqlserver 2000 installed i want to copy it in my laptop(64bit) having sql server 2008 ..
i tried removing a backup of it..
like this ;
BACKUP DATABASE SAHS
TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\BACKUP\SAHSFullRM.bak'
WITH FORMAT;
GO
can i copy the backup file in my laptop and use the restore statement?.. if yes please show the restore statement im getting error in this :
restore database SAHS
from disk = 'C:...'
saying operating system error.
is there any other simpler way to do so..pls help
Make sure the target server has access to the folder containing the bak file. And on the target server you can try running the Restore Database wizard from SSMS.
i did it myself .Open sqlserver management studio ,from the object explorer expand database column, right click any databse , select task --> restore --> database or Files or Filegroups , restore dialog box opes , write the new name of yor database in To Database , select location and type it in From Device option , select the checkbox , and click on OK ..
ITS DONE .. this is a new feature in sqlserver 2008 ..

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.