<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; data integration</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/data-integration/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Insert data into MySQL with T-SQL</title>
		<link>http://www.youdidwhatwithtsql.com/insert-data-mysql-tsql/4</link>
		<comments>http://www.youdidwhatwithtsql.com/insert-data-mysql-tsql/4#comments</comments>
		<pubDate>Thu, 19 Feb 2009 22:03:07 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[data integration]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[sql server 2005]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[xml documents]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=4</guid>
		<description><![CDATA[One of the killer features of SQL Server is the ability to retrieve data from almost any source. Want to query MySQL, Access databases, text files, Active Directory, Exchange mailboxes or XML documents? All this is possible with SQL Server and is relatively simple to do so and all without resorting to SSIS. Getting data [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/insert-data-mysql-tsql/4">Insert data into MySQL with T-SQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>One of the killer features of SQL Server is the ability to retrieve data from almost any source. Want to query MySQL, Access databases, text files, Active Directory, Exchange mailboxes or XML documents? All this is possible with SQL Server and is relatively simple to do so and all without resorting to <a href="http://msdn.microsoft.com/en-us/library/ms141026(SQL.90).aspx">SSIS</a>.  Getting data into SQL Server, from outside sources, is well documented but pushing it out into other DBMSs, like MySQL, is not. This example will demonstrate how you can use SQL Server to get data from an XML file into MYSQL with just a <a href="http://msdn.microsoft.com/en-us/library/ms188279.aspx">Linked Server </a>and a bit of TSQL.</p>
<p><strong>Setup</strong></p>
<p>For this exercise you will need the following pieces of software installed on the same computer. Ensure all these pieces of software are installed and configured correctly before proceeding further.</p>
<p><i>SQL Server 2005</i> &#8211; <a href="http://www.microsoft.com/Sqlserver/2005/en/us/express.aspx">http://www.microsoft.com/Sqlserver/2005/en/us/express.aspx</a><br />
<i>MySQL</i> – <a href="http://www.mysql.com">http://www.mysql.com</a> (5.0.51b-community-nt)<br />
<i>MySQL ODBC 3.51 Driver</i> &#8211; <a href="http://dev.mysql.com/downloads/connector/odbc/3.51.html">http://dev.mysql.com/downloads/connector/odbc/3.51.html</a></p>
<p><strong>MySQL Database Setup</strong></p>
<p>Run this SQL script against your MySQL database to create the required objects. You will also need to <a href="http://dev.mysql.com/doc/refman/5.1/en/create-user.html">create a Mysql user</a> for this database so it can be used in a <a href="http://en.wikipedia.org/wiki/Database_Source_Name">DSN</a>. The database created will contain two tables called <em>customers</em> and <em>customer_contacts</em>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code11'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p411"><td class="code" id="p4code11"><pre class="mysql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Create a MySQL database</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">DATABASE</span> MyCRM<span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;">-- Select MySQL for use</span>
<span style="color: #990099; font-weight: bold;">USE</span> MyCRM<span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;">-- Create a table for customers</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> customers
<span style="color: #FF00FF;">&#40;</span>
	id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	business_name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	address1 <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	address2 <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	city <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	postcode <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">8</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	telephone <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	website <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	email <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span> <span style="color: #CC0099;">=</span> <span style="color: #990099; font-weight: bold;">InnoDB</span><span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;">-- Create a table for customer contacts</span>
<span style="color: #990099; font-weight: bold;">CREATE</span> <span style="color: #990099; font-weight: bold;">TABLE</span> customer_contacts
<span style="color: #FF00FF;">&#40;</span>
	id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #990099; font-weight: bold;">PRIMARY KEY</span><span style="color: #000033;">,</span>
	customer_id <span style="color: #999900; font-weight: bold;">INTEGER</span> <span style="color: #FF9900; font-weight: bold;">UNSIGNED</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	first_name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	last_name <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">50</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	telephone <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	mobile <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">30</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	email <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">100</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><span style="color: #000033;">,</span>
	<span style="color: #008000;">`comment`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">1000</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">ENGINE</span> <span style="color: #CC0099;">=</span> <span style="color: #990099; font-weight: bold;">InnoDB</span><span style="color: #000033;">;</span>
<span style="color: #808080; font-style: italic;">-- Add a foreign key</span>
<span style="color: #990099; font-weight: bold;">ALTER</span> <span style="color: #990099; font-weight: bold;">TABLE</span> customer_contacts <span style="color: #990099; font-weight: bold;">ADD</span> <span style="color: #990099; font-weight: bold;">CONSTRAINT</span> fk_customer_id
<span style="color: #990099; font-weight: bold;">FOREIGN KEY</span> <span style="color: #FF00FF;">&#40;</span>customer_id<span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">REFERENCES</span> customers <span style="color: #FF00FF;">&#40;</span>id<span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">;</span></pre></td></tr></table></div>

<p><strong>Linked Server Setup</strong></p>
<p><a href="http://msdn.microsoft.com/en-us/library/aa213778(SQL.80).aspx">Linked Servers</a> allow commands to be executed against other data sources from within SQL Server.  First configure a data source that the linked server will use.</p>
<p><strong>Add a System DSN</strong></p>
<p>Setup may vary slightly by OS. These instructions refer to Vista.</p>
<p>Start &gt; Control Panel &gt; Administrative Tools &gt; Data Sources (ODBC) &gt; System DSN.<br />
Add a new DSN using the MySQL ODBC 3.51 Driver.</p>
<div id="attachment_5" class="wp-caption alignnone" style="width: 600px"><img class="size-full wp-image-5" title="system_dsn" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/02/system_dsn.jpg" alt="system dsn Insert data into MySQL with T SQL" width="590" height="476" /><p class="wp-caption-text">System DSN Configuration in Vista</p></div>
<p>Configure your System DSN like above and click &#8220;Test&#8221; to ensure it functions. Next we need to login to your SQL Server 2005 instance to add a Linked Server which will be using the above MySQL database.</p>
<p>Run the following TSQL to create the linked server…</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code12'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p412"><td class="code" id="p4code12"><pre class="t-sql" style="font-family:monospace;">EXEC master.dbo.sp_addlinkedserver @server = N'MYSQL', @srvproduct=N'MySQL', @provider=N'MSDASQL', @datasrc=N'MYSQL';
&nbsp;
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'MYSQL',@useself=N'False',@locallogin=NULL,@rmtuser=NULL,@rmtpassword=NULL;</pre></td></tr></table></div>

<p>To ensure the Linked Server is running correctly run the following TSQL</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code13'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p413"><td class="code" id="p4code13"><pre class="t-sql" style="font-family:monospace;">SELECT *
FROM OPENQUERY(MYSQL, 'SELECT * FROM customers');</pre></td></tr></table></div>

<p>An empty resultset should be displayed giving you the customer table columns.</p>
<div id="attachment_6" class="wp-caption alignnone" style="width: 543px"><img src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/02/linked_server_test.jpg" alt="linked server test Insert data into MySQL with T SQL" title="linked_server_test" width="533" height="117" class="size-full wp-image-6" /><p class="wp-caption-text">TSQL to test the linked server</p></div>
<p>The configuration of the Linked Server is now complete and we are ready to begin inserting data into MySQL from our SQL Server instance. The TSQL script will be explained section by section before being represented in whole.</p>
<p>First we declare a few variables that we will be using in our script. <strong>@xml</strong> will contain our xml data, <strong>@handle</strong> will be used to reference the XML document created by SQL Server, and <strong>@customer_id</strong> is a value we will retrieve from MySQL after inserting a record. The <strong>@xml</strong> variable is populated with some XML containing a single customer record with two customer_contact records.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code14'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p414"><td class="code" id="p4code14"><pre class="t-sql" style="font-family:monospace;">DECLARE @xml VARCHAR(MAX),
		@handle INT,
		@customer_id INT;
&nbsp;
-- The XML document containg a single customer
-- with two contacts
SET @xml = '&lt;customer&gt;
				&lt;business_name&gt;ACME Ltd&lt;/business_name&gt;
				&lt;address1&gt;100 Saffron Hill&lt;/address1&gt;
				&lt;address2&gt;Farringdon Road&lt;/address2&gt;
				&lt;city&gt;London&lt;/city&gt;
				&lt;postcode&gt;EC1N 8FH&lt;/postcode&gt;
				&lt;telephone&gt;0123456789&lt;/telephone&gt;
				&lt;website&gt;http://www.acmeltd.com&lt;/website&gt;
				&lt;email&gt;info@acmeltd.com&lt;/email&gt;
				&lt;customer_contact&gt;
					&lt;first_name&gt;Rhys&lt;/first_name&gt;
					&lt;last_name&gt;Campbell&lt;/last_name&gt;
					&lt;telephone&gt;0123456789&lt;/telephone&gt;
					&lt;mobile&gt;0123456789&lt;/mobile&gt;
					&lt;email&gt;rhys@acmltd.com&lt;/email&gt;
					&lt;comment&gt;Owner&lt;/comment&gt;
				&lt;/customer_contact&gt;
				&lt;customer_contact&gt;
					&lt;first_name&gt;Joe&lt;/first_name&gt;
					&lt;last_name&gt;Bloggs&lt;/last_name&gt;
					&lt;telephone&gt;0123456789&lt;/telephone&gt;
					&lt;mobile&gt;0123456789&lt;/mobile&gt;
					&lt;email&gt;joe@acmeltd.com&lt;/email&gt;
					&lt;comment&gt;Employee&lt;/comment&gt;
				&lt;/customer_contact&gt;
			&lt;/customer&gt;';</pre></td></tr></table></div>

<p>Next <a href="http://msdn.microsoft.com/en-us/library/aa260385(SQL.80).aspx">prepare</a> an internal representation of the XML document so it can be used by SQL Server.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code15'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p415"><td class="code" id="p4code15"><pre class="t-sql" style="font-family:monospace;">-- Prepare the xml document
EXEC sp_xml_preparedocument @handle OUTPUT, @xml;</pre></td></tr></table></div>

<p>SQL Server can now select data from the XML document and consume the data in other operations. First we will insert the customer record into the MySQL customers table. This part of the script introduces the <a href="http://msdn.microsoft.com/en-us/library/aa276847(SQL.80).aspx">OPENXML</a> rowset provider. The <a href="http://msdn.microsoft.com/en-us/library/ms188427.aspx">OPENQUERY</a> function needs to read the metadata for the table it is inserting into. The WHERE clause, that will never be true, is included so an empty resultset is returned and performance is maintained as the table grows. (Yes, otherwise you will return data from MySQL to SQL Server that will not be used).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code16'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p416"><td class="code" id="p4code16"><pre class="t-sql" style="font-family:monospace;">-- Insert the customer record
INSERT INTO OPENQUERY(MYSQL, 'SELECT business_name,
									 address1,
									 address2,
									 city,
									 postcode,
									 telephone,
									 website,
									 email
							  FROM customers WHERE 1 != 1')
SELECT business_name,
	   address1,
	   address2,
	   city,
	   postcode,
	   telephone,
	   website,
	   email
FROM OPENXML(@handle, '/customer', 2)
WITH
(
	business_name VARCHAR(100),
	address1 VARCHAR(100),
	address2 VARCHAR(100),
	city VARCHAR(50),
	postcode VARCHAR(8),
	telephone VARCHAR(30),
	website VARCHAR(100),
	email VARCHAR(100)
);</pre></td></tr></table></div>

<p>We need to add two records to the customer_contacts table so we need the customer id value we have just created. The <a href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id">LAST_INSERT_ID()</a> MySQL function used below is similar to the <a href="http://msdn.microsoft.com/en-us/library/ms187342.aspx">@@IDENTITY</a> function.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code17'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p417"><td class="code" id="p4code17"><pre class="t-sql" style="font-family:monospace;">-- Get the id for the created customer
SET @customer_id = (SELECT * 
					FROM OPENQUERY(MYSQL, 'SELECT LAST_INSERT_ID()'));</pre></td></tr></table></div>

<p>Once the @customer_id variable has been set we are able to insert the two customer_contacts records.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code18'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p418"><td class="code" id="p4code18"><pre class="t-sql" style="font-family:monospace;">-- Insert the contact records
INSERT INTO OPENQUERY(MYSQL, 'SELECT customer_id,
									 first_name,
									 last_name,
									 telephone,
									 mobile,
									 email,
									 comment
							  FROM customer_contacts WHERE 1 != 1')
SELECT @customer_id,
	   first_name,
	   last_name,
	   telephone,
	   mobile,
	   email,
	   comment
FROM OPENXML(@handle, '/customer/customer_contact', 2)
WITH
(
	first_name VARCHAR(50),
	last_name VARCHAR(50),
	telephone VARCHAR(30),
	mobile VARCHAR(30),
	email VARCHAR(100),
	comment TEXT
);</pre></td></tr></table></div>

<p>Finally we need to free up the resources consumed by the XML document by <a href="http://msdn.microsoft.com/en-us/library/aa260386(SQL.80).aspx">removing</a> it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code19'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p419"><td class="code" id="p4code19"><pre class="t-sql" style="font-family:monospace;">-- Remove the xml document
EXEC sp_xml_removedocument @handle;</pre></td></tr></table></div>

<p>That&#8217;s it! Now there should be one record in the customer table and two records in the customer_contacts table. SQL Server Management Studio should display something like&#8230;</p>
<div id="attachment_7" class="wp-caption alignnone" style="width: 543px"><img src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/02/after_insert.jpg" alt="after insert Insert data into MySQL with T SQL" title="after_insert" width="533" height="109" class="size-full wp-image-7" /><p class="wp-caption-text">SSMS After TSQL Script</p></div>
<p>The script is repeated here in full so you can copy and paste this into SSMS.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4code20'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p420"><td class="code" id="p4code20"><pre class="t-sql" style="font-family:monospace;">DECLARE @xml VARCHAR(MAX),
		@handle INT,
		@customer_id INT;
&nbsp;
-- The XML document containg a single customer
-- with two contacts
SET @xml = '&lt;customer&gt;
				&lt;business_name&gt;ACME Ltd&lt;/business_name&gt;
				&lt;address1&gt;100 Saffron Hill&lt;/address1&gt;
				&lt;address2&gt;Farringdon Road&lt;/address2&gt;
				&lt;city&gt;London&lt;/city&gt;
				&lt;postcode&gt;EC1N 8FH&lt;/postcode&gt;
				&lt;telephone&gt;0123456789&lt;/telephone&gt;
				&lt;website&gt;http://www.acmeltd.com&lt;/website&gt;
				&lt;email&gt;info@acmeltd.com&lt;/email&gt;
				&lt;customer_contact&gt;
					&lt;first_name&gt;Rhys&lt;/first_name&gt;
					&lt;last_name&gt;Campbell&lt;/last_name&gt;
					&lt;telephone&gt;0123456789&lt;/telephone&gt;
					&lt;mobile&gt;0123456789&lt;/mobile&gt;
					&lt;email&gt;rhys@acmltd.com&lt;/email&gt;
					&lt;comment&gt;Owner&lt;/comment&gt;
				&lt;/customer_contact&gt;
				&lt;customer_contact&gt;
					&lt;first_name&gt;Joe&lt;/first_name&gt;
					&lt;last_name&gt;Bloggs&lt;/last_name&gt;
					&lt;telephone&gt;0123456789&lt;/telephone&gt;
					&lt;mobile&gt;0123456789&lt;/mobile&gt;
					&lt;email&gt;joe@acmeltd.com&lt;/email&gt;
					&lt;comment&gt;Employee&lt;/comment&gt;
				&lt;/customer_contact&gt;
			&lt;/customer&gt;';
&nbsp;
-- Prepare the xml document
EXEC sp_xml_preparedocument @handle OUTPUT, @xml;
&nbsp;
-- Insert the customer record
INSERT INTO OPENQUERY(MYSQL, 'SELECT business_name,
									 address1,
									 address2,
									 city,
									 postcode,
									 telephone,
									 website,
									 email
							  FROM customers WHERE 1 != 1')
SELECT business_name,
	   address1,
	   address2,
	   city,
	   postcode,
	   telephone,
	   website,
	   email
FROM OPENXML(@handle, '/customer', 2)
WITH
(
	business_name VARCHAR(100),
	address1 VARCHAR(100),
	address2 VARCHAR(100),
	city VARCHAR(50),
	postcode VARCHAR(8),
	telephone VARCHAR(30),
	website VARCHAR(100),
	email VARCHAR(100)
);	
&nbsp;
-- Get the id for the created customer
SET @customer_id = (SELECT * 
					FROM OPENQUERY(MYSQL, 'SELECT LAST_INSERT_ID()'));
&nbsp;
-- Insert the contact records
INSERT INTO OPENQUERY(MYSQL, 'SELECT customer_id,
									 first_name,
									 last_name,
									 telephone,
									 mobile,
									 email,
									 comment
							  FROM customer_contacts WHERE 1 != 1')
SELECT @customer_id,
	   first_name,
	   last_name,
	   telephone,
	   mobile,
	   email,
	   comment
FROM OPENXML(@handle, '/customer/customer_contact', 2)
WITH
(
	first_name VARCHAR(50),
	last_name VARCHAR(50),
	telephone VARCHAR(30),
	mobile VARCHAR(30),
	email VARCHAR(100),
	comment TEXT
);
&nbsp;
-- Remove the xml document
EXEC sp_xml_removedocument @handle;</pre></td></tr></table></div>

<p>This solution will be the perfect fit for SQL Server based systems where it is not appropriate to add another layer of complexity like SSIS. The ease at which SQL Server deals with multiple data sources places it in an ideal position to become a data broker between systems and techniques like above make it a snap!</p>
<p><map name='google_ad_map_4_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/4?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_4_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=4&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Finsert-data-mysql-tsql%2F4' title="Insert data into MySQL with T SQL" alt=" Insert data into MySQL with T SQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/insert-data-mysql-tsql/4">Insert data into MySQL with T-SQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/insert-data-mysql-tsql/4" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/insert-data-mysql-tsql/4/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

