Click here to Skip to main content
Licence CPOL
First Posted 5 Sep 2008
Views 134,518
Bookmarked 40 times

Migrate MySQL to Microsoft SQL Server

By Niklas Henricson | 5 Sep 2008
Migrate MySQL to Microsoft SQL Server in just a few simple steps
 
Part of The SQL Zone sponsored by
See Also

1
1 vote, 5.3%
2

3
3 votes, 15.8%
4
15 votes, 78.9%
5
4.91/5 - 19 votes
1 removed
μ 4.65, σa 1.32 [?]

Introduction

This article describes a few simple steps in order to migrate MySQL into Microsoft SQL Server 2005. The technique is very easy, but useful if you plan to move your data from MySQL and upgrade it finally to a Microsoft SQL Server environment.

Background

Initially, I started my search for an article on CodeProject regarding MySQL->MS SQL migration without any success. I had an old PHPbb forum running, that needed to be upgraded to a Microsoft environment entirely. I could have just kept MySQL and Apache server, but instead I decided to migrate the entire concept of PHPbb to a YAF-forum.

Setup ODBC Connection to MySQL Database

This article will not go through how to setup a MySQL or Microsoft SQL server, but make sure you have downloaded at least the MySQL ODBC Connector from here.

For this article, I downloaded the MySQL ODBC Connector 5.1.

The setup of this connector is pretty simple:

  • Open your ODBC Data Source Administrator from the Control Panel -> Administrative Tools. Under the tab labelled as "System DSN", press the "Add" button.

    Setup_ODBC1.jpg

  • On the "Create New Data Source" dialog that appeared, choose MySQL ODBC 5.1 Driver and then press the "Finish" button.

    Setup_ODBC2.jpg

  • After that, a MySQL connection configuration dialog will appear. Add your MySQL database account information in it, preferably the "root" account which has full access to your databases in MySQL. In this case, my database is called "tigerdb". Do not change the port to anything other than 3306, unless during your MySQL server installation, you have defined something else.

    Setup_ODBC3.jpg

  • Press the "Test" button to ensure your connection settings are set properly and then the "OK" button when you're done.

Create a Microsoft SQL Link to your MySQL Database

In this state, you are ready to establish a link towards MySQL database from your Microsoft SQL Server Management Studio. Open a query window and run the following SQL statement:

EXEC master.dbo.sp_addlinkedserver 
@server = N'MYSQL', 
@srvproduct=N'MySQL', 
@provider=N'MSDASQL', 
@provstr=N'DRIVER={MySQL ODBC 5.1 Driver}; SERVER=localhost; _
	DATABASE=tigerdb; USER=root; PASSWORD=hejsan; OPTION=3'

This script will produce a link to your MySQL database through the ODBC connection you just created in the previous stage of this article. The link will appear in the Microsoft SQL Server Management Studio like this:

Create_link.jpg

If it doesn't show up in the treeview, press the refresh button.

Import Data between the Databases

Create a new database in Microsoft SQL Server. I called mine "testMySQL". In the query window, run the following SQL statement to import table shoutbox from the MySQL database tigerdb, into the newly created database in Microsoft SQL called testMySQL.

SELECT * INTO testMySQL.dbo.shoutbox
FROM openquery(MYSQL, 'SELECT * FROM tigerdb.shoutbox')

That's it!

Points of Interest

During this migration, I had to import lately my newly migrated database into the structure of "Yet Another Forum" tables. For that, I used a series of SQL-scripts. However I am not going to post them here. If folks leave comments here about the need for these scripts, just tell me and I will gladly change this article and start adding them. You're welcome to post your comments.

Another issue you will most likely encounter are the differences between these two databases based on datatypes. I would suggest to proceed with a reverse engineering of your MySQL database (for example, Visio is one application that provides reverse engineering functionality) and start mapping all the differences and potential risks of losing parts of data for instance, within varchar columns.

Microsoft SQL datatypes: http://msdn.microsoft.com/en-us/library/aa258271.aspx

MySQL datatypes: http://dev.mysql.com/tech-resources/articles/visual-basic-datatypes.html

History

  • 2008-09-05: First version of this article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Niklas Henricson

Web Developer
Cybercom Group South AB
Sweden Sweden

Member
Niklas Henricson began with programming in 1999 by working for the Swedish Working Life Enviroment authority.
 
Since then, he has been involved in numerous projects involving MTS/COM+, VB 6.0, ISE Eiffel 4.5, ASP.NET/C#/VB.NET as well as common ASP, and finally database enviroments based on Oracle, MySQL and MS SQL.
 
Between April 2007 and December 2008, Niklas worked as consultant for Mandator AB with some of his assignments spended in Ericsson.
 
Today he works as a senior consultat at Cybercom Group in Sweden assigned in a project for the Swedish Institute for Infectious Disease Control.
 
One of his most favorite books regarding programming is "The Pragmatic Programmer". Something he is recommending everyone to read.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionProblem to migrate a whole mysql DB into MSSQL 2008 PinmemberKodjak1:59 25 Nov '11  
AnswerRe: Problem to migrate a whole mysql DB into MSSQL 2008 PinmemberKodjak4:32 25 Nov '11  
Questionproblem PinmemberMihaly Sogorka6:12 9 Nov '11  
AnswerRe: problem PinmemberNiklas Henricson7:43 9 Nov '11  
QuestionError to mygrate data Pinmemberratulalam22:56 7 Oct '11  
AnswerRe: Error to mygrate data PinmemberNiklas Henricson7:44 9 Nov '11  
QuestionDon't see MySQL option when adding new ODBC data source in control panel Pinmemberhalifaxdal2:59 18 Sep '11  
AnswerRe: Don't see MySQL option when adding new ODBC data source in control panel PinmemberNiklas Henricson7:48 9 Nov '11  
QuestionHow can loop through all tables in my db Pinmemberaramosvizcarra5:14 15 Sep '11  
GeneralMigration another solution PinmemberTheAceSolutions4:07 14 May '11  
GeneralRe: Migration another solution PinmemberNiklas Henricson5:22 14 May '11  
GeneralError converting data type DBTYPE_DBDATE to datetime. PinmemberNasser01919:21 23 Apr '11  
GeneralRe: Error converting data type DBTYPE_DBDATE to datetime. PinmemberNiklas Henricson5:26 14 May '11  
QuestionMultiple tables? Pinmembersillicoid2:08 5 Mar '11  
AnswerRe: Multiple tables? PinmemberNiklas Henricson5:28 14 May '11  
GeneralVery cool, but what about large DBs Pinmembersheph3:09 25 Aug '10  
GeneralRe: Very cool, but what about large DBs Pinmembersheph3:54 25 Aug '10  
GeneralMy vote of 5 PinmemberS.H.Bouwhuis2:34 5 Jul '10  
GeneralRe: My vote of 5 PinmemberNiklas Henricson5:29 14 May '11  
GeneralVery much thanks Pinmembersahil31_mohali3:10 20 Mar '10  
GeneralRe: Very much thanks PinmemberNiklas Henricson5:30 14 May '11  
GeneralGood Job PinmemberMike Hankey22:03 6 Sep '09  
NewsPerfecto Pinmembercmschick20:00 8 Jul '09  
AnswerRe: Perfecto PinmemberNiklas Henricson22:51 8 Jul '09  
GeneralProblem with initializing the data source object of OLE DB PinmemberAhmed Barradah9:26 2 Jul '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 5 Sep 2008
Article Copyright 2008 by Niklas Henricson
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid