Create copy of database in sql server 2012 express - sql-server

Hello I have database on which user work.
I want to create copy of this database for testing purpose.
Can anyone tell how can this be done?
I tried everything from copy a database within SQL Server Express?
But Copying files and attaching it destroys first database and trying to restore backup into new database throws error that database is not the same.
Can anyone suggest me solution?
as #Nadeem_MK suggested I created backup and now I'm trying to restore with this script:
RESTORE DATABASE [Equipment Test] -- use your new db name
FROM DISK = N'C:\Backup\ExistingDb.bak' --Path of backup location
WITH REPLACE,RECOVERY,
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\ExistingDb.mdf', --logical name and physical name
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\ExistingDb.ldf' --logical name and physical name
and this throws error:
Msg 1834, Level 16, State 1, Line 1
The file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf' cannot be overwritten. It is being used by database 'Equipment'.
Msg 3156, Level 16, State 4, Line 1
File 'Equipment' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 1
The file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf' cannot be overwritten. It is being used by database 'Equipment'.
Msg 3156, Level 16, State 4, Line 1
File 'Equipment_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
As you can see restores is trying to alter files from backup-ed database instead of new database
#Update:
BackUp Script:
--To backup Database (Delete the previous backup first)
BACKUP DATABASE Equipment --Database Name
TO DISK = 'C:\Backup\Equipment.bak' WITH INIT --Path of backup location
file locations:
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment Test.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment Test_log.mdf
Restore function:
RESTORE DATABASE [Equipment Test] -- use your new db name
FROM DISK = N'C:\Backup\Equipment.bak' --Path of backup location
WITH REPLACE,RECOVERY,
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment2.mdf', --logical name and physical name
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log2.ldf' --logical name and physical name

You can create a backup and restore it using the following script;
--To backup Database (Delete the previous backup first)
BACKUP DATABASE ExistingDb --Database Name
TO DISK = 'C:\DBBackup\ExistingDb.bak' WITH INIT --Path of backup location
Then restore it;
RESTORE DATABASE NewDbName -- use your new db name
FROM DISK = N'C:\DBBackup\ExistingDb.bak' --Path of backup location
WITH REPLACE,RECOVERY,
MOVE N'Reporting' TO N'C:\Databases\MDF\NewDbName.mdf', --logical name and physical name
MOVE N'Reporting_log' TO N'C:\Databases\LDF\NewDbName.ldf' --logical name and physical name

Assuming this are the paths of the original database:
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf
and the filename of your backup is called
Equipment.bak
Make sure that the filenames for the new database aren't the same as the old one. That is what the errors you are getting means:
The file 'C:\...' cannot be overwritten. It is being used by database 'Equipment'.
Explanation: Don't use filenames of files you already have!
Instead, give these a new name. Like this:
RESTORE DATABASE [Equipment Test] -- use your new db name
FROM DISK = N'C:\Backup\Equipment.bak' --Path of backup location
WITH REPLACE,RECOVERY,
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment2.mdf', --logical name and physical name
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log2.ldf' --logical name and physical name
This is basically Nadeem_MK's answer, but instead of your original names Equipment.mdf and Equipment_log.ldf, now you should have Equipment2.mdf and Equipment_log.ldf
If this works, please also opvote Nadeem_MK's answer, as this is basically what he was saying. I just don't have enough reputation yet to comment on his answer.

Simple, Do these steps:
Take backup of your primary database.
Create new database with whatever name you want to give it for testing.
Restore backup of your primary database to your newly created testing database.
Done!!

Note: The functionality below is not available in SQL Express
I have left the answer here for completeness for future readers only.
The simplest method I can think of is to let Management Studio do all the work!
Right-click the database you wish to copy and look for the Copy Database... option under the Tasks menu.

Why don't you script the database and run the script?

Related

Unable To Execute restore Operation [migrated]

This question was migrated from Stack Overflow because it can be answered on Database Administrators Stack Exchange.
Migrated 22 days ago.
I tried to run this SQL statement:
RESTORE DATABASE test1
FROM DISK = N'D:\Projects\Billing DB\Test1.bak'
WITH REPLACE;
but all I get are these errors:
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.mdf" failed with the operating system error 3(The system cannot find the path specified.).
Msg 3156, Level 16, State 3, Line 1
File 'ChickenDB' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 5133, Level 16, State 1, Line 1
Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.ldf" failed with the operating system error 3(The system cannot find the path specified.).
Msg 3156, Level 16, State 3, Line 1
File 'ChickenDB_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
Like the error says you need to use WITH MOVE to specify valid file locations for the database files. By default the file locations on the source system are used.
An easy way to do this is to restore the database with the restore wizard in SSMS. It will write the SQL for you, and even execute it if you want.
A SQL Server backup file contains information on where the database files were located when the (full) backup (set) was created.
In your case the backup file contains the following information:
Logical Name | Physical Name
---------------+--------------------------------------------------------------------------------
ChickenDB | C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.mdf
ChickenDB_log | C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\DATA\Test1.ldf
When restoring a database with the RESTORE DATABASE .... command, then the restore process will read the backup file and extract this information and use it as a default value for the restore process.
If you want to determine the file locations stored in the backup file then use the RESTORE FILIELISTONLY... command.
Returns a result set containing a list of the database and log files contained in the backup set in SQL Server.
You can use the RESTORE FILIELISTONLY... command like this:
RESTORE FILELISTONLY FROM DISK = `D:\Projects\Billing DB\Test1.bak`;
In order to restore the database to a new location you will have to use the MOVE parameter of the RESTORE DATABASE .... command.
MOVE 'logical_file_name_in_backup' TO 'operating_system_file_name' [ ...n ]
Specifies that the data or log file whose logical name is specified by logical_file_name_in_backup should be moved by restoring it to the location specified by operating_system_file_name. The logical file name of a data or log file in a backup set matches its logical name in the database when the backup set was created.
Solution
For your backup you would probably be best of with something like this:
RESTORE DATABASE test1
FROM DISK = N'D:\Projects\Billing DB\Test1.bak'
WITH
MOVE 'ChickenDB' TO 'D:\SomeDirectory\Test1.mdf',
MOVE 'ChickenDB_log' TO 'D:\SomeDirectory\Test1.ldf',
REPLACE,
RECOVERY;
Example code can also be found in the online documentation for the RESTORE DATABASE .... command in the example D. Restore a database and move files.

SQL-Server: Cannot be overwritten. It is being used by database

RESTORE DATABASE Tes
FROM DISK = '{7522204E-0256-47B3-9864-137D6D1FD449}6'
WITH MOVE 'Test' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.mdf',
MOVE 'Test_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.ldf'
The above restore command is working fine for new dbs, But few old databases it showing a errors like
The file 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf' cannot be overwritten. It is being used by database 'windb'.
Msg 3156, Level 16, State 4, Line 1
File 'windb' cannot be restored to 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf'. Use WITH MOVE to identify a valid location for the file
I found that only dbs having more than one ndf files having this issues ..
Any solutions this issue ?
Try to restore the database from the Restore Database Wizard. For that, in the Object Explorer, right-click on Databases -> select Restore Databases.... In the window that appears, set the necessary details for the Source and the Destination. And in the left pane, either select Options or Files, depending on the SSMS version you have.
In the table that appears, find the column Restore As. In each row, give a new file name, instead of the name that shows. For example, in your case it might be showing the file names as something similar to <Folder Path>\DATA\Tes.mdf and <Folder Path>\DATA\Tes.ldf. Give a different name other than Tes. (A name that does not already exist in the DATA folder).
Note: The file names do not have anything to do with the database name, but it will easy referencing later on to give a meaningful name, in case you have to backup or look for the files.
If you are trying to move your database to a new location, you should better detach the source database or take it offline first
I faced this issue when working on an MS SQL Server 2012.
I was trying to restore a database using the script below
USE master;
GO
ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE my_db.dev
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev.bak'
WITH REPLACE,
STATS = 10,
RESTART,
MOVE 'com.mydb.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf',
MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf'
GO
ALTER DATABASE com.mydb.dev SET MULTI_USER;
GO
And then this was throwing the error below when I tried executing the restore task:
The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev.mdf' cannot be overwritten. It is being used by database restdb
The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\com.mydb.dev_log.ldf' cannot be overwritten. It is being used by database restdb
Here's how I fixed it:
The issue was that the database backup file that I was using for this database restore operation had been previously used to restore data to a different database on the same server.
All I had to do was to change the Physical names of the Logical files to that of the database that I was to restore to. That is from com.mydb.dev.mdf to my_db.mdf and from com.mydb.dev_log.ldf to my_db_log.ldf:
USE master;
GO
ALTER DATABASE my_db SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
RESTORE DATABASE my_db
FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\com.mydb.dev.bak'
WITH REPLACE,
STATS = 10,
RESTART,
MOVE 'com.my_db.dev' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db.mdf',
MOVE 'com.mydb.dev_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\my_db_log.ldf'
GO
ALTER DATABASE my_db SET MULTI_USER;
GO
And this time, the database restore task ran successfully.
That's all.
I hope this helps

Need to Backup and Restore a database

I need to create a script that when I execute it. It will backup a production database (keep in mind this is a live database) and restore it as development database(if exists overwrite)
I have tried this query but getting error message. I need to find a way to do this without taking the database offline or single user mode
USE [master]
RESTORE DATABASE [Development] FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\Production.bak'
WITH FILE = 6, MOVE N'Producation' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Development.mdf',
MOVE N'Production_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\Development_log.ldf', NOUNLOAD, REPLACE, STATS = 5
Msg 3101, Level 16, State 1, Line 67
Exclusive access could not be obtained because the database is in use.
Msg 3013, Level 16, State 1, Line 67
RESTORE DATABASE is terminating abnormally.
I realized I do not need to keep the development online and can kill connections.
so I have used the ALTER DATABASE [ALI] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO to resolve this error

Restore MSSQL database backup to a new database

I need to make clean database template backup and then generate a new one database with another name, but with backuped content.
Backup script is:
sqlcmd -S %DB_HOST% -Q "BACKUP DATABASE %DB_NAME% TO DISK = '%BACKUP_FILE%'"
Restore script is:
sqlcmd -S %DB_HOST% -Q "RESTORE DATABASE %DB_NAME% FROM DISK = '%BACKUP_FILE%' WITH REPLACE"
And it works on the same database name.
But if i backup DB 'ORIGINAL_DB_NAME' and then restore it to 'NEW_DB_NAME' it will raise exception:
Msg 1834, Level 16, State 1, Server <HOST>, Line 1
The file '.....\MSSQL\Data\ORIGINAL_DB_NAME.mdf' cannot be overwritten. It is being used by database 'ORIGINAL_DB_NAME'.
....
You need to specify a new location for the data/log files.
See this MSDN article, Example D
RESTORE DATABASE AdventureWorks2012
FROM AdventureWorksBackups
WITH NORECOVERY,
MOVE 'AdventureWorks2012_Data' TO
'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.mdf',
MOVE 'AdventureWorks2012_Log'
TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.ldf';
RESTORE LOG AdventureWorks2012
FROM AdventureWorksBackups
WITH RECOVERY;
You can also avoid that conundrum using SSMS. Under the Options tab, after you have selected a source for your restore direct the data and log files to either the files you want to overwrite, or point them to the directory you need and name them appropriately.
If you go to Files on the Restore Configuration, make sure that the Original File Name is not the same as Restore As, that should solve your issue.

Create Duplicate SQL Database for Testing

I have created a database on SQL server and a front end user application in winforms c#.
It's up and running and working fine, but I've now been asked to set up a test version by the client for training new employees, so they can put in dummy data whilst they are trained without effecting the 'real' live database.
When I installed the database I had it all scripted, but things have changed since, and I don't particularly want to script all the tables again.
Is there a fast, convenient way of duplicating the database (and its data) on the same server, but under a different name?
EDIT (subsequent to Tony Hopkinson post)
I've got as far as this
exec sp_addumpdevice 'Disk','MyDatabaseBackup',N'D:\MyDatabaseBackup'
Restore Database MyDatabase From MyDatabaseBackup With File = 1,
Move 'MyDatabase' To N'C:\Program Files\Microsoft SQL
Server\MSSQL11.SQLE\MSSQL\DATA\MyDatabaseTEST.mdf',
Move 'MyDatabase_Log' To N'C:\Program Files\Microsoft SQL
Server\MSSQL11.SQLE\MSSQL\DATA\MyDatabaseTEST_log.ldf',
NORECOVERY, NOUNLOAD, STATS = 10
RESTORE LOG [MyDatabaseTEST] FROM [MyDatabaseBackup] WITH FILE = 2, NOUNLOAD, STATS = 10
exec sp_dropdevice MyDatabaseBackup
But I'm getting the following error message
Msg 3234, Level 16, State 2, Line 2
Logical file 'MyDatabase' is not part of database 'MyDatabase'. Use RESTORE FILELISTONLY to list the logical file names.
Msg 3013, Level 16, State 1, Line 2
RESTORE DATABASE is terminating abnormally.
Msg 3154, Level 16, State 4, Line 6
The backup set holds a backup of a database other than the existing 'MyDatabaseTEST' database.
Msg 3013, Level 16, State 1, Line 6
RESTORE LOG is terminating abnormally.
Device dropped.
use copy database option in SQL server management studio
I found this method to be most effective on SQL Server 2005 and 2008, Express Editions:
From the Microsoft Docs:
Right click on the database you want to duplicate and choose Tasks->"Back Up..."
Save the back up to a .bak file
Right click on the "Databases" folder in the Object Explorer in SQL Server Management Studio
Choose "Restore Database"
As the source, select "File" and point to the .bak file you created earlier.
Change the name of the database to restore to (this was the key step for me - you are not constrained to the options in the dropdown.)
SSMS will restore your .bak file to a new database, according to the name that you give it.
First do a full backup your current database, which of course you have :)
The you restore it to another one
e.g. something like
exec sp_addumpdevice 'Disk','LiveDataBackup',N'Insert backup file name here including path'
Restore Database TestData From LiveDataBackup With File = 1,
Move 'LiveData' To N'Path to where sqlserver expects the mdfs to be\TestData.mdf',
Move 'LiveData_Log' To N'Path to where sqlserver expects the ldf to be\TaxData1.ldf',
NORECOVERY, NOUNLOAD, STATS = 10
RESTORE LOG [TestData] FROM [LiveDataBackup] WITH FILE = 2, NOUNLOAD, STATS = 10
exec sp_dropdevice LiveDataBackup
Above assume your live database is cunningly named LiveData and test, TestData.
The path to where the mdf and ldf will be depends on the version of sql server and the instance name
It should be something like
C:\Program Files\Microsoft SQL Server\MSSQL11.DENALI\MSSQL\DATA\
MSSQL11 because it's sql 2012, and DENALI is my instance name and it was installed by default in C: \Program Files
Also there's no with replace, so if you wanted to run it again, you'd need to Drop your test database.
There's probably some way to do this from the GUI, but I found it a massive PIA trying to relate the UI, to what I wanted to do.
using MS SQLServer 2012, you need to perform 3 basic steps
first, generate .sql file containing only the structure of the source DB
=> right click on the source DB and then Tasks then Generate Scripts
=> follow the wizard and u can save the .sql file locally
Second, replace in .sql file the source db with the destination one
=> right click on the destination file, open the .sql file and press New Query and Ctrl-H or (edit - find and replace - Quack replace)
finally, populate with data
=> right click on the detination DB, then Tasks and then Import Data
=> Data source drop dow set to ".net framework data procider for sql server" + set connection string text field under DATA ex: Data Source=Mehdi\SQLEXPRESS;Initial Catalog=db_test;User ID=sa;Password=sqlrpwrd15
=> Same thing to do with the destination
=> check the table you want to transfer or check box besides "source :....." to check all of them
you are done.
You want to copy one Database_Production to Database_Testing in the same server. I would take database_production database as an example. I tested it in my server successfully.
Firstly, backup the database Database_Production.
BACKUP DATABASE Database_Production TO DISK ='H:\test\Database_Production.bark';
Secondly, restore Database_Production and this could rename the database name to Database_Testing.
RESTORE DATABASE Database_Testing
FROM DISK='H:\test\Database_Production.bark'
WITH
MOVE 'Database_Production_Data' TO 'H:\test\Database_Testing_Data.mdf',
MOVE 'Database_Production_log' to 'H:\test\Database_Testing_Data.ldf';
GO
Then the database Database_Production is copied to database Database_Testing. The MOVE statement causes the data and log file to be restored to the specified locations. You do not need to create database Database_Testing and the script would create it.
Dump your database into a backup file
Re-create your database from your dump - that is a script which you can run - with different name (that you have to change into the script)
You can follow (this)
If you need to create a database on the same server just create the empty database. Right Click on it and Select Restore-> Choose the database you want to make a copy of and click okay.

Resources