Database migration is a common task when moving to a new server, creating backups, or setting up development/staging environments. In this guide, we’ll walk through the complete process of migrating a database from a source server to a destination server, including both the database structure and data.
We have:
TestDB with a cities tableTestDBFirst, let’s create our sample database on the source server:
-- Connect to Source Server in SSMS -- Create TestDB database CREATE DATABASE TestDB; GO USE TestDB; GO -- Create cities table CREATE TABLE cities ( city_id INT IDENTITY(1,1) PRIMARY KEY, city_name NVARCHAR(100) NOT NULL, country NVARCHAR(100) NOT NULL, population INT, established_year INT ); GO -- Insert sample data (5 cities) INSERT INTO cities (city_name, country, population, established_year) VALUES ('New York', 'United States', 8336817, 1624), ('London', 'United Kingdom', 9002488, 43), ('Tokyo', 'Japan', 13960000, 1603), ('Dubai', 'United Arab Emirates', 3331420, 1833), ('Sydney', 'Australia', 5312163, 1788); GO -- Verify data SELECT * FROM cities;
Now let’s create a script to migrate the database structure to the destination server.
In SSMS, connect to your Source Server
In Object Explorer (left panel), locate your database
Right-click on TestDB database
Select Tasks → Generate Scripts…
dbo.citiesC:\Temp\TestDB_Schema.sqlThe wizard will generate a SQL script file containing all CREATE statements for your database objects.
Connect to your Destination Server in SSMS and run:
-- Create the database on destination server CREATE DATABASE TestDB; GO USE TestDB; GO
If you want the script to create the database automatically:
CREATE DATABASE statementsC:\Temp\TestDB_Schema.sqlTestDB (if dropdown is empty)Your database structure is now created on the destination server!
-- Run on Destination Server USE TestDB; GO -- Check if table exists SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'cities'; -- Check table structure SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'cities';
Now that we have the database structure in place, let’s copy the data.
Connect to Source Server in SSMS
Right-click TestDB → Tasks → Generate Scripts
Select the cities table
Click Next → Advanced
Change “Types of data to script” to “Data only”
Save to file: C:\Temp\TestDB_Data.sql
Click Next → Finish
Step 2: Execute Data Script on Destination
TestDB-- The generated script will look something like this: INSERT INTO [dbo].[cities] (city_name, country, population, established_year) VALUES ('New York', 'United States', 8336817, 1624), ('London', 'United Kingdom', 9002488, 43), ('Tokyo', 'Japan', 13960000, 1603), ('Dubai', 'United Arab Emirates', 3331420, 1833), ('Sydney', 'Australia', 5312163, 1788);
TestDB → Tasks → Export DataMicrosoft OLE DB Driver for SQL ServerTestDBMicrosoft OLE DB Driver for SQL ServerTestDB[dbo].[cities]The wizard will copy all data directly from source to destination.Summary of Steps
\


