<?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; sql server 2005</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/sql-server-2005/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>Testing datetime dependent Stored Procedures</title>
		<link>http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433</link>
		<comments>http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433#comments</comments>
		<pubDate>Sat, 14 Nov 2009 14:24:28 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[sql server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433</guid>
		<description><![CDATA[This week I was tasked with testing a stored procedure that was meant to output data on certain days. This was over a 120 day period, so I wanted to find some automated way of doing this, rather than changing the server date manually for each execution. The method I came up with involves the [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433">Testing datetime dependent Stored Procedures</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This week I was tasked with testing a stored procedure that was meant to output data on certain days. This was over a 120 day period, so I wanted to find some automated way of doing this, rather than changing the server date manually for each execution. The method I came up with involves the use of <a href="http://msdn.microsoft.com/en-us/library/ms175046.aspx" target="_blank">xp_cmdshell</a> to execute the <a href="http://en.wikipedia.org/wiki/List_of_DOS_commands#time_and_date" target="_blank">date command</a>. Here&#8217;s an illustration of what I came up with.</p>
<p>First create this stored procedure. This will just check to see if it&#8217;s Saturday and output &quot;Yes&quot; if it is.</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('p433code3'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4333"><td class="code" id="p433code3"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
<span style="color: #008080;">-- =============================================</span>
<span style="color: #008080;">-- Author: Rhys Campbell</span>
<span style="color: #008080;">-- Create date: 2009-11-14</span>
<span style="color: #008080;">-- Description:	Outputs &quot;Yes&quot; if it is Saturday</span>
<span style="color: #008080;">-- otherwise &quot;No&quot;</span>
<span style="color: #008080;">-- =============================================</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> usp_isItSaturday
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
	<span style="color: #0000FF;">DECLARE</span> @isItSaturday <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span>;
&nbsp;
    	<span style="color: #0000FF;">SET</span> @isItSaturday <span style="color: #808080;">=</span> <span style="color: #0000FF;">CASE</span>
				<span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>dw, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #000;">7</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'Yes'</span>
				<span style="color: #0000FF;">ELSE</span>
					<span style="color: #FF0000;">'No'</span>
			    <span style="color: #0000FF;">END</span>;
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, @isItSaturday;
&nbsp;
<span style="color: #0000FF;">END</span>
GO</pre></td></tr></table></div>

<p>The script below will change the date by one day, then execute usp_isItSaturday, in sequence up to the end of 2009. I had to introduce the <a href="http://msdn.microsoft.com/en-us/library/ms187331.aspx" target="_blank">WAITFOR</a> delay because <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx" target="_blank">SQL Server</a> seems to take a few seconds to register the date change. The script will simply loop until the day changes before executing the store procedure.</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('p433code4'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4334"><td class="code" id="p433code4"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Enable xp_cmdshell</span>
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_configure</span> <span style="color: #FF0000;">'xp_cmdshell'</span>, <span style="color: #000;">1</span>;
<span style="color: #0000FF;">RECONFIGURE</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @command <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span>,
		@<span style="color: #0000FF;">date</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>,
		@starting <span style="color: #0000FF;">DATETIME</span>,
		@<span style="color: #0000FF;">day</span> <span style="color: #0000FF;">INTEGER</span>;
&nbsp;
<span style="color: #0000FF;">SET</span> @starting <span style="color: #808080;">=</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">&#40;</span>@starting <span style="color: #808080;">&lt;=</span> <span style="color: #FF0000;">'2009-12-31T00:00:00'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">date</span> <span style="color: #808080;">=</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>d, @starting<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>m, @starting<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>YYYY, @starting<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;
	<span style="color: #008080;">-- Get the day for tracking when the date change has taken effect</span>
	<span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">day</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>d, @starting<span style="color: #808080;">&#41;</span>;
	<span style="color: #0000FF;">SET</span> @command <span style="color: #808080;">=</span> <span style="color: #FF0000;">'date '</span> <span style="color: #808080;">+</span> @<span style="color: #0000FF;">date</span>;
	<span style="color: #0000FF;">EXEC</span> xp_cmdshell @command, no_output;
&nbsp;
	<span style="color: #008080;">-- SQL Server takes a while to pickup the date change</span>
	<span style="color: #008080;">-- Loop around until the day has changed before executing the proc</span>
	<span style="color: #0000FF;">WHILE</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">day</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>d, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">WAITFOR</span> DELAY <span style="color: #FF0000;">'00:00:01'</span>;
	<span style="color: #0000FF;">END</span>
&nbsp;
	<span style="color: #008080;">-- Call the procedure with the changed date</span>
	<span style="color: #0000FF;">EXEC</span> dbo.<span style="color: #202020;">usp_isItSaturday</span>;  
	<span style="color: #008080;">-- Increment date by 1 day</span>
	<span style="color: #0000FF;">SET</span> @starting <span style="color: #808080;">=</span> <span style="color: #FF00FF;">DATEADD</span><span style="color: #808080;">&#40;</span>d, <span style="color: #000;">1</span>, @starting<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">END</span> 
&nbsp;
<span style="color: #008080;">-- Disable xp_cmdshell</span>
<span style="color: #0000FF;">EXEC</span> master.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_configure</span> <span style="color: #FF0000;">'xp_cmdshell'</span>, <span style="color: #000;">0</span>;
<span style="color: #0000FF;">RECONFIGURE</span>;</pre></td></tr></table></div>

<p>Here&#8217;s a sample of the output. You can see it has correctly identified the days that are Saturday.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/usp_isItSaturday.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="usp_isItSaturday" border="0" alt="usp isItSaturday thumb Testing datetime dependent Stored Procedures" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/11/usp_isItSaturday_thumb.png" width="121" height="244" /></a></p>
<p><map name='google_ad_map_433_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/433?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_433_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=433&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftesting-datetime-dependent-stored-procedures%2F433' title="Testing datetime dependent Stored Procedures" alt=" Testing datetime dependent Stored Procedures" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433">Testing datetime dependent Stored Procedures</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/testing-datetime-dependent-stored-procedures/433/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ping-SQL Sneak Peek</title>
		<link>http://www.youdidwhatwithtsql.com/ping-sql-sneak-peek/38</link>
		<comments>http://www.youdidwhatwithtsql.com/ping-sql-sneak-peek/38#comments</comments>
		<pubDate>Fri, 13 Mar 2009 19:16:20 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Ping-SQL]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[ping.fm]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[sql server 2005]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ping-sql-sneak-peek/38</guid>
		<description><![CDATA[Ping-SQL allows you to play with the Ping.fm&#160;API with standard T-SQL in Microsoft SQL Server 2005 and above. Ping-SQL includes procedures to programmatically adjust configuration, replicate data to local tables and, of course, interact directly with the ping.fm API. Here’s a quick summary of the procedures currently in the suite… Procedure Classification Comment ping_cfg_debug Configuration [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ping-sql-sneak-peek/38">Ping-SQL Sneak Peek</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://ping-sql.com/" target="_blank">Ping-SQL</a> allows you to play with the <a href="http://ping.fm" target="_blank">Ping.fm</a>&#160;<a href="http://groups.google.com/group/pingfm-developers/web/api-documentation" target="_blank">API</a> with standard T-SQL in Microsoft SQL Server 2005 and above. <a href="http://www.ping-sql.com" target="_blank">Ping-SQL</a> includes procedures to programmatically adjust configuration, replicate data to local tables and, of course, interact directly with the ping.fm API. Here’s a quick summary of the procedures currently in the suite…</p>
<table cellspacing="0" cellpadding="0" border="1">
<tbody>
<tr>
<td valign="top" width="200">
<p><b>Procedure</b></p>
</td>
<td valign="top" width="119">
<p><b>Classification</b></p>
</td>
<td valign="top" width="297">
<p><b>Comment</b></p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_cfg_debug</p>
</td>
<td valign="top" width="119">
<p>Configuration</p>
</td>
<td valign="top" width="297">
<p>Allows you to avoid posting test data to the ping.fm API when set to On / 1.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_cfg_license_key</p>
</td>
<td valign="top" width="119">
<p>Configuration</p>
</td>
<td valign="top" width="297">
<p>Change the Ping-SQL License key.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_cfg_no_rsp</p>
</td>
<td valign="top" width="119">
<p>Configuration</p>
</td>
<td valign="top" width="297">
<p>Removes any data table called “rsp” from resultsets.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_cfg_no_services</p>
</td>
<td valign="top" width="119">
<p>Configuration</p>
</td>
<td valign="top" width="297">
<p>Removes any data tables called “services” from resultsets.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_cfg_resultset_send</p>
</td>
<td valign="top" width="119">
<p>Configuration</p>
</td>
<td valign="top" width="297">
<p>Changes the way resultsets are presented.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_cfg_sys_debug</p>
</td>
<td valign="top" width="119">
<p>Configuration</p>
</td>
<td valign="top" width="297">
<p>Enables Ping-SQL application debugging.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_cfg_user_key</p>
</td>
<td valign="top" width="119">
<p>Configuration</p>
</td>
<td valign="top" width="297">
<p>Allows the user key used to authenticate with ping.fm to be changed.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_sys_systemServicesTable</p>
</td>
<td valign="top" width="119">
<p>Data Replication</p>
</td>
<td valign="top" width="297">
<p>Creates a local table containing the data returned from the system.services API method. This table is called ping_systemServicesTable. Any existing table with this name is dropped first.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_sys_userServicesTable</p>
</td>
<td valign="top" width="119">
<p>Data Replication</p>
</td>
<td valign="top" width="297">
<p>Creates a local table containing the data returned from the user.services API method. The table is called ping_userServicesTable. Any existing table with this name is dropped first.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_systemServices</p>
</td>
<td valign="top" width="119">
<p>Ping.fm API</p>
</td>
<td valign="top" width="297">
<p>Retuns a complete list of services supported by ping.fm.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_userKey</p>
</td>
<td valign="top" width="119">
<p>Ping.fm API</p>
</td>
<td valign="top" width="297">&#160;</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_userLatest</p>
</td>
<td valign="top" width="119">
<p>Ping.fm API</p>
</td>
<td valign="top" width="297">
<p>Returns the last 25 messages a user has posted through Ping.fm.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_userPost</p>
</td>
<td valign="top" width="119">
<p>Ping.fm API</p>
</td>
<td valign="top" width="297">
<p>Post a message to the users ping.fm services.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_userServices</p>
</td>
<td valign="top" width="119">
<p>Ping.fm API</p>
</td>
<td valign="top" width="297">
<p>Returns a list of services the user has configured in their ping.fm account.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_userTriggers</p>
</td>
<td valign="top" width="119">
<p>Ping.fm API</p>
</td>
<td valign="top" width="297">
<p>Returns a list of custom triggers.</p>
</td>
</tr>
<tr>
<td valign="top" width="200">
<p>ping_userValidate</p>
</td>
<td valign="top" width="119">
<p>Ping.fm API</p>
</td>
<td valign="top" width="297">
<p>Validates the configured user key.</p>
</td>
</tr>
</tbody>
</table>
<p>So what can you do with <a href="http://www.ping-sql.com" target="_blank">Ping-SQL</a>?</p>
<p><strong>Update your Facebook status with Ping-SQL</strong></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('p38code9'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p389"><td class="code" id="p38code9"><pre class="t-sql" style="font-family:monospace;">-- Update your facebook status with Ping-SQL
DECLARE @status VARCHAR(140),
		@optional VARCHAR(100);
&nbsp;
-- Status message to post
SET @status = 'is posting my status to facebook';
-- Specify the service to post to here
SET @optional = 'service=facebook'
&nbsp;
EXEC dbo.ping_userPost null, @status, @optional, null, null;</pre></td></tr></table></div>

<p><strong>Post to Twitter with Ping-SQL</strong></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('p38code10'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3810"><td class="code" id="p38code10"><pre class="t-sql" style="font-family:monospace;">-- Twitter microblogging with Ping-SQL
DECLARE @microblog VARCHAR(140),
		@optional VARCHAR(100);
&nbsp;
-- Status message to post
SET @microblog = 'Drinking Coffee at home';
-- Specify the service to post to here
SET @optional = 'service=twitter'
&nbsp;
EXEC dbo.ping_userPost null, @microblog, @optional, null, null;</pre></td></tr></table></div>

<p><strong>Post to Blogger with Ping-SQL</strong></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('p38code11'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3811"><td class="code" id="p38code11"><pre class="t-sql" style="font-family:monospace;">-- Post to Blogger with Ping-SQL
DECLARE @title VARCHAR(100),
		@blog VARCHAR(MAX),
		@optional VARCHAR(100);
&nbsp;
-- Set the title of your blog
SET @title= 'Test Blog Title';
-- The blog text
SET @blog = 'Lorem ipsum dolor sit amet, consectetur 
adipisicing elit, sed do eiusmod tempor incididunt ut 
labore et dolore magna aliqua. Ut enim ad minim veniam, 
quis nostrud exercitation ullamco laboris nisi ut aliquip 
ex ea commodo consequat. Duis aute irure dolor in 
reprehenderit in voluptate velit esse cillum dolore eu 
fugiat nulla pariatur. Excepteur sint occaecat cupidatat 
non proident, sunt in culpa qui officia deserunt mollit 
anim id est laborum.';
&nbsp;
-- Specify the service to post to here
-- and set the title tag
SET @optional = 'service=blogger&amp;amp;title=' + @title;
&nbsp;
-- Important @post_method must be set to blog!
EXEC dbo.ping_userPost 'blog', @blog, @optional, null, null;</pre></td></tr></table></div>

<p><strong>Upload Photos to Flickr with Ping-SQL</strong></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('p38code12'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3812"><td class="code" id="p38code12"><pre class="t-sql" style="font-family:monospace;">-- Upload a photo to Flickr!
DECLARE @title VARCHAR(140),
		@optional VARCHAR(100),
		@media_path VARCHAR(100);
&nbsp;
-- Title of the Photo
SET @title = 'Pink Floyd Album Covers';
-- Specify the service to post to here
SET @optional = 'service=flickr';
-- Set the image path 
SET @media_path = 'C:\Users\Rhys\Pictures\pink_floyd_001.jpg';
&nbsp;
EXEC dbo.ping_userPost null, @title, @optional, @media_path, null;</pre></td></tr></table></div>

<p><a href="http://www.ping-sql.com" target="_blank">Ping-SQL</a> is also capable of calling <a href="http://ping.fm/triggers/" target="_blank">triggers</a> that you have setup on your <a href="http://ping.fm" target="_blank">ping.fm</a> account. In fact <a href="http://www.ping-sql.com" target="_blank">Ping-SQL</a> should be capable of calling any of the services supported by ping.fm. Write T-SQL to play with Bebo, Blogger, BrightKite, Facebook, FriendFeed, MySpace, Plurk, Twitter, WordPress.com, Yahoo 360, Yammer and many more.</p>
<p><map name='google_ad_map_38_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/38?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_38_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=38&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fping-sql-sneak-peek%2F38' title="Ping SQL Sneak Peek" alt=" Ping SQL Sneak Peek" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ping-sql-sneak-peek/38">Ping-SQL Sneak Peek</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/ping-sql-sneak-peek/38" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ping-sql-sneak-peek/38/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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('p4code23'); return false;">View Code</a> MYSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p423"><td class="code" id="p4code23"><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('p4code24'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p424"><td class="code" id="p4code24"><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('p4code25'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p425"><td class="code" id="p4code25"><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('p4code26'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p426"><td class="code" id="p4code26"><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('p4code27'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p427"><td class="code" id="p4code27"><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('p4code28'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p428"><td class="code" id="p4code28"><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('p4code29'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p429"><td class="code" id="p4code29"><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('p4code30'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p430"><td class="code" id="p4code30"><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('p4code31'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p431"><td class="code" id="p4code31"><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('p4code32'); return false;">View Code</a> T-SQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p432"><td class="code" id="p4code32"><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>

