SQL Server: how do I export entire database? - sql-server

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.

Related

How to move SQL Server Database to another?

Originally, I created my database using SQL Server Management Studio on my local machine.
I now want to copy it into my App_Data in my web site folder, and in this way I hope to move my web site freely, meanwhile moving my database which contained in my App_Data to the other machine.
What steps should I follow?
I'm using SQL Server 2008 Express.
There are many way to transfer running database.
Using backup restore. refer microsoft
Using Replication.
Detach and Attach is not the right way because there is chance of data loss.
There are many ways to move/copy a database from one server to another server using SQL Server Tools.
i) Detach the database from the old server and attach it in the new server. This is purely offline operation and it moves the database instead of copying it.
Refer http://msdn.microsoft.com/en-us/library/ms190209.aspx : how to use attach/detach database in SQL Server Management Studio(SSMS)
ii) Back up the database in the old server and restore it in the destination server. This can be performed during online and it creates a new database in the destination server.
Refer http://msdn.microsoft.com/en-us/library/ms187048.aspx for more information.
iii) Using copy database wizard in SQL Server Management Studio.
Select the database in the source server in SSMS
Right click ->Tasks->Copy Database Wizard to launch the copy database wizard.
Enter the source and destination credentials and select either attach/detach or SMO type
click next and you can schedule or run immediately
Click finish to execute it
Refer http://msdn.microsoft.com/en-us/library/ms188664.aspx for more information.
iv) The last type is to generate the create script using Generate Script Wizard (SSMS) and execute it in the destination server.
Select the database in the source server in SSMS
Right click ->Tasks->Generate Scripts Wizard to launch the wizard.
Select the various scripting options needed and select the objects needed to generate the scripts for them. Make sure script data = true in the scripting option to generate script for data as well (INSERT statements)
click next ->next and finish to generate the script (new query window or clip board or file)
connect to the destination server and create the new database in it.
Click new query window and paste the script generated using GSW above and execute them with the destination database context.
Refer http://msdn.microsoft.com/en-us/library/ms181421.aspx for more information
v) Using Transfer Object in SMO
Sample code:
ScriptingOptions so = new ScriptingOptions();
so.ScriptData = true;
Transfer t = new Transfer(db);
t.CopyAllObjets = true;
t.options = so;
...................
..................
t.TransferData();
Note: Transfer class is available in Microsoft.SqlServer.SmoExtendedClass.dll (SQL Server 2008)
or Microsoft.SqlServer.Smo.dll (SQL Server 2005)
There are various member variables to be configurable. Also ScriptingOptions class object can be created and assigned to the transfer object as well.
set ScriptData = true in order to transfer data also. It copies the destination instead of moving the database
Refer http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.transfer.aspx for more information.
vi) You can make use of Database Publishing Wizard to accomplish this. You can specify the target version as SQL 2005 or SQL 2000 etc as per your requirement.

How to import a bak file into SQL Server Express

I have a .bak file, and I want to use this file to recreate the database in a fresh install of SQL Server 2008 Management Studio.
Can someone point me in the right direction on how this can be done?
I have tried:
right click on the Databases container within object explorer
from context menu select Restore database
Specify To Database as either a new or existing database
Specify Source for restore as from device
Select Backup media as File
Click the Add button and browse to the location of the BAK file
but I am getting error that
Restore failed for Server 'MyServer-PC'. (Microsoft.SqlServer.SmoExtended)
ADDITIONAL INFORMATION:
System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing 'invent' database. (Microsoft.SqlServer.Smo)
There is a step by step explanation (with pictures) available # Restore DataBase
Click Start, select All Programs, click Microsoft SQL Server 2008 and select SQL Server Management Studio.
This will bring up the Connect to Server dialog box.
Ensure that the Server name YourServerName and that Authentication is set to Windows Authentication.
Click Connect.
On the right, right-click Databases and select Restore Database.
This will bring up the Restore Database window.
On the Restore Database screen, select the From Device radio button and click the "..." box.
This will bring up the Specify Backup screen.
On the Specify Backup screen, click Add.
This will bring up the Locate Backup File.
Select the DBBackup folder and chose your BackUp File(s).
On the Restore Database screen, under Select the backup sets to restore: place a check in the Restore box, next to your data and in the drop-down next to To database: select DbName.
You're done.
To do this via TSQL (ssms query window or sqlcmd.exe) just run:
RESTORE DATABASE MyDatabase FROM DISK='c:\backups\MyDataBase1.bak'
To do it via GUI - open SSMS, right click on Databases and follow the steps below
Using management studio the procedure can be done as follows
right click on the Databases container within object explorer
from context menu select Restore database
Specify To Database as either a new or existing database
Specify Source for restore as from device
Select Backup media as File
Click the Add button and browse to the location of the BAK file
refer
You'll need to specify the WITH REPLACE option to overwrite the existing adventure_second database with a backup taken from a different database.
Click option menu and tick Overwrite the existing database(With replace)
Reference
I had the same error. What worked for me is when you go for the SMSS GUI option, look at General, Files in Options settings.
After I did that (replace DB, set location) all went well.
Restoring a Database from Backup
sql-server-->connect to instance-->Databases-->right-click on databases-->Restore
DataBase..-->Device-->Add-->choose the path_filename(.bak)-->click OK

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 .

Best (easiest) way to make a SQL Server dump and import that dump in another SQL Server

I would like to achieve a database export (dump) in SQL Server from one server and import that dump in another SQL Server and not necessarily in the same schema name.
For example if I have a database prepared with all the data set for implement a new DB for a new customer, that db is for example named DB_EMPTY
And then I have to setup the same DB on some external server for a customer for example in the schema DB_MY_CUSTOMER
What is the best/simplest way to export (dump) a DB_EMPTY, and import it in DB_MY_CUSTOMER?
Possibly with SQL Server Management Studio?
An easy way would be to use SQL Server Management Studio, in the Object Explorer right click on the database you want to export, select Tasks -> Back Up, then select a destination and file name in the Destination box at the bottom of the dialog. You can play around with the various settings, but you don't need to.
To restore it on another server is basically the opposite, choose Tasks -> Restore -> Database, in the dialog select From Device, then click the browse ellipsis, from there you get a browse dialog, click Add and then navigate to the back up file you created. You can change the databse name in the To database textbox, and control where the files get stored by going to the Options tab and altering the Restore As column entries in the table labelled as Restore the database files as:.
Just in case someone is ending here and noticing "Tasks -> Back up" option does not exists; on recent versions of SSMS ( v18 for example ), you'll need to use "Generate scripts" option and then on "Advanced" select "Schema and Data" on "Types of data to script".
Credits to: http://statmap.co.uk/?page_id=9207
This is called replication : http://databases.about.com/od/sqlserver/ht/distribution.htm
Try to look to the snapshot replication. It is configurable to determine the destination database.

copy a database within SQL Server Express?

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.

Resources