copy a database within SQL Server Express? - sql-server

I would like to make a copy of a database I have but keep it on the same server as a test database. However, everything I have found is to use the copy database wizard (I am using MS SQL Server Express).
The instructions always say: In SQL Server Management Studio, in Object Explorer, expand Databases, right-click a database, point to Tasks, and then click Copy Database.
I don't have the Copy Database option. I am running as an admin, so no clue why it is missing for me - is it something I have to install separately? I can't do the Detach/Attach since it is copying to the same server. I tried detaching, copying the MDF/LDF, renaming, attaching but as you can imagine that messed a ton up :) I am not great with SQL to do it all programatically. Is there a tool out there I could use?

In SSMS 2008 you can do this:
Create a backup of the database you want to copy
In SSMS, right-click 'Databases' and select 'Restore Database'
Select the database you wish to copy from the 'From database' drop-down list in the 'Source for restore' section
Enter the name of the new database in the 'To database' field in the 'Destination for Restore' section - this cannot be the name of an existing database.
Click OK
You're done! :)

In SQL Server Express 2012 you can do following steps:
Create a backup of the database you want to copy
right-click "Databases" and select "Restore Files and Filegroups"
Enter the name of the new database in the "To database" field.
Select "From device" and then select the file that you backuped in the first step
click "OK"
this will "clone" the Database with the correct table settings such as the "default value" and "auto increase" etc.

SQL Express database has an export button, I just exported the database to a new database on the same server, it is copying the database. Just right-click on the database name.

Take these steps to make a copy of the database in SQL Express
Stop SQL
Copy the mdf, ldf and any other file for the db to a NEW location (make sure you get the log file)
Change the name of each copied file
Start SQL
Right-click Database in SSMQ and select attach
Make sure you change the name in the column "Attach As"
Update the file location in the lower pane of "Database Details" to the location of your copied files (especially that log file)
I was able to copy a database on my SQL Express system with this method

I had a problem creating a copy of my database as well using SQL Express 2012.
I have solved it by the backup and restore method.
After making the backup I used: restore -> files and file groups
Next step was to write a new name for the new database and set the source:
Pointing the source file
and finally, a overwrite the existing database with replace must be selected
and set names for new files with extension mdf and ldf that are different from the existing where is: Restore as
This method worked for me

Just be aware if you are using SQL Server Express 2012 of going to the option Files and make sure that the destination files (Restore As column) are different from the original files *.mdf and *.log
( I tried to put an image but need 10 reputation :p)

I do not believe the Express version of the manager will have the copy database feature. Have you considered copying via the backup and restore method?
http://msdn.microsoft.com/en-us/library/ms190436.aspx

I found the problem! Click on Databases, restore, then do the following:
After choosing from where to restore, and writing destination db name, go to files [annotation 1 on picture] and change the very right column files names to different than original [annotation 2 on picture] then it works :)

I just thought of a really nifty way to get around this :) So I thought I should post my idea. Note that this is 'untested' but I think it will work.
Do a "Back Up..." database (theoretically this is on your
production server, but it doesn't have to be)
Copy the backup file (from your prod server) onto your development machine
Assuming you're using SSMS Developer Edition on your development
machine, you can then do a "Restore" onto your development machine,
then do a "Copy Database" afterwards also on your development
machine (to create a new copy of the DB)
Now do a "Back Up..." on the new DB you just created, copy the backup file (to your production server) and do a "Restore" on the sql server express server :)
Hope this helps out a few people :)
Cheers,
Jeff

The solution is definitely to create a backup and restore it, but ensure that you're restored copy is pointing to different .mdf and .ldf files.
Here's how to check that using SSMS 2014 and a SQL Server 12 installation: assuming you're creating and restoring backups on your local disk.
Create a backup of your existing database
Right click the database, and choose "back up..." under "tasks."
(If you leave the location as the default, you don't have to hunt for the back up in the next step when you restore.)
Restore your backup to a NEW database:
Right click on databases, choose "restore database"
Select "device"
Click the ellipsis button ("...") to open the "Selct Backup devices" dialog.
Choose "File" as the backup media type and click the "add" button
Select the backup you just made, click ok (twice)
Now, back in the "restore database" dialog, type a new name for your destination database
Click "files" under "select a page" and make sure that "restore as" is pointing to .mdf and .ldf file names that do not already exist
Click ok!

I think you could try import data to a new database.
Create an empty database in your local sql server
Right click on the new database -> tasks -> import data
In the SQL Server Import and Export Wizard, select product env's servername as data source. And select your new database as the destination data.

As for the first part of your question (why do you not see this Copy Database Wizard option under Tasks for your dbs), the answer is indeed in the fact that you are running SQL Server Express. SQL Server Express doesn't support the SQL Server Agent feature, which this Copy DB feature relies upon, so the Copy DB feature is not shown (but many online resources fail to make that observation).
Fortunately, most everyone else has addressed the second part of your question (how to achieve the task otherwise), and nearly all point out using the existing backup and restore options (which DO exist in Express), or the export/import (which also exists in Express, I can confirm).

Try making a backup of your database, and restoring it into a brand new database.
Create new DB.
Make a full backup of your original.
Right click on your new DB, hit restore.
Navigate to your .BAK, and ensure the .mdf and .ldf match the new.

Related

restore .bak mssql 2005 to a new a server of mssql 2008r2

I created a .bak file (backup) from server A with the with following specs: Windows server 2003, MSSQL 2005. Now I would like to restore this backup on a new Server B with the following specs: Windows 8, MSSQL 2008 R2. I did the following to try and do the restore
Copy files to the new server(Server B)
Clicked on Microsoft SQL Server Management Studio 2008R2(Server B)
Right click on Databases to create a new Database called Boom (Server B)
After creating the new Database(Boom), right clicked on Tasks->Restore->Database and
On the source for restore area, Clicked From device and located the .bak file, select it and cliked ok.
Instead of getting the success message, I get the following error:
Restore failed for Server 'Server B'.(Microsft.SqlServer.SmoExtended)
Additional information: System.Data.SqlError: The backup set holds a backup of a database other than the existing 'Boom' database(Microsoft.SqlServer.Smo).
Please assist, Im am new to MSSQL
Right click on Databases to create a new Database called Boom (Server B)
After creating the new Database(Boom), right clicked on Tasks->Restore->Database and
Well now you're making a new database and trying to overwrite it with a different database's backup. Hence:
The backup set holds a backup of a database other than the existing 'Boom' database
There's a WITH REPLACE option that allows you to proceed, but just avoid the indirection to begin with: restore the database, don't make a new one.
You'll probably need to delete the redundant Boom database you made, first. If, for whatever reason, you couldn't delete the database you'd have to use WITH REPLACE.
"Restore failed for Server 'Server B'.(Microsft.SqlServer.SmoExtended) Additional information: System.Data.SqlError: The backup set holds a backup of a database other than the existing 'Boom' database(Microsoft.SqlServer.Smo)."
I encountered this error when the logical name of the files do not match. Check on the logical name of the database you backed up and you will need the same logical name for the new database you created.
Or you can surely use the With Replace option too as specified by ta

How to get a copy of a SQL Server database

I am very new to database and programming.
I have created database on one PC. Now I need to get a copy of same DB and have it on my PC(including the tables and its data).
I have SQL Server 2005.
When I try to export the database it will ask for another machine.
Please provide me guidance or beginners level example link
There are plenty of ways to do this, but I'm a huge proponent of BACKUP and RESTORE.
Create a folder called c:\temp\ and give EVERYONE full control. Open a query window in SSMS.
BACKUP DATABASE database_name
TO DISK = 'C:\temp\database_name.BAK' WITH INIT;
Also run the following to determine the logical filenames:
USE database_name;
GO
EXEC sp_helpfile;
Now copy the file to the other machine (in a folder also called c:\temp). And in a query window on that instance:
RESTORE DATABASE database_name
FROM DISK = 'C:\temp\database_name.BAK'
WITH REPLACE, RECOVERY,
MOVE 'data file name' TO '...path...\database.mdf',
MOVE 'log file name' TO '...path...\database.ldf';
You'll need to replace some variables there but hopefully that provides the gist.
You can backup and restore the database.
Another method is to go to SQL Server Management studio and script out all objects in the database.
On the advanced tab, you can choose to have it script out the data in tables too.
To get to the wizard, right click on the database, choose "Generate Scripts", and follow the instructions in the wizard.
Then, you can save it out as a file. Or, you can just paste it into a SSMS window connected to the other database and run it.
You would not do this for a database that has any significant amount of data.
Best way to do this is to Backup and Restore like Aaron already suggested but here is another thing you can try.
Stop SQL Server services. (Go to SQL Server Configuration manager -> SQL Server Services -> Right click SQL Server service and click stop)
Go to SQL Server data folder (Right click on the database -> Properties -> Files)
Copy MDF and LDF files to another computer
Start the SQL Server services again from the SQL Server Configuration Manager
On target machine open SQL Server Management Studio, right click on the Databases folder and click Attach and select MDF and LDF files you copied.
this will help u...
go to your database,select database which you want to use on other pc,right click on it go to task->select detach,then it will show database name then click OK.
After this its .log and .mdf file goes to c:/program files/Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA .here you will find your database name 2 files ,from this you copy it and past it where you want it.you can get database on another pc .

SQL-Server: The backup set holds a backup of a database other than the existing

I am trying to restore a SQL Server backup file for my database, but it is throwing an error as follow:
The backup set holds a backup of a database other than the existing
My database in SQL Server 2008 and the backup file is in 2005.
What can be the problem?
I too came across this issue.
Solution :
Don't create an empty database and restore the .bak file on to it.
Use 'Restore Database' option accessible by right clicking the "Databases" branch of the SQL Server Management Studio and provide the database name while
providing the source to restore.
Also change the file names at "Files" if the other database still exists. Otherwise you get "The file '...' cannot be overwritten. It is being used by database 'yourFirstDb'".
Either:
1) Use WITH REPLACE while using the RESTORE command (if using the GUI, it is found under Options -> Overwrite the existing database (WITH REPLACE)).
2) Delete the older database which is conflicting and restore again using RESTORE command.
Check the link for more details.
First create a blank database of the same name. Then go for the restore option
Under Options on the left pane don't forget to select
Overwrite the existing database
Preserve the replication settings
That's it
I was facing same problem and found the solution by doing this, using SSMS 2014
Just select the Option Overwrite the existing database(WITH REPLACE)
This causes always due to version incompatibility.
follow these steps to solve:
Step 1: Create a database with your preferred name. (In our case AdventureWorks)
Step 2: Write click on the database and click on Tasks >> Restore >> Database…
Step 3: On the restore screen go to third selection of Options. Now select the checkbox “Overwrite the existing database (WITH REPLACE)”
Step 4: Click OK. It should successfully restore the database.
Note: When you restore a database WITH REPLACE it will overwrite the old database.
USE [master];
GO
CREATE DATABASE db;
GO
CREATE DATABASE db2;
GO
BACKUP DATABASE db TO DISK = 'c:\temp\db.bak' WITH INIT, COMPRESSION;
GO
RESTORE DATABASE db2
FROM DISK = 'c:\temp\db.bak'
WITH REPLACE,
MOVE 'db' TO 'c:\temp\db2.mdf',
MOVE 'db_log' TO 'c:\temp\db2.ldf';
Simple 3 steps:
1- Right click on database → Tasks → restore → Database
2- Check Device as source and locate .bak (or zipped .bak) file
3- In the left pane click on options and:
check Overwrite the existing database.
uncheck Take tail-log backup before restore
check Close existing connection to destination database.
Other options are really optional (and important of course)!
If you are using the script approach and have an error concerning the LDF and MDF files, you can first query the the backup file for the logical names (and other details) of files in the backup set, using the following:
-- Queries the backup file for the file list in backup set, where Type denotes
-- type of file. Can be L,D,F or S
-- info: https://learn.microsoft.com/en-us/sql/t-sql/statements/restore-statements-filelistonly-transact-sql
RESTORE FILELISTONLY FROM DISK = 'C:\Temp\DB_backup.bak'
GO
You will get results similar to the following:
And then you can use those logical names in the queries:
-- Script assumes you want MDF and LDF files restored on separate drives. Modify for your scenario
RESTORE DATABASE DB
FROM DISK='C:\Temp\DB_backup.bak'
WITH REPLACE,
MOVE 'DB' TO 'E:\MSSQL\Data\DB.mdf', -- "DB" is the mdf logical name from query above
MOVE 'DB_log' TO 'F:\MSSQL\Logs\DB.ldf'; -- "DB_log" is LDF logical name from query above
More info on RESTORE FILELISTONLY can be found from the SQL Server docs.
Its because the .mdf and .ldf Files from the original Db were locate at maybe c:\programFile\.... and this info is saved in the Backup!
If you create the same DB on a different SQL Server where the installation is on c:\program Files (x86)\ .... you can not restore as usually. You need to relocate the path for .mdf and .ldf Files.
Therefore:
Create a empty DB on the new Server
Right click on the empty Db > Tasks > Restore > Database > click Device select your .bak Files > Select Db to restore into
click on Files at left side > Select "Relocate all Files to Folder"
click Options on the left site > click on Overwrite
Done!
Hope it helps!
I had ran into similar problem today. Tried all the above solutions but didn't worked. So posting my solution here.
Don't forget to uncheck Tail-long Backup before restore
Hope it help others too!
Also as important is to make sure that, your database name matches the data base name in the backup you are trying to restore. If it does not match, you will get the same error.
system.data.sqlclient.sqlerror:The backup set holds a backup of a database other than the existing 'Dbname' database
I have came across to find soultion
Don't Create a database with the same name or different database name !Important.
right click the database | Tasks > Restore > Database
Under "Source for restore" select "From Device"
Select .bak file
Select the check box for the database in the gridview below
To DataBase: "Here You can type New Database Name" (Ex:DemoDB)
Don't select the Existing Database From DropDownlist
Now Click on Ok Button ,it will create a new Databse and restore all data from your .bak file .
you can get help from this link even
Hope it will help to sort out your issue...
Before doing anything else, confirm if your backup is Full or Differential. If you're trying to create a new database out of a differential backup, no matter what you do you will encounter the error.
Same issue with me.The solution for me is:
Right click on the database.
Select tasks, select restore database.
Click options on the left hand side.
Check first option OverWrite the existing database(WITH REPLACE).
Go to General, select source and destination database.
Click OK, that's it
I was just trying to solve this issue.
I'd tried everything from running as admin through to the suggestions found here and elsewhere; what solved it for me in the end was to check the "relocate files" option in the Files property tab.
Hopefully this helps somebody else.
Some of you have highly over complicated this. I found this to be extremely simple.
1) Create a database with the same name as your .bak file database name !Important
2) right click the database | Tasks > Restore > Database
3) Under "Source for restore" select "From Device"
4) Select .bak file
5) Select the check box for the database in the gridview below
6) Under "Select a Page" on the right Select "Options"
7) Select the checkbox labeled "Preserve the replication settings(WITH KEEP_REPLICATION)
Now Go back to the General page and click OK to restore the database...That is it.
I had to create new db on my local for testing & i had a back up from my prod. I created the db first and tried to run the BAK on top of the new db which produced this error for me. I deleted the db and restored it while sourcing the new db name in the restore screen itself. The db was automatically created on restore.
I got work done through alternate way, using Generate scripts. That did work for me as Backup-Restore didn't help to resolve the issue due to same error.
In the Options, change the "Restore As" file name to the new database mdf and ldf. It is referencing the source database .mdf and .ldf files.
You can restore to a new DB, verify the file name syntax, it ll be in the log file, for the new SQL version ll be a "_log" suffix
ad check the overwrite the existing database flag in option tab
Fabio
This helped me to import the back-up file from the system drive
Create a database with the same name(preferably) as your .bak file database name
Right click the database > Tasks > Restore > Database
Under "Source for restore" select "From Device"
Select the .bak file selecting the path from the system
Select the check box for the database in the list box below
Under "Select a Page" on the right Select "Options"
Select the checkbox labeled "Preserve the replication settings(WITH KEEP_REPLICATION)
Select the checkbox for Overwrite the existing database(WITH REPLACE)
Now Go back to the General page and click OK to restore the database...
Im sure this problem is related to the files and folders permissions.
I was trying to restore a production database to a staging database on the same server.
The only thing that worked in my case was restore to a new blank database. This worked great, did not try to overwrite production files (which it would if you just restore production backup file to existing staging database). Then delete old database and rename - the files will keep the new temp name but in my case that is fine.
(Or otherwise delete the staging database first and then you can restore to new database with same name as staging database)
instead of click on Restore Database click on Restore File and Filegroups..
thats work on my sql server
I had the same issue but on PS. I leave it here in case someone is trying to do the same
Restore-SqlDatabase -ServerInstance "<your instance name>" -Database "<your db name>" -BackupFile "<backup file route>.bak" -ReplaceDatabase
remember to use Set-ExecutionPolicy Unrestricted -Force and import import-module sqlps. Don't forget to set back your Execution Policy back to restricted once you are done.
Usually dealing with .bak files are coming with headaches, a more straight forward way is using sqldump files to transfer databases.
Generate script of current database you want to move or copy.
Instead of entire database, select All Tables.
From the option menu choose save as script, and click on advance button and set the following configs (I am suing MSSQL 2016):
Now, where you want to import these data, create a new database, switch to this new database (I mean set it as default)
Finally run the script, all table and data will be imported.
Good Luck.
List item

SQL Server: how do I export entire database?

I need to export database from one server and import it into another server.
How do I export the entire database to a file, or two files mdf, ldf (either option is fine)
How do I import it into a new server using ssms?
In the instructinos frmo ponies, it says:
In the To a point in time text box,
either retain the default (Most recent
possible) or select a specific date
and time by clicking the browse
button, which opens the Point in Time
Restore dialog box. For more
information, see How to: Restore to a
Point in Time (SQL Server Management
Studio).
To specify the source and location of the backup sets to restore, click
one of the following options:
From database
Enter a database name in the list box.
I am unable to type anything in restore
https://stackoverflow.com/questions/3241108/i-backed-up-the-database-in-ssms-sql-server-how-do-i-restore-it
Using SQL Server Management Studio, you use Backup/Restore feature. The Restore process is laid out in the MSDN documentation. And here's the MSDN article for backing up a database...
Obviously, you restore to another SQL Server database instance (a "database instance" can contain multiple databases.). The version of the instance can be newer than the version the backup came from - the compatibility level will just be set accordingly.
If you want to generate a .sql file:
right click on the database in SQL Server Management Studio
select Tasks->Generate Scripts... .
In the dialog that pops up, Select All on the screen that says "Select database objects to script"
The generated .sql file can be opened again in SQL Server Management Studio and be run. If the .sql file is very large, see How do you import a large MS SQL .sql file?
If you want to export / save all data of a database into a .sql file, do this:
Right click on the database in SQL Server Management Studio
Tasks -> Generate Scripts
Script entire database and all database objects
Next
Click - Advanced
Scroll down to Types of data to script and set from Schema only -> Schema and data -> Ok
Save as script file (name it and save it where you want it)
Next
Next
Done ✔️
You can open the file now and see that all values are also included now
The best way to do this is to backup the database. This will backup to one file. Then take that file to your new server and do a restore. This should restore everything, from tables to stored procedures to foreign keys and all the data.
Also, if you just want to move a database to a new server, Detach/Attach is a quicker option. Backup/Restore is what you want if you want to keep the original database in place.

How Do I Copy and Overwrite a Database in SQL Server 2005 with SSIS?

I HAVE to be missing something really simple here!
I have a database in my development environment called Project.
I have a database in my test environment called Project_UAT.
I've created an SSIS package that successfully copied my database from Project to Project_UAT. I'm pretty sure this eliminates most permission and configuration issues.
Now, I want to re-create the package and this time allow it to overwrite the destination, which is Project_UAT. This is simply because from time to time I want to click a button in the Microsoft SQL Management Studio that pushes the new database schema, data, users, and everything, out to my testing environment. I WANT to overwrite the data.
So I create the package just like I did before, but this time I specify the already-existing database name as the "Destination database" and I select the radio button called "Drop any database on the destination server with the same name, then continue with the database transfer, overwriting existing database files."
I click Next, and what does it tell me?
"Database name already exists at destination"
Well, I KNOW! I just told you I want to overwrite it!
How do I make this work?
Not sure if I am missing the point but why do you not use a task to drop/delete the existing database prior to your deployment step?
Perhaps you could qualify the SSIS Component Tasks you are using within your SSIS package.
Cheers, John
You can add an Execute SQL Task into the Control Flow to drop the database. Just set the SQLStatement property to
DROP DATABASE Project_UAT
After this step is executed the new copy of the Project_UAT database won't have to overwrite the old one.
I had this problem because I deleted the database before hand. The database is not in the destination folder, but SQL Server 2008 still thinks it is there. Refresh didn't work. And SQL Server wouldn't honor the selection of "Drop any database on the destination server..." It just complained that the database already existed.
Guys this is a common sense solution. A lot of complexity for nothing.
Backup the destination database you want to copy to and delete the destination database.
Open the copy database wizard and follow the steps.
Use the detach and attach method.
When you get to the configure destination database use the option if destination database exists select drop database on destination server with same name.
Now it will continue to the next screen.
But this only works if you delete the destination database first before starting the wizard.
I may have missed something but this worked for me.

Resources