Import MySQL into MSSQL (free/easy)

Jul 27, 2008

I needed to move several medium size MySQL backend apps to SQL server 2005 and did not have a cool tool for doing it. I found a way that was suprisingly easy... To do this you will need to install the ODBC drivers for your version of MySQL; I'm using MySQL 5.

Then in your SQL Management Studio, modify and run the following code.

EXEC master.dbo.sp_addlinkedserver
    @server = N'MYSQL',
    @srvproduct=N'MySQL',
    @provider=N'MSDASQL',
    @provstr=N'DRIVER={MySQL ODBC 5.1 Driver}; SERVER=127.0.0.1; DATABASE=db_name; USER=user; PASSWORD=pass; OPTION=3'

Then write seperate scripts that contain an instance of the following pieces of code per table in your MySQL db. In my case I had about 40 tables, so I wrote a ColdFusion job to do it smartly and added SQL to drop the target table if it existed.

SELECT * INTO [target_mssql_db].dbo.[target_mssql_table]
FROM openquery(MySQL, 'select * from mysql_db_name.mysql_tablename')

One of my apps had some tables with LOTS of rows, so I also set my script up to only run 5 conversions at a time to avoid timing out MSSQL locks. This should also work on MSSQL 2000 and I'm sure at least a few versions back of MySQL.

Comments

brandon

brandon wrote on 07/28/08 9:10 AM

How about advice for the other way? ... MSSQL to MySQL
Russell Brown

Russell Brown wrote on 07/28/08 9:15 AM

Buy NaviCat [http://www.navicat.com] which you will probably want if you have any MySQL DBs anyway and just do an import via ODBC.
Russell Brown

Russell Brown wrote on 08/31/08 8:43 PM

MSSQL Connection Example EXEC sp_addlinkedserver @server='ConnectionAliasName', @srvproduct='', @provider='SQLNCLI', @datasrc='ServerName\\Instance (if one)' GO EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'ConnectionAliasName', @locallogin = NULL , @useself = N'False', @rmtuser = N'UserName', @rmtpassword = N'Password'
Porfirio

Porfirio wrote on 12/03/08 8:52 PM

Thanks this was something that i really need it to know
Babar

Babar wrote on 04/13/09 8:20 AM

I'm beginner. i have executed your give code... but it doesn't work following error occurred.

Msg 15247, Level 16, State 1, Procedure sp_MSaddserver_internal, Line 36
User does not have permission to perform this action.
Koushik

Koushik wrote on 10/08/09 8:55 AM

This code is very good. But this can be achieved in other way.
1. Install MySQL odbc driver from http://dev.mysql.com/downloads/connector/odbc/5.1.html,
2. Create ODBC Data source (system DSN,User DSN or File DSN available in administrative tools
3. Now SQL server's Import/Export wizard can be used to move data between SQL server and MySQL.
Dunit

Dunit wrote on 11/18/09 3:57 AM

Have had many issues with the mysql odbc driver in SQL Servers import/export wizard, around the addition of double quotes.
Although this was a little more manual, it worked a treat.
Thanks for the idea.
V. Leela Madhav

V. Leela Madhav wrote on 01/03/10 7:58 AM

Thanks a lot. This solution has helped me to save a lot of time and made my job to complete it easily.
stefan

stefan wrote on 07/19/10 4:09 PM

Hello I get access denied with. USER=user .. I also tryed to set USER=root but get the same error that access denide. Thanks for help about how to find the correct USER and how and where to set this. :-)

Write your comment



(it will not be displayed)



Subscribe to this comment thread