Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Scheduled data transfer from SQL Server to MySQL Expand / Collapse
Posted Friday, October 15, 2010 12:51 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Today @ 11:00 AM
Points: 792, Visits: 483
I work in an environment where from time to time I have to export data from a SQL Server database to a MySQL database. Currently this is a fiddly manual process (done with linked tables both ways in Access, with a local temp table).
Needless to say I have been looking into automating this, and in doing so have found major shortfalls in the ability of SSIS to export data from SQL Server to a MySQL database (going the other way is no problem at all).

The most reliable way I have found so far is via a Powershell script. The script below uses the AdventureWorks 2008 database as its source and expects the target tables to be present.

# Function to  transfer data from MS SQL SErver to MySQL
# requires MySQL Connector for .Net 6.2.3 and Powershell 1.0 runtime installed
# no ODBC data source required
# MySql Connector for .Net homepage:
# J Barnett, Sep-Oct 2010.

function CopyDataFromMSSqlToMySql {

##$file = "C:\\Program Files\\MySQL\\MySQL Connector Net 6.2.3\\Assemblies\\MySql.Data.dll\"

# define connection strings
$mssql_connectionString = "Server=(local);Integrated Security=SSPI;Database=Adventureworks"
$mysql_connectionString = "server=localhost;uid=xxxxxx;pwd=yyyyyy;database=mydb;"

# Create MS SqlConnection object and define connection string
$mssql_con = New-Object System.Data.SqlClient.SqlConnection
$mssql_con.ConnectionString = $mssql_connectionString

# Create SqlCommand object, define command text, and set the connection
$mssql_cmd = New-Object System.Data.SqlClient.SqlCommand
$mssql_cmd.CommandText = "select EmployeeID, replace (LoginID, '\', '\\') as loginid, cast(CAST(YEAR(birthdate) AS VARCHAR(4)) + '-' +
CAST(MONTH(birthdate) AS VARCHAR(2)) + '-' + CAST(DAY(birthdate) AS VARCHAR(2)) as varchar (10)) BirthDate from HumanResources.employee;"
$mssql_cmd.Connection = $mssql_con

# Create SqlDataReader
$dr = $mssql_cmd.ExecuteReader()

If ($dr.HasRows)

# connect to MySQL

$mysql_connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$mysql_connection.ConnectionString = $mysql_connectionString
$mysql_cmd = New-Object MySql.Data.MySqlClient.MySqlCommand

Write-Host "Number of fields: " $dr.FieldCount

While ($dr.Read())
$employeeid = $dr["employeeid"]
$loginid = $dr["loginid"]
$birthdate = $dr["birthdate"]

##$mysql_command = "insert into employees (employeeid, loginid, birthdate) values ('$employeeid', '$loginid', '$birthdate' )"
$mysql_command = "replace into employees (employeeid, loginid, birthdate) values ('$employeeid', '$loginid', '$birthdate')"
Write-Host $mysql_command # don't run it yet

$mysql_cmd.connection = $mysql_connection
$mysql_cmd.CommandText = $mysql_command
$mysql_da = New-Object MySql.Data.MySqlClient.MySqlDataAdapter ($mysql_cmd)
$dataSet = New-Object System.Data.DataSet
$mysql_da.Fill($dataSet, "test1")


# Close MySQL connection
Write-Host "No matching source data"


# Close the data reader and the connection

NB for those of you who don't usually deal with MySQL this contains a few nuggets you may not be familiar with that I have had to deal with:
1. Dates are retrieved by Powershell as dd/mm/yyyy whereas they need to be in ANSI format to be inserted into the MySQL database without errors.
2. "replace into" is MySQL specific syntax. It basically combines an insert into (to add rows that don't exist) and an update on the primary key value where it differs.
3. The single \ character is doubled up as it is treated as a delimiter otherwise.

I would be the first to admit I am not an SSIS guru (which would be my ideal way), but does anybody have any ideas on how to improve on this or ideas on getting a functional equivalent working via integration services (or a better way altogether)? Our MySQL Servers are linux based and they don't have drivers installed for connecting to MSSQL servers (and I can't seem to persuade the administrators of those server to install them either).

Post #1005503
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse