How to copy a database from one computer to another? - sql-server

I have a database in SQL Server 2008, which I want to copy to another computer.
How do I make such a copy?
Once this is done, what should I do at the other computer to build the database once again from my copy?

Using SQL Server Management Studio, here are the steps:
1.Right-click the database and select Tasks | Backup
2.Make sure that the Backup type is Full
3.Click Add and specify the location and backup name
4.Copy the created backup file to another computer
5.In SQL Server Management Studio on another computer, right-click the SQL Server instance and select Restore Database
6.Select Device and click the elipsis button to navigate to the copied backup file

Simple Answer: Back it up, then restore it on the other computer.
Have a look here: http://technet.microsoft.com/en-us/library/cc966495.aspx
There's a lot of stuff there, but essentially, right click on the database, Tasks > Backup. Fill in the options to perform a full backup to somewhere.
Once it has created the backup (one big file, by convention with a BAK extension), on the second computer, right click the Databases folder, Restore Database and follow the prompts.
You can do it as well in SQL if you wish:
Backup: http://msdn.microsoft.com/en-us/library/ms186865.aspx
Restore: http://msdn.microsoft.com/en-us/library/ms186858.aspx
There might be times when it's better to detach and move, but this approach always feels a bit safer!
This will copy both structure and the data in the database.

Use TSQL backup and restore. This should help http://www.sqlmag.com/article/tsql3/use-t-sql-to-back-up-and-restore-sql-server-user-databases

One of the easiest ways to do this is back up and restore.
http://msdn.microsoft.com/en-us/library/ms187048.aspx
Another option you have is detaching your database from SQL Server, copying the MDF and LDF files to the target machine, and re-attaching there. Note, if you want to keep a copy of the database on the original server, you will also need to attach there too.
http://msdn.microsoft.com/en-us/library/ms190794.aspx

You can dump the content of your database and restore it on another machine. The same mechanism can be used also for backup purposes. Check the backup&restore functionality of MS SQL.

Related

what and where can I find the database/table files for sql server 2005?

In my sql server 2005 I have a database named movies . In this database I have a table named list. I wanted to test my application on some another machine. Is there any way I can take with me that database or tables so that there is no need to create them again there. What type of files are they if there is a way out other than creating a new database and tables again
One thing you can do is to take a local backup of the database (if you you want all the transactions as well include the transaction log file). Then put that .BAK file on a thumb drive and go to the other machine. Once at the other machine you can "Restore" the .BAK as the database on this new machine.
Here is a screen shot of the "Back Up..." and restore options:
You'll want to 1st back it up to either a network location or even onto say the desktop of the first machine. You can keep most of the defaults when you do a backup and I would recommend a "FULL BACKUP". Once you click ok, it will generate a Database.BAK file. Then you can take this file to be used when you do a "Restore" on the other machine. The restore can have the same database name or a different one on this 2nd machine.

Opening a SQL Server .bak file (Not restoring!)

I have been reading a LOT of google posts and StackOverflow questions about how to restore a database in SQL Server from a .bak file.
But none of them states how to just READ the tables in the database-backup.
(None that I could find anyway?)
I just want to check out some old information which now has been deleted, without actually restoring the full database.
Is this possible?
.
EDIT:
I just wanted to post my T-SQL solution to the problem, so others may use it and I can go back and look it up ;)
First I created a new database called backup_lookup and took it offline.
After this I could restore my old database mydb to the new one, without ever touching my original.
USE master
GO
RESTORE DATABASE backup_lookup
FROM DISK = 'D:\backup\mydb.bak'
WITH REPLACE,
MOVE 'mydb' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup.mdf',
MOVE 'mydb_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup_log.ldf'
GO
I hope this helps :)
From SQL Server 2008 SSMS (SQL Server Management Studio), simply:
Connect to your database instance (for example, "localhost\sqlexpress")
Either:
a) Select the database you want to restore to; or, alternatively
b) Just create a new, empty database to restore to.
Right-click, Tasks, Restore, Database
Device, [...], Add, Browse to your .bak file
Select the backup.
Choose "overwrite=Y" under options.
Restore the database
It should say "100% complete", and your database should be on-line.
PS: Again, I emphasize: you can easily do this on a "scratch database" - you do not need to overwrite your current database. But you do need to RESTORE.
PPS: You can also accomplish the same thing with T-SQL commands, if you wished to script it.
The only workable solution is to restore the .bak file. The contents and the structure of those files are not documented and therefore, there's really no way (other than an awful hack) to get this to work - definitely not worth your time and the effort!
The only tool I'm aware of that can make sense of .bak files without restoring them is Red-Gate SQL Compare Professional (and the accompanying SQL Data Compare) which allow you to compare your database structure against the contents of a .bak file. Red-Gate tools are absolutely marvelous - highly recommended and well worth every penny they cost!
And I just checked their web site - it does seem that you can indeed restore a single table from out of a .bak file with SQL Compare Pro ! :-)
There is no standard way to do this. You need to use 3rd party tools such as ApexSQL Restore or SQL Virtual Restore. These tools don’t really read the backup file directly. They get SQL Server to “think” of backup files as if these were live databases.
Just to add my TSQL-scripted solution:
First of all; add a new database named backup_lookup.
Then just run this script, inserting your own databases' root path and backup filepath
USE [master]
GO
RESTORE DATABASE backup_lookup
FROM DISK = 'C:\backup.bak'
WITH REPLACE,
MOVE 'Old Database Name' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup.mdf',
MOVE 'Old Database Name_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\backup_lookup_log.ldf'
GO
It doesn't seem possible with SQL Server 2008 alone. You're going to need a third-party tool's help.
It will help you make your .bak act like a live database:
http://www.red-gate.com/products/dba/sql-virtual-restore/

Backup remote SQL Server database to local

I'm trying to backup a live database to my computer and I can't find the option to do it. I'm connecting to it using Microsoft SQL Server Management Studio 2008 R2. I'm a MySQL monkey, so I'm used to being able to backup to .sql files and move them around.
Anyone have any idea how I can create a file backup of the database? I've found the backup option which only backs up on the server, or the export, which seems to only allow a single table, or code an SQL query, which I'm not too sure on, short of putting in something like SHOW TABLES;
Anyone have any ideas? I'm limited to readonly access for various reasons, nothing bad, promise!
You will only be able to backup the database to a location the service account for SQL has access to. If you have access to a central share on the server/network that you can access and the service can, you might backup to that location and then browse from your computer to pull it down.
If you are just wanting the database structure you could script the database out to a file. This would let you save it locally. If you also want the data though doing a full backup is the quickest way I know of.
EDIT
I would use the T-SQL BACKUP comand and include WITH COPY_ONLY to backup the database, since you stated this is a "live" database. If a scheduled job is performing backups against the database and you break in to do an additional one you will effect the backup recovery chain of the database. Using the COPY_ONLY will allow you to get a backup of the database without requiring it in the event of a recovery need.
You can also create sql dumps with Management Studio.
Right-click the database and select Tasks - Generate Scripts. This will open a wizard that allows you to select what the dump should include (e.g. tables, indices, views, ...).
Make sure you set "Script Data" to true if you want your dump to include inserts.
You can enter a valid UNC path in the Backup option.

Copy entire database contents (schema and data)

I need to copy the contents (tables, views, procs, DATA, etc.) of a SQL Server database and copy it to another SQL Server database. I need to do this entirely in a script as I am not able to carry over files to the destination web server. Using Microsoft SQL Server Management Studio, how can I accomplish this?
Note: The database I am copying to will have the same name and be completely empty.
You can script the database schema and data using SSMS
Right click on the database. Choose tasks....then choose generate sql scripts.
Specify all objects. Then in the options menu choose at the bottom to generate scripts for data.
Below are some links that will be helpful
http://blog.sqlauthority.com/2007/08/21/sql-server-2005-create-script-to-copy-database-schema-and-all-the-objects-stored-procedure-functions-triggers-tables-views-constraints-and-all-other-database-objects/
http://blog.sqlauthority.com/2007/11/16/sql-server-2005-generate-script-with-data-from-database-database-publishing-wizard/
You can backup the database and restore to any instance you'd like. Since I don't like posting the "how to" when I learned it someplace else originally - here's the MSDN describing the process.
http://msdn.microsoft.com/en-us/library/ms187048.aspx
It is how we do things in our production environment. We back up and restore to our other instances.
The easiest way would be to restore from a backup, but since you can't copy files to the file system, the Copy database wizard will do this.
http://msdn.microsoft.com/en-us/library/ms188664.aspx

How best to copy entire databases in MS SQL Server?

I need to copy about 40 databases from one server to another. The new databases should have new names, but all the same tables, data and indexes as the original databases. So far I've been:
1) creating each destination database
2) using the "Tasks->Export Data" command to create and populate tables for each database individually
3) rebuilding all of the indexes for each database with a SQL script
Only three steps per database, but I'll bet there's an easier way. Do any MS SQL Server experts out there have any advice?
Given that you're performing this on multiple databases -- you want a simple scripted solution, not a point and click solution.
This is a backup script that i keep around.
Get it working for one file and then modify it for many.
(on source server...)
BACKUP DATABASE Northwind
TO DISK = 'c:\Northwind.bak'
(target server...)
RESTORE FILELISTONLY
FROM DISK = 'c:\Northwind.bak'
(look at the device names... and determine where you want the mdf and
ldf files to go on this target server)
RESTORE DATABASE TestDB
FROM DISK = 'c:\Northwind.bak'
WITH MOVE 'Northwind' TO 'c:\test\testdb.mdf',
MOVE 'Northwind_log' TO 'c:\test\testdb.ldf'
GO
Maybe the easiest is to detach/reattach. Right-click in the server manager on the DB, tasks --> detach. Then copy the MDF/LDF files to the new server and then reattach by clicking on the server icon and tasks-->attach. It will ask you for the MDF file - make sure the name etc is accurate.
In order of ease
stop server/fcopy/attach is probably easiest.
backup/restore - can be done disconnected pretty simple and easy
transfer DTS task - needs file copy permissions
replication - furthest from simple to setup
Things to think about permissions, users and groups at the destination server esp. if you're transferring or restoring.
There are better answers already but this is an 'also ran' because it is just another option.
For the low low price of free you could look at the Microsoft SQL Server Database Publishing Wizard. This tool allows you to script the schema, data or data and schema. Plus is can be run from a UI or command line <- think CI process.
Backup -> Restore is the simplest, if not to use the replication.
If you use the Backup/Restore solution you're likely to have orphaned users so be sure to check out this article<microsoft> on how to fix them.
Another one to check out that is quick and simple:
Simple SQL BULK Copy
http://projects.c3o.com/files/3/plugins/entry11.aspx
Backup the databases using the standard SQL backup tool in Enterprise Manager, then when you restore on the second server you can specify the name of the new database.
This is the best way to maintain the schema in its entirety.
use backups to restore the databases to the new server with the new names.
Redgate SQL Compare and SQL Data Compare. The Comparison Bundle was by far the best investment a company I worked for ever made. Moving e-training content was a breeze with it.
Check those links:
For multiple db's backup
and single db restore

Resources