<?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>kSQL.net</title>
	<atom:link href="http://ksql.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://ksql.net</link>
	<description>Philosophy Major turned DBA</description>
	<lastBuildDate>Fri, 10 Feb 2012 18:50:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>T-SQL: Capitalize First Character In String</title>
		<link>http://ksql.net/2012/02/10/t-sql-capitalize-first-character-in-string/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=t-sql-capitalize-first-character-in-string</link>
		<comments>http://ksql.net/2012/02/10/t-sql-capitalize-first-character-in-string/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 18:50:53 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Capitalize]]></category>
		<category><![CDATA[Capitalize First Letter]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=185</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/development/tsql/" title="View all posts in T-SQL" rel="category tag">T-SQL</a></p><p>Tags: <a href="http://ksql.net/tag/capitalize/" rel="tag">Capitalize</a>, <a href="http://ksql.net/tag/capitalize-first-letter/" rel="tag">Capitalize First Letter</a>, <a href="http://ksql.net/tag/tsql/" rel="tag">T-SQL</a></p>First, thanks to Hipster Ipsum for providing really fun random string data! I created a proc this week that was a simple SELECT ALL from a lookup and the output was going to be used in a drop down menu in a Reporting Services report. The data wasn&#8217;t the prettiest and I wanted to capitalize [...]<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2012/02/10/t-sql-capitalize-first-character-in-string/' title='T-SQL: Capitalize First Character In String'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>First, thanks to <strong><a title="Hipster Ipsum" href="http://hipsteripsum.me/" target="_blank">Hipster Ipsum</a></strong> for providing really fun random string data!</p>
<p>I created a proc this week that was a simple SELECT ALL from a lookup and the output was going to be used in a drop down menu in a Reporting Services report. The data wasn&#8217;t the prettiest and I wanted to capitalize the first letter of every item within my SELECT <em>without</em> using a function. Here is how I did it:</p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span> #StringData(StringData <span style="color: #0000ff;">VARCHAR</span>(100));</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> #StringData(StringData)<br />
<span style="color: #0000ff;">VALUES</span> (<span style="color: #ff0000;">'High life truffaut artisan salvia'</span>),<br />
(<span style="color: #ff0000;">'raw denim tofu terry richardson skateboard'</span>),<br />
(<span style="color: #ff0000;">'iphone PickLED'</span>),<br />
(<span style="color: #ff0000;">'BOOTH SQUID'</span>),<br />
(<span style="color: #ff0000;">' '</span>)</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">UPPER</span>(<span style="color: #999999;">LEFT</span>(StringData,1)) + <span style="color: #999999;">RIGHT</span>(StringData,(<span style="color: #ff00ff;">LEN</span>(StringData)-1)) AS <span style="color: #ff0000;">'With Capital'</span>,<br />
StringData AS <span style="color: #ff0000;">'Original'</span><br />
<span style="color: #0000ff;">FROM</span> #StringData<br />
<span style="color: #0000ff;">WHERE</span> <span style="color: #ff00ff;">LEN</span>(StringData) &gt; 0</code></p>
<p style="padding-left: 30px;"><span style="color: #0000ff;"><code>UNION</code></span></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">SELECT</span> '' AS <span style="color: #ff0000;">'With Capital'</span>,<br />
StringData AS <span style="color: #ff0000;">'Original'</span><br />
<span style="color: #0000ff;">FROM</span> #StringData<br />
<span style="color: #0000ff;">WHERE</span> <span style="color: #ff00ff;">LEN</span>(StringData) = 0</code></p>
<p>The output is below: it works in every case except a string with a length of 0.</p>
<p><img class="alignleft size-full wp-image-186" title="Capital" src="http://ksql.net/wp-content/uploads/2012/02/Capital.jpg" alt="" width="478" height="140" /></p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2012/02/10/t-sql-capitalize-first-character-in-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL &#8211; Selecting the Start of Previous Hour</title>
		<link>http://ksql.net/2012/01/12/t-sql-selecting-the-start-of-previous-hour/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=t-sql-selecting-the-start-of-previous-hour</link>
		<comments>http://ksql.net/2012/01/12/t-sql-selecting-the-start-of-previous-hour/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 04:40:57 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[date variable]]></category>
		<category><![CDATA[DATEPART]]></category>
		<category><![CDATA[Previous Hour]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=180</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/development/" title="View all posts in Development" rel="category tag">Development</a>, <a href="http://ksql.net/category/development/tsql/" title="View all posts in T-SQL" rel="category tag">T-SQL</a></p><p>Tags: <a href="http://ksql.net/tag/date-variable/" rel="tag">date variable</a>, <a href="http://ksql.net/tag/datepart/" rel="tag">DATEPART</a>, <a href="http://ksql.net/tag/previous-hour/" rel="tag">Previous Hour</a></p>One area I&#8217;ve always felt MSSQL was lacking is handling dates. This week I needed to declare a date variable and select the beginning of the previous hour. I cobbled together a solution that I thought would be useful to add to the blogosphere. Below is a screenshot and the code you can copy and [...]<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2012/01/12/t-sql-selecting-the-start-of-previous-hour/' title='T-SQL - Selecting the Start of Previous Hour'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>One area I&#8217;ve always felt MSSQL was lacking is handling dates. This week I needed to declare a date variable and select the beginning of the previous hour. I cobbled together a solution that I thought would be useful to add to the blogosphere. Below is a screenshot and the code you can copy and paste.<br />
<a href="http://ksql.net/wp-content/uploads/2012/01/Screen-shot-2012-01-12-at-9.36.58-PM.png"><img class="aligncenter size-full wp-image-183" title="Screen shot 2012-01-12 at 9.36.58 PM" src="http://ksql.net/wp-content/uploads/2012/01/Screen-shot-2012-01-12-at-9.36.58-PM.png" alt="" width="555" height="559" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p style="padding-left: 30px;"><code><br />
SELECT CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR) AS 'Year'<br />
SELECT CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR) AS 'Month'<br />
SELECT CAST(DATEPART(DAY, GETDATE()) AS VARCHAR) AS 'Day'<br />
SELECT CAST(DATEPART(HOUR, GETDATE()) AS VARCHAR) AS 'Hour'</code><br />
<code><br />
SELECT CAST(DATEPART(YEAR,GETDATE()) AS VARCHAR) +<br />
'/' +<br />
CAST(DATEPART(MONTH,GETDATE()) AS VARCHAR) +<br />
'/' +<br />
CAST(DATEPART(DAY,GETDATE()) AS VARCHAR) +<br />
SPACE(1) +<br />
CAST(DATEPART(HOUR,GETDATE()) - 1 AS VARCHAR) +<br />
':00:00.000' AS 'Beginning Of Prvious Hour'</code></p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2012/01/12/t-sql-selecting-the-start-of-previous-hour/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Count Characters in a String</title>
		<link>http://ksql.net/2011/12/06/count-characters-in-a-string/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=count-characters-in-a-string</link>
		<comments>http://ksql.net/2011/12/06/count-characters-in-a-string/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 04:26:59 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Character Count]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=176</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/development/tsql/" title="View all posts in T-SQL" rel="category tag">T-SQL</a></p><p>Tags: <a href="http://ksql.net/tag/character-count/" rel="tag">Character Count</a>, <a href="http://ksql.net/tag/tsql/" rel="tag">T-SQL</a></p>Neat trick that Lee Ervin showed me:<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2011/12/06/count-characters-in-a-string/' title='Count Characters in a String'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>Neat trick that Lee Ervin showed me:</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/Screen-shot-2011-12-06-at-9.17.43-PM.png"><img class="aligncenter size-full wp-image-177" title="Screen shot 2011-12-06 at 9.17.43 PM" src="http://ksql.net/wp-content/uploads/2011/12/Screen-shot-2011-12-06-at-9.17.43-PM.png" alt="" width="652" height="261" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2011/12/06/count-characters-in-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Index Interrogation</title>
		<link>http://ksql.net/2011/12/06/index-interrogation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=index-interrogation</link>
		<comments>http://ksql.net/2011/12/06/index-interrogation/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 00:40:33 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[Indexes]]></category>
		<category><![CDATA[Index fragmentation]]></category>
		<category><![CDATA[index interrogation]]></category>
		<category><![CDATA[SQL DMV]]></category>
		<category><![CDATA[unused indexes]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=65</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/administration/indexes/" title="View all posts in Indexes" rel="category tag">Indexes</a></p><p>Tags: <a href="http://ksql.net/tag/index-fragmentation/" rel="tag">Index fragmentation</a>, <a href="http://ksql.net/tag/index-interrogation/" rel="tag">index interrogation</a>, <a href="http://ksql.net/tag/sql-dmv/" rel="tag">SQL DMV</a>, <a href="http://ksql.net/tag/unused-indexes/" rel="tag">unused indexes</a></p>I&#8217;ve stumbled across many index interrogation queries over the years and each had something unique to offer. I thought I would share the one I use. It functions at the database level so you&#8217;d need to run it once per database. I successfully used it to drop unused indexes and defragment several production servers this [...]<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2011/12/06/index-interrogation/' title='Index Interrogation'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve stumbled across many index interrogation queries over the years and each had something unique to offer. I thought I would share the one I use. It functions at the database level so you&#8217;d need to run it once per database. I successfully used it to drop unused indexes and defragment several production servers this past quarter. It gives information on seeks, scans,  lookups, updates, index type, last table stats update, page count, fragmentation, reserved and used KB, which indexes are hard coded into any procs, indexed and included columns, among other goodies. Thanks to <a href="http://sqlfool.com" target="_blank">Michelle </a>for a decent chunk of it!</p>
<p>The script is quite in-depth and should be run with caution on production servers.</p>
<div class="wp_syntax">
<pre class="tsql" style="font-family: monospace;"><span style="color: #0000ff;">DECLARE</span> @objectID <span style="color: #0000ff;">INT</span>;
<span style="color: #0000ff;">SET</span> @objectID = <span style="color: #ff00ff;">OBJECT_ID</span>(<span style="color: #ff00ff;">QUOTENAME</span>(<span style="color: #ff00ff;">DB_NAME</span>(<span style="color: #ff00ff;">DB_ID</span>())));

<span style="color: #0000ff;">DECLARE</span> @Objects <span style="color: #0000ff;">TABLE</span> (HardCodedIndexes <span style="color: #0000ff;">VARCHAR</span>(1000));
<span style="color: #0000ff;">INSERT INTO</span> @Objects
<span style="color: #0000ff;">SELECT</span> name
<span style="color: #0000ff;">FROM</span> [sys].[objects]
<span style="color: #0000ff;">WHERE</span> [object_id] IN (<span style="color: #0000ff;">SELECT</span> [object_id] <span style="color: #0000ff;">FROM</span> [sys].[sql_modules] <span style="color: #0000ff;">WHERE</span> [definition] <span style="color: #808080;">LIKE</span> <span style="color: #ff0000;">'%INDEX = %'</span>);

<span style="color: #0000ff;">IF NOT EXISTS</span> (<span style="color: #0000ff;">SELECT TOP</span> 1 0 <span style="color: #0000ff;">FROM</span> @Objects)
 <span style="color: #0000ff;">BEGIN</span>;
  <span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> @Objects <span style="color: #0000ff;">SELECT </span><span style="color: #ff0000;">'None'</span>;
 <span style="color: #0000ff;">END</span>;

<span style="color: #0000ff;">WITH</span> indexCTE <span style="color: #0000ff;">AS</span>
	(<span style="color: #0000ff;">SELECT </span>sps.name <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'partition_scheme_name'</span>,
		spf.name <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'partition_function_name'</span>,
		sps.data_space_id <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'data_space_id'</span>
    <span style="color: #0000ff;">FROM</span> [sys].[partition_schemes] <span style="color: #0000ff;">AS</span> sps
	<span style="color: #808080;">INNER JOIN</span> [sys].[partition_functions] <span style="color: #0000ff;">AS</span> spf
		<span style="color: #0000ff;">ON</span> sps.function_id = spf.function_id),

DMV_OperationalStats <span style="color: #0000ff;">AS</span>
	(<span style="color: #0000ff;">SELECT </span>index_id,
		<span style="color: #000000;">[object_id],</span>
		range_scan_count,
		singleton_lookup_count,		<span style="color: #339966;">-- number of times the index has been used to fetch a single row</span>
		forwarded_fetch_count,		<span style="color: #339966;">-- for a heap, shows the number of forward pointers have been used to resolve a query</span>
		lob_fetch_in_pages,		<span style="color: #339966;">-- Quantifies the #of large object (varchar(max),varbinary(max),text etc) retrieved using this index</span>
		lob_fetch_in_bytes,
		row_lock_count,			<span style="color: #339966;">-- number of row locks that have been requested against this index</span>
		row_lock_wait_count,		<span style="color: #339966;">-- number of times a process has waited on a row lock against this index</span>
		row_lock_wait_in_ms,		<span style="color: #339966;">-- amount of time spent waiting on a row lock against this index</span>
		page_latch_wait_count,		<span style="color: #339966;">-- number of waits and time waited on the physical page of the object to have the latch removed</span>
		page_io_latch_wait_count
	<span style="color: #0000ff;">FROM</span> [sys].[dm_db_index_operational_stats](<span style="color: #ff00ff;">DB_ID<span style="color: #000000;">()</span></span>,<span style="color: #808080;">NULL</span>, <span style="color: #808080;">NULL</span>, <span style="color: #808080;">NULL</span>)),

DMV_UsageStats <span style="color: #0000ff;">AS</span>
	(<span style="color: #0000ff;">SELECT </span><span style="color: #ff00ff;">QUOTENAME</span>(<span style="color: #ff00ff;">DB_NAME</span>(u.database_id))	<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'database_name'</span>,
		i.name					<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'index_name'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.user_seeks,NULL)		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'user_seeks'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.last_user_seek, NULL)	<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'last_user_seek'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.last_system_seek, NULL)	<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'last_system_seek'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.user_scans, NULL)		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'user_scans'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.last_user_scan, NULL)	<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'last_user_scan'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.last_system_scan, NULL)	<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'last_system_scan'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.user_lookups, NULL)		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'user_lookups'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.last_user_lookup, NULL)	<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'last_user_lookup'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.last_system_lookup, NULL) <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'last_system_lookup'</span>,
		<span style="color: #ff00ff;">COALESCE</span>(u.user_updates, NULL)		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'user_updates'</span>
	<span style="color: #0000ff;">FROM</span> [sys].[indexes] <span style="color: #0000ff;">AS</span> i
		<span style="color: #808080;">INNER JOIN</span> [sys].[objects] <span style="color: #0000ff;">AS</span> o
			<span style="color: #0000ff;">ON</span> i.object_id = o.object_id
		<span style="color: #808080;">LEFT OUTER JOIN</span> [sys].[dm_db_index_usage_stats] <span style="color: #0000ff;">AS</span> u
			<span style="color: #0000ff;">ON</span> i.object_id = u.object_id
				<span style="color: #808080;">AND</span> i.index_id = u.index_id
	<span style="color: #0000ff;">WHERE</span> u.database_id = <span style="color: #ff00ff;">DB_ID<span style="color: #000000;">()</span></span>
		<span style="color: #808080;">AND</span> o.[type] &lt;&gt; <span style="color: #ff0000;">'S'</span>),

Fragmentation <span style="color: #0000ff;">AS</span>
	(<span style="color: #0000ff;">SELECT </span><span style="color: #ff00ff;">QUOTENAME</span>(<span style="color: #ff00ff;">DB_NAME</span>(<span style="color: #ff00ff;">DB_ID</span>()))		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'databaseName'</span>,
			ps.[object_id]			<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'objectID'</span>,
			ps.index_id			<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'indexID'</span>,
			ps.partition_number		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'partitionNumber'</span>,
			<span style="color: #ff00ff;">SUM</span>(ps.avg_fragmentation_in_percent) <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'fragmentation'</span>,
			<span style="color: #ff00ff;">SUM</span>(ps.page_count)		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'page_count'</span>,
			os.range_scan_count,
			<span style="color: #ff00ff;">GETDATE</span>()			<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'scanDate'</span>
	<span style="color: #0000ff;">FROM</span> [sys].[dm_db_index_physical_stats](<span style="color: #ff00ff;">DB_ID</span>(), <span style="color: #808080;">NULL</span>, <span style="color: #808080;">NULL</span> , <span style="color: #808080;">NULL</span>, <span style="color: #ff0000;">'LIMITED'</span>) <span style="color: #0000ff;">AS</span> ps
		<span style="color: #808080;">INNER JOIN</span> [sys].[dm_db_index_operational_stats](<span style="color: #ff00ff;">DB_ID</span>(), <span style="color: #808080;">NULL</span>, <span style="color: #808080;">NULL</span> , <span style="color: #808080;">NULL</span>) <span style="color: #0000ff;">AS</span> os
			<span style="color: #0000ff;">ON</span> ps.database_id = os.database_id
				<span style="color: #808080;">AND</span> ps.[OBJECT_ID] = os.[OBJECT_ID]
				<span style="color: #808080;">AND</span> ps.index_id = os.index_id
				<span style="color: #808080;">AND</span> ps.partition_number = os.partition_number
	<span style="color: #0000ff;">GROUP BY</span> <span style="color: #ff00ff;">QUOTENAME</span>(<span style="color: #ff00ff;">DB_NAME</span>(ps.database_id)), ps.[OBJECT_ID], ps.index_id, ps.partition_number, os.range_scan_count),

SpaceUsed <span style="color: #0000ff;">AS</span>
	(<span style="color: #0000ff;">SELECT </span>i.<span style="color: #000000;">[object_id],</span>
		i.index_id,
		a.total_pages * 8	<span style="color: #0000ff;">AS </span><span style="color: #ff0000;">'Reserved(KB)'</span>,
		a.used_pages * 8	<span style="color: #0000ff;">AS </span><span style="color: #ff0000;">'Used(KB)'</span>,
		o.create_date		<span style="color: #0000ff;">AS </span><span style="color: #ff0000;">'TableCreateDate'</span>,
		o.modify_date		<span style="color: #0000ff;">AS </span><span style="color: #ff0000;">'TableLastModified'</span>
<span style="color: #0000ff;">FROM</span> [sys].[indexes] <span style="color: #0000ff;">AS</span> i
   <span style="color: #808080;"> INNER JOIN</span> [sys].[partitions] <span style="color: #0000ff;">AS</span> p
		<span style="color: #0000ff;">ON</span> i.<span style="color: #000000;">[object_id] = p.[object_id]</span>
			<span style="color: #808080;">AND</span> i.index_id = p.index_id
    <span style="color: #808080;">INNER JOIN</span> [sys].[allocation_units] <span style="color: #0000ff;">AS</span> a
		<span style="color: #0000ff;">ON</span> p.partition_id = a.container_id
   <span style="color: #808080;"> LEFT OUTER JOIN</span> [sys].[objects] <span style="color: #0000ff;">AS</span> o
		<span style="color: #0000ff;">ON</span> o.[object_id] = i.[object_id]
<span style="color: #0000ff;">WHERE</span> o.type = <span style="color: #ff0000;">'U'</span>),

HardCodedIndexes
<span style="color: #0000ff;">AS</span> (<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">COALESCE</span>(HardCodedIndexes + <span style="color: #ff0000;">','</span> + HardCodedIndexes, HardCodedIndexes) AS <span style="color: #ff0000;">'HardCodedIndexes'</span> <span style="color: #0000ff;">FROM</span> @Objects)

<span style="color: #0000ff;">SELECT </span>dus.database_name,
	st.name				<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'Table'</span>,
	<span style="color: #ff00ff;">ISNULL</span>(ix.name, <span style="color: #ff0000;">''</span>)		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'Index'</span>,
	dus.user_seeks			<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'User Seeks'</span>,
	dus.user_scans			<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'User Scans'</span>,
	dus.user_lookups		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'User Lookups'</span>,
	dus.user_updates		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'User Updates'</span>,
	<span style="color: #0000ff;">CAST</span>(<span style="color: #0000ff;">CASE</span> <span style="color: #0000ff;">WHEN</span> ix.index_id <span style="color: #808080;">=</span> 1 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">'clustered'</span> <span style="color: #0000ff;">WHEN</span> ix.index_id <span style="color: #808080;">=</span> 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">'heap'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">'nonclustered'</span> <span style="color: #0000ff;">END</span> +
		 <span style="color: #0000ff;">CASE</span> <span style="color: #0000ff;">WHEN</span> ix.ignore_dup_key <span style="color: #808080;">&lt;&gt;</span> 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', ignore duplicate keys'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END</span> +
		<span style="color: #0000ff;"> CASE WHEN</span> ix.is_unique <span style="color: #808080;">&lt;&gt;</span> 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', unique'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END</span> +
		<span style="color: #0000ff;"> CASE WHEN</span> ix.is_primary_key <span style="color: #808080;">&lt;&gt;</span> 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', primary key'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END</span> <span style="color: #0000ff;">AS</span> <span style="color: #0000ff;">VARCHAR</span>(210)) <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'Index Type'</span>,
	<span style="color: #ff00ff;">STATS_DATE</span>(ix.[object_id], ix.index_id) <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'Last Stats Update'</span>,
	fr.page_count,
	fr.fragmentation,
	dos.range_scan_count,
		<span style="color: #ff00ff;">SUM</span>(<span style="color: #0000ff;">ROWS</span>)		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'row_count'</span>,
		su.[Reserved(KB)],
		su.[Used(KB)],
		h.HardCodedIndexes	<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'Indexes Hard-Coded'</span>,
		<span style="color: #ff00ff;">ISNULL</span>(<span style="color: #ff00ff;">REPLACE</span>( <span style="color: #ff00ff;">REPLACE</span>( <span style="color: #ff00ff;">REPLACE</span>(
        (   <span style="color: #0000ff;">SELECT</span> c.name	<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'columnName'</span>
            <span style="color: #0000ff;">FROM</span> [sys].[index_columns] <span style="color: #0000ff;">AS</span> sic
				<span style="color: #808080;">INNER JOIN</span> [sys].[columns] <span style="color: #0000ff;">AS</span> c
					<span style="color: #0000ff;">ON</span> c.column_id = sic.column_id
						<span style="color: #808080;">AND</span> c.[object_id] = sic.[object_id]
            <span style="color: #0000ff;">WHERE</span> sic.[object_id] = ix.[object_id]
                <span style="color: #808080;">AND</span> sic.index_id = ix.index_id
                <span style="color: #808080;">AND</span> is_included_column = 0
            <span style="color: #0000ff;">ORDER BY</span> sic.index_column_id
            <span style="color: #0000ff;">FOR XML Raw</span>)
            , <span style="color: #ff0000;">'"/&gt;&lt;row columnName="'</span>, <span style="color: #ff0000;">', '</span>)
            , <span style="color: #ff0000;">'&lt;row columnName="'</span>, <span style="color: #ff0000;">''</span>)
            , <span style="color: #ff0000;">'"/&gt;'</span>, <span style="color: #ff0000;">''</span>), <span style="color: #ff0000;">''</span>) <span style="color: #0000ff;">AS </span><span style="color: #ff0000;">'indexed_columns'</span>,
		<span style="color: #ff00ff;">ISNULL</span>(<span style="color: #ff00ff;">REPLACE</span>(<span style="color: #ff00ff;">REPLACE</span>(<span style="color: #ff00ff;">REPLACE</span>(
        (   <span style="color: #0000ff;">SELECT</span> c.name <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'columnName'</span>
            <span style="color: #0000ff;">FROM</span> [sys].[index_columns] <span style="color: #0000ff;">AS</span> sic
				<span style="color: #808080;">INNER JOIN</span> [sys].[columns] <span style="color: #0000ff;">AS</span> c
					<span style="color: #0000ff;">ON</span> c.column_id = sic.column_id
						<span style="color: #808080;">AND</span> c.[object_id] = sic.[object_id]
            <span style="color: #0000ff;">WHERE</span> sic.[object_id] = ix.[object_id]
                <span style="color: #666699;">AND</span> sic.index_id = ix.index_id
                <span style="color: #666699;">AND</span> is_included_column = 1
           <span style="color: #0000ff;"> ORDER BY</span> sic.index_column_id
            <span style="color: #0000ff;">FOR XML Raw</span>)
            , <span style="color: #ff0000;">'"/&gt;&lt;row columnName="'</span>, <span style="color: #ff0000;">', '</span>)
            , <span style="color: #ff0000;">'&lt;row columnName="'</span>, <span style="color: #ff0000;">''</span>)
            , <span style="color: #ff0000;">'"/&gt;'</span>, <span style="color: #ff0000;">''</span>), <span style="color: #ff0000;">''</span>)        <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'included_columns'</span>,
        fr.partitionNumber,
	dus.last_user_seek,
	dus.last_system_seek,
	dus.last_user_scan,
	dus.last_system_scan,
	dus.last_user_lookup,
	dus.last_system_lookup,
	ix.[object_id],
	ix.index_id,
	<span style="color: #ff00ff;">COUNT</span>(partition_number)		<span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'partition_count'</span>,
	<span style="color: #ff00ff;">ISNULL</span>(cte.partition_scheme_name,<span style="color: #ff0000;">''</span>) <span style="color: #0000ff;">AS</span> <span style="color: #ff0000;">'partition_scheme_name'</span>,
	su.TableCreateDate,
	su.TableLastModified,
	dos.singleton_lookup_count,
	dos.forwarded_fetch_count,
	dos.lob_fetch_in_pages,
	dos.lob_fetch_in_bytes,
	dos.row_lock_count,
	dos.row_lock_wait_count,
	dos.row_lock_wait_in_ms,
	dos.page_latch_wait_count,
	dos.page_io_latch_wait_count
<span style="color: #0000ff;">FROM</span> [sys].[indexes] <span style="color: #0000ff;">AS</span> ix
	<span style="color: #808080;">INNER JOIN</span> [sys].[partitions] <span style="color: #0000ff;">AS</span> sp
		<span style="color: #0000ff;">ON</span> ix.OBJECT_ID = sp.OBJECT_ID
			<span style="color: #808080;">AND</span> ix.index_id = sp.index_id
	<span style="color: #808080;">INNER JOIN</span> [sys].[tables] <span style="color: #0000ff;">AS</span> st
		<span style="color: #0000ff;">ON</span> ix.OBJECT_ID = st.OBJECT_ID
	<span style="color: #808080;">LEFT OUTER JOIN</span> indexCTE <span style="color: #0000ff;">AS</span> cte
		<span style="color: #0000ff;">ON</span> ix.data_space_id = cte.data_space_id
	<span style="color: #808080;">LEFT OUTER JOIN</span> DMV_OperationalStats <span style="color: #0000ff;">AS</span> dos
		<span style="color: #0000ff;">ON</span> dos.index_id = ix.index_id
			<span style="color: #808080;">AND</span> dos.object_id = ix.object_id
	<span style="color: #808080;">INNER JOIN</span> DMV_UsageStats <span style="color: #0000ff;">AS</span> dus
		<span style="color: #0000ff;">ON</span> ix.name = dus.index_name
	<span style="color: #808080;">LEFT OUTER JOIN</span> Fragmentation <span style="color: #0000ff;">AS</span> fr
		<span style="color: #0000ff;">ON</span> fr.objectID = IX.OBJECT_ID
			<span style="color: #808080;">AND</span> fr.databaseName = dus.database_name
			<span style="color: #808080;">AND</span> fr.indexID = ix.index_id
	<span style="color: #808080;">LEFT OUTER JOIN</span> SpaceUsed <span style="color: #0000ff;">AS</span> su
		<span style="color: #0000ff;">ON</span> fr.objectid = su.object_id
			<span style="color: #808080;">AND</span> su.index_id = fr.indexID
	<span style="color: #808080;">CROSS JOIN</span> HardCodedIndexes <span style="color: #0000ff;">AS</span> h
<span style="color: #0000ff;">WHERE</span> ix.[object_id] = <span style="color: #ff00ff;">ISNULL</span>(@objectID, ix.[object_id])
<span style="color: #0000ff;">GROUP BY</span> st.name,
<span style="color: #ff00ff;">ISNULL</span>(ix.name, <span style="color: #ff0000;">''</span>),
ix.[object_id],
ix.index_id,
<span style="color: #ff00ff;">CAST</span>(<span style="color: #0000ff;">CASE</span> <span style="color: #0000ff;">WHEN</span> ix.index_id = 1 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">'clustered'</span> <span style="color: #0000ff;">WHEN</span> ix.index_id <span style="color: #808080;">=</span> 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">'heap'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">'nonclustered'</span> <span style="color: #0000ff;">END</span>
	<span style="color: #808080;">+</span> <span style="color: #0000ff;">CASE</span> <span style="color: #0000ff;">WHEN</span> ix.ignore_dup_key <span style="color: #808080;">&lt;&gt;</span> 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', ignore duplicate keys'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END</span>
	<span style="color: #808080;">+</span> <span style="color: #0000ff;">CASE</span> <span style="color: #0000ff;">WHEN</span> ix.is_unique <span style="color: #808080;">&lt;&gt;</span> 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', unique'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END</span>
	<span style="color: #808080;">+</span> <span style="color: #0000ff;">CASE</span> <span style="color: #0000ff;">WHEN</span> ix.is_primary_key <span style="color: #808080;">&lt;&gt;</span> 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', primary key'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END </span><span style="color: #0000ff;">AS VARCHAR</span>(210)),
<span style="color: #ff00ff;">ISNULL</span>(cte.partition_scheme_name, <span style="color: #ff0000;">''</span>),
<span style="color: #ff00ff;">ISNULL</span>(cte.partition_function_name, <span style="color: #ff0000;">''</span>),
dus.database_name,
dus.user_seeks,
dus.last_user_seek,
dus.last_system_seek,
dus.user_scans,
dus.last_user_scan,
dus.last_system_scan,
dus.user_lookups,
dus.last_user_lookup,
dus.last_system_lookup,
dus.user_updates,
dos.range_scan_count,
dos.singleton_lookup_count,
dos.forwarded_fetch_count,
dos.lob_fetch_in_pages,
dos.lob_fetch_in_bytes,
dos.row_lock_count,
dos.row_lock_wait_count,
dos.row_lock_wait_in_ms,
dos.page_latch_wait_count,
dos.page_io_latch_wait_count,
fr.fragmentation,
fr.partitionNumber,
fr.page_count,
su.[Reserved(KB)],
su.[Used(KB)],
su.TableCreateDate,
su.TableLastModified,
h.HardCodedIndexes
<span style="color: #0000ff;">ORDER BY</span> dus.user_seeks <span style="color: #0000ff;">ASC</span>,
dus.user_scans ASC,
dus.user_lookups ASC,
<span style="color: #ff00ff;">CAST</span>(<span style="color: #0000ff;">CASE</span> <span style="color: #0000ff;">WHEN</span> ix.index_id = 1 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">'clustered'</span> <span style="color: #0000ff;">WHEN</span> ix.index_id = 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">'heap'</span> <span style="color: #0000ff;">ELSE</span> 'nonclustered' <span style="color: #0000ff;">END</span> <span style="color: #808080;">+</span>
    <span style="color: #0000ff;"> CASE WHEN</span> ix.ignore_dup_key &lt;&gt; 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', ignore duplicate keys'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END</span> <span style="color: #808080;">+</span>
   <span style="color: #0000ff;"> CASE WHEN</span> ix.is_unique &lt;&gt; 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', unique'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END</span> <span style="color: #808080;">+</span>
    <span style="color: #0000ff;"> CASE WHEN</span> ix.is_primary_key &lt;&gt; 0 <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">', primary key'</span> <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #0000ff;">END </span><span style="color: #0000ff;">AS VARCHAR</span>(210)) <span style="color: #0000ff;">DESC</span>,
fr.page_count <span style="color: #0000ff;">DESC</span>
<span style="color: #0000ff;">OPTION (MAXDOP 1</span>);</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2011/12/06/index-interrogation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Incremental Insert from MySQL to MSSQL via SSIS with Variables</title>
		<link>http://ksql.net/2011/12/06/incremental-insert-from-mysql-to-mssql-via-ssis-with-variables/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=incremental-insert-from-mysql-to-mssql-via-ssis-with-variables</link>
		<comments>http://ksql.net/2011/12/06/incremental-insert-from-mysql-to-mssql-via-ssis-with-variables/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 23:15:45 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SSIS]]></category>
		<category><![CDATA[ADO.NET Connection]]></category>
		<category><![CDATA[Expressions]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[MySQL Data Provider]]></category>
		<category><![CDATA[Variables]]></category>
		<category><![CDATA[Visual Studio 2008]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=135</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/development/" title="View all posts in Development" rel="category tag">Development</a>, <a href="http://ksql.net/category/development/ssis/" title="View all posts in SSIS" rel="category tag">SSIS</a></p><p>Tags: <a href="http://ksql.net/tag/ado-net-connection/" rel="tag">ADO.NET Connection</a>, <a href="http://ksql.net/tag/expressions/" rel="tag">Expressions</a>, <a href="http://ksql.net/tag/mysql/" rel="tag">MySQL</a>, <a href="http://ksql.net/tag/mysql-data-provider/" rel="tag">MySQL Data Provider</a>, <a href="http://ksql.net/tag/ssis/" rel="tag">SSIS</a>, <a href="http://ksql.net/tag/variables/" rel="tag">Variables</a>, <a href="http://ksql.net/tag/visual-studio-2008/" rel="tag">Visual Studio 2008</a></p>I recently inherited a SSIS package that moves a daily snapshot of a log table on a MySQL database over to a MSSQL database. The existing tasks truncate the destination tables and perform a fresh insert every night which works for now since the tables are relatively small. A log table, on the other hand, [...]<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2011/12/06/incremental-insert-from-mysql-to-mssql-via-ssis-with-variables/' title='Incremental Insert from MySQL to MSSQL via SSIS with Variables'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>I recently inherited a SSIS package that moves a daily snapshot of a log table on a MySQL database over to a MSSQL database. The existing tasks truncate the destination tables and perform a fresh insert every night which works for now since the tables are relatively small. A log table, on the other hand, needs to be moved in increments because of it&#8217;s size.  I needed to be able to get the identifier for the last inserted row from my MSSQL table, set it as a variable in the SSIS package, and then pass it as a parameter in my query against the MySQL database. The following steps are shown in Visual Studio 2008.</p>
<p>The first step is to verify you have the correct MySQL drivers on your development machine. I used 5.0.5. You can download the necessary drivers for free from <a title="MySQL.com" href="http://mysql.com" target="_blank">mysql.com</a>.</p>
<p>Now create a solution and your SSIS package. Add a MSSQL connection, then a MySQL connection. To create a MySQL connection, right click in the Connection Managers pane and choose New ADO.NET Connection and then click New. In the Provider dropdown you should have an option for MySQL Data Provider in the .Net Providers tree. If not, you&#8217;ll need to add a MySQL Connector to Visual Studio. Go to the <a title="MySQL NET Connector" href="http://www.mysql.com/downloads/connector/net/" target="_blank">mysql.com download page</a>, find the section for Connector/Net and download the database driver. After installation you&#8217;ll need to close and re-open Visual Studio for the connector to show.</p>
<p>I named my connection MySQL. In the image below you can see the MySQL Data Provider connection and the fields I filled in: Database, Server, Password, and User ID.</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/MySQLConnection.jpg"><img class="aligncenter size-full wp-image-140" title="MySQLConnection" src="http://ksql.net/wp-content/uploads/2011/12/MySQLConnection.jpg" alt="" width="610" height="624" /></a></p>
<p>Now we will add two variables: one for the last LogID in our destination log table and another to assist with dynamically building the SQL command that we will send to the source MySQL database. The scope is the entire package and not one of the steps. I also added an Execute SQL task and a Data Flow task in my Control Flow that you can see to the right; they&#8217;re named so that their purpose should be fairly obvious <img src='http://ksql.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  See the image below:</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/Image1.jpg"><img class="aligncenter size-full wp-image-143" title="Image1" src="http://ksql.net/wp-content/uploads/2011/12/Image1.jpg" alt="" width="843" height="746" /></a></p>
<p>Double click the execute SQL task &#8220;Get last LogID&#8221; in the Control Flow window so we can begin filling out the task. In the Execute SQL Task Editor window that pops up you&#8217;ll need to go to General and match the settings in the image below. As you can see my SQLStatement is a simple select that gets the most recent id from my MSSQL log table.</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/GetID.jpg"><img class="aligncenter size-full wp-image-146" title="GetID" src="http://ksql.net/wp-content/uploads/2011/12/GetID.jpg" alt="" width="883" height="627" /></a>Once you&#8217;ve set General skip down to Result Set and assign the output from your SQL statement to the MaxLogID variable you created earlier. You&#8217;ll need to click Add and choose your MaxLogID in the Variable Name dropdown. The image below shows how my Result Set window looks.</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/Image3.jpg"><img class="aligncenter size-full wp-image-148" title="Image3" src="http://ksql.net/wp-content/uploads/2011/12/Image3.jpg" alt="" width="752" height="637" /></a></p>
<p>Next we&#8217;ll set up our data flow task &#8220;Copy MySQL data to MSSQL database&#8221;. Double click it in the Control Flow window and add ADO NET Source from Data Flow Sources and OLD DB Destination from Data Flow Destinations. We&#8217;ll assume instructions for associating the MSSQL connection to the OLE DB Destination step aren&#8217;t needed. Setting up the ADO NET Source is trickier.</p>
<p>First we set the ADO.NET connection manager to point to our MySQL connection. Data access mode should be &#8220;SQL command&#8221;. Last, you&#8217;ll need to put your MySQL query into the SQL command text. I aliased the table as l and every column must be surrounded by grave accents (to the left of the 1 key on a QWERTY keyboard).  I ran into a DATETIME conversion issue where the date in MySQL was stored as UNIXTIME and I wanted to use DATETIME so I&#8217;ve shown that conversion in my query as well. Now, for your WHERE clause, you&#8217;ll need to choose a valid entry. I chose 0, which you&#8217;ll see is surrounded by standard single quotes. Note: this is not what will execute at runtime.  See below for my settings.</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/ADOconn.jpg"><img class="aligncenter size-full wp-image-151" title="ADOconn" src="http://ksql.net/wp-content/uploads/2011/12/ADOconn.jpg" alt="" width="838" height="724" /></a>Finally the the most difficult task: building the query with the MaxLogID parameter. Return to the Control Flow tab and open your SQLCommand variable. Your value should be<span style="text-decoration: underline;"> your MySQL query all the way up to but not including your variable</span>. It must be contained in a single line with no carriage returns.</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/QueryFirstHalf.jpg"><img class="aligncenter size-full wp-image-156" title="QueryFirstHalf" src="http://ksql.net/wp-content/uploads/2011/12/QueryFirstHalf.jpg" alt="" width="616" height="133" /></a></p>
<p>Next, single click the &#8220;Copy MySQL data to MSSQL database&#8221; task and look in your Properties window. Scroll to and click Expressions.</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/ExpressBox.jpg"><img class="aligncenter size-full wp-image-157" title="ExpressBox" src="http://ksql.net/wp-content/uploads/2011/12/ExpressBox.jpg" alt="" width="880" height="546" /></a></p>
<p>Click the ellipsis with three periods in it and a Property Expressions Editor should pop up. Choose [ADO NET Source].[SqlCommand] for Property. Now we will build our expression. Click the far right ellipsis to bring up the Expression Builder. Expand the Variables tree and drag-and-drop User::SQLCommand into the Expression window.</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/ExpBuilder.jpg"><img class="aligncenter size-full wp-image-158" title="ExpBuilder" src="http://ksql.net/wp-content/uploads/2011/12/ExpBuilder.jpg" alt="" width="629" height="534" /></a></p>
<p>This is how we will append the rest of the query to what we set the SQLCommand variable to earlier. Next, add a single quote to the right of your expression (addition symbol, space, double quote, space, single quote,double quote, space, addition symbol).  Since we are building a string to execute we&#8217;ll need to cast our MaxLogID variable as a string. Expand Type Casts and choose (DT_STR, &lt;&lt;length&gt;&gt;, &lt;&lt;code_page&gt;&gt;). Drag and drop it to the Expression window then fill in an appropriate length you expect your variable to be. 1252 is standard for &lt;&lt;code_page&gt;&gt;.  Now add your MaxLogID variable by dragging and dropping it from the Variables tree. Last, append another single quote to finish out the query. Below is how my finished expression appears.</p>
<p><a href="http://ksql.net/wp-content/uploads/2011/12/FinishedExpression1.jpg"><img class="aligncenter size-full wp-image-155" title="FinishedExpression" src="http://ksql.net/wp-content/uploads/2011/12/FinishedExpression1.jpg" alt="" width="688" height="342" /></a>That&#8217;s it! One other issue I ran into was that my package was timing out when validating into the MySQL server, so I set DelayValidation to True.</p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2011/12/06/incremental-insert-from-mysql-to-mssql-via-ssis-with-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Poor Query Plans</title>
		<link>http://ksql.net/2011/09/10/poor-query-plans/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=poor-query-plans</link>
		<comments>http://ksql.net/2011/09/10/poor-query-plans/#comments</comments>
		<pubDate>Sat, 10 Sep 2011 17:37:27 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[Administration]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=131</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/administration/" title="View all posts in Administration" rel="category tag">Administration</a></p><p></p><table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2011/09/10/poor-query-plans/' title='Poor Query Plans'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p><a href="http://ksql.net/wp-content/PoorQueryPlan.jpg"><img class="aligncenter size-full wp-image-132" title="PoorQueryPlan" src="http://ksql.net/wp-content/uploads/2011/09/IMG_2490.jpg" alt="" width="320" height="422" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2011/09/10/poor-query-plans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing a Comma-Delimited String</title>
		<link>http://ksql.net/2011/08/29/parsing-a-comma-delimited-string/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=parsing-a-comma-delimited-string</link>
		<comments>http://ksql.net/2011/08/29/parsing-a-comma-delimited-string/#comments</comments>
		<pubDate>Tue, 30 Aug 2011 00:59:12 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[comma delimited string]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=118</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/development/" title="View all posts in Development" rel="category tag">Development</a>, <a href="http://ksql.net/category/development/tsql/" title="View all posts in T-SQL" rel="category tag">T-SQL</a></p><p>Tags: <a href="http://ksql.net/tag/comma-delimited-string/" rel="tag">comma delimited string</a>, <a href="http://ksql.net/tag/tsql/" rel="tag">T-SQL</a></p>There are oodles of solutions to this problem out there, but this is one I hacked together when I needed a quick solution. Say you have a varchar parameter containing a comma separated list of integers e.g. &#8217;101,102, &#8230; &#8216; that you need inserted to a table. This script will do that pretty quickly, with [...]<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2011/08/29/parsing-a-comma-delimited-string/' title='Parsing a Comma-Delimited String'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>There are oodles of solutions to this problem out there, but this is one I hacked together when I needed a quick solution. Say you have a varchar parameter containing a comma separated list of integers e.g. &#8217;101,102, &#8230; &#8216; that you need inserted to a table. This script will do that pretty quickly, with the only annoying caveat that the string must end with a comma! Suggestions on how to tweak the code so that isn&#8217;t a hurdle would not be unwelcome.</p>
<p>(I set statistics on so I could easily compare this to other solutions)</p>
<p style="padding-left: 30px;"><span style="color: #0000ff;"><code>SET STATISTICS IO ON<br />
SET NOCOUNT ON<br />
GO</code></span></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">DECLARE</span> @VarcharList <span style="color: #0000ff;">NVARCHAR</span>(256) = <span style="color: #ff0000;">'100,101,102,103,104,105,106,107'</span><br />
<span style="color: #0000ff;">DECLARE</span> @DestinationTable <span style="color: #0000ff;">TABLE</span>(ID <span style="color: #0000ff;">TINYINT</span>)</code></p>
<p style="padding-left: 30px;"><span style="color: #339966;"><code>-- Caveat: IDList string must be comma delimited and end with ',' e.g. '111,222,333,'</code></span></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">IF</span> <span style="color: #999999;">RIGHT</span>(@VarcharList,1) &lt;&gt; <span style="color: #ff0000;">','</span><br />
<span style="color: #0000ff;">BEGIN</span><br />
<span style="color: #0000ff;">SET</span> @VarcharList = @VarcharList + <span style="color: #ff0000;">','</span><br />
<span style="color: #0000ff;">END</span></code></p>
<p style="padding-left: 30px;"><code><span style="color: #339966;">-- Break out IDs from @IDList into @DestinationTable</span><br />
<span style="color: #0000ff;">WHILE</span> 1=1<br />
<span style="color: #0000ff;">BEGIN</span><br />
<span style="color: #0000ff;">DECLARE</span> @CommaLocation <span style="color: #0000ff;">INT</span> = <span style="color: #ff00ff;">CHARINDEX</span>(<span style="color: #ff0000;">','</span>,@VarcharList)</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> @DestinationTable(ID) <span style="color: #0000ff;">SELECT</span> <span style="color: #999999;">LEFT</span>(@VarcharList,@CommaLocation-1)</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">SET</span> @VarcharList = <span style="color: #999999;">RIGHT</span>(@VarcharList,(<span style="color: #ff00ff;">LEN</span>(@VarcharList)-@CommaLocation))</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">IF</span> <span style="color: #ff00ff;">LEN</span>(@VarcharList) = 0</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">BREAK</span><br />
<span style="color: #0000ff;">END</span></code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">SELECT</span> * <span style="color: #0000ff;">FROM</span> @DestinationTable</code></p>
<p style="padding-left: 30px;"><span style="color: #0000ff;"><code>SET STATISTICS IO OFF<br />
GO</code></span></p>
<p>Your results should be as follows:</p>
<p><img class="aligncenter size-full wp-image-120" title="Output" src="http://ksql.net/wp-content/uploads/2011/08/Rows.png" alt="Output" width="75" height="195" /></p>
<p>And here is my statistics output:</p>
<p><img class="aligncenter size-full wp-image-121" title="Stats" src="http://ksql.net/wp-content/uploads/2011/08/Stats.png" alt="Stats" width="1110" height="170" /></p>
<p>I&#8217;ve not tried this on a set more then 100 records or so and cannot attest to this working well for large data sets.</p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2011/08/29/parsing-a-comma-delimited-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selecting Odd or Even Rows with ABS() Function</title>
		<link>http://ksql.net/2011/08/29/selecting-odd-or-even-rows-with-abs-function/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=selecting-odd-or-even-rows-with-abs-function</link>
		<comments>http://ksql.net/2011/08/29/selecting-odd-or-even-rows-with-abs-function/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 19:00:12 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ABS()]]></category>
		<category><![CDATA[Even]]></category>
		<category><![CDATA[Odd]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=112</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/development/" title="View all posts in Development" rel="category tag">Development</a></p><p>Tags: <a href="http://ksql.net/tag/abs/" rel="tag">ABS()</a>, <a href="http://ksql.net/tag/even/" rel="tag">Even</a>, <a href="http://ksql.net/tag/odd/" rel="tag">Odd</a>, <a href="http://ksql.net/tag/tsql/" rel="tag">T-SQL</a></p>Short little script that allows you to select Odd or Even rows from a table with an identity column: IF OBJECT_ID('tempdb.dbo.#TestData','U') IS NOT NULL DROP TABLE #TestData; CREATE TABLE #TestData( ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL, Glaven VARCHAR(100)) INSERT INTO #TestData(Glaven) SELECT NEWID() UNION SELECT NEWID() UNION SELECT NEWID() UNION SELECT NEWID() UNION SELECT NEWID() [...]<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2011/08/29/selecting-odd-or-even-rows-with-abs-function/' title='Selecting Odd or Even Rows with ABS() Function'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>Short little script that allows you to select Odd or Even rows from a table with an identity column:</p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">IF</span> <span style="color: #ff00ff;">OBJECT_ID</span>(<span style="color: #ff0000;">'tempdb.dbo.#TestData'</span>,<span style="color: #ff0000;">'U'</span>) <span style="color: #999999;">IS NOT NULL</span> <span style="color: #0000ff;">DROP</span> <span style="color: #0000ff;">TABLE</span> #TestData;<br />
<span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span> #TestData(<br />
ID <span style="color: #0000ff;">INT</span> <span style="color: #0000ff;">IDENTITY</span>(1,1) <span style="color: #0000ff;">PRIMARY</span> <span style="color: #0000ff;">KEY</span> <span style="color: #999999;">NOT NULL</span>,<br />
Glaven <span style="color: #0000ff;">VARCHAR</span>(100))</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> #TestData(Glaven)<br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">NEWID</span>() <span style="color: #0000ff;">UNION</span><br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">NEWID</span>() <span style="color: #0000ff;">UNION</span><br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">NEWID</span>() <span style="color: #0000ff;">UNION</span><br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">NEWID</span>() <span style="color: #0000ff;">UNION</span><br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">NEWID</span>()</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">SELECT</span> ID, Glaven<br />
<span style="color: #0000ff;">FROM</span> #TestData<br />
<span style="color: #0000ff;">WHERE</span> <span style="color: #ff00ff;">ABS</span>(ID) % 2 = 1 <span style="color: #339966;">-- Select only Odds!</span></code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">SELECT</span> ID, Glaven<br />
<span style="color: #0000ff;">FROM</span> #TestData<br />
<span style="color: #0000ff;">WHERE</span> <span style="color: #ff00ff;">ABS</span>(ID) % 2 = 0 <span style="color: #339966;">-- Select only Evens!</span></code></p>
<p><img class="aligncenter size-full wp-image-114" title="OddOrEvenRows" src="http://ksql.net/wp-content/uploads/2011/08/OddOrEvenRows.jpg" alt="OddOrEvenRows" width="952" height="730" /></p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2011/08/29/selecting-odd-or-even-rows-with-abs-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monitoring a While Loop</title>
		<link>http://ksql.net/2011/08/29/monitoring-a-while-loop/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=monitoring-a-while-loop</link>
		<comments>http://ksql.net/2011/08/29/monitoring-a-while-loop/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 18:48:05 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[WHILE Loop]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=71</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/development/" title="View all posts in Development" rel="category tag">Development</a></p><p>Tags: <a href="http://ksql.net/tag/sql-server-2008/" rel="tag">SQL Server 2008</a>, <a href="http://ksql.net/tag/tsql/" rel="tag">T-SQL</a>, <a href="http://ksql.net/tag/while-loop/" rel="tag">WHILE Loop</a></p>When I&#8217;m running while loops I like to see what&#8217;s going on. The benefits to this are obvious. Here is a little script I like to use to see the number of items finishedout of the whole, the latest timestamp, and the currently processing item. First, let&#8217;s insert 10,000 rows to a temporary table. I&#8217;m [...]<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2011/08/29/monitoring-a-while-loop/' title='Monitoring a While Loop'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>When I&#8217;m running <span style="color: #000000;">while</span> loops I like to see what&#8217;s going on. The benefits to this are obvious. Here is a little script I like to use to see the number of items finishedout of the whole, the latest timestamp, and the currently processing item.</p>
<p>First, let&#8217;s insert 10,000 rows to a temporary table. I&#8217;m fond of using the <span style="color: #ff00ff;">NEWID</span>() function.</p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">IF</span> <span style="color: #ff00ff;">OBJECT_ID</span>(<span style="color: #ff0000;">'tempdb.dbo.#TestData'</span>,<span style="color: #ff0000;">'U'</span>) <span style="color: #999999;">IS NOT NULL</span> <span style="color: #0000ff;">DROP</span> <span style="color: #0000ff;">TABLE</span> #TestData</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span> #TestData(<br />
ID <span style="color: #0000ff;">INT</span> <span style="color: #0000ff;">IDENTITY</span>(1,1) <span style="color: #0000ff;">PRIMARY</span> <span style="color: #0000ff;">KEY</span> <span style="color: #999999;">NOT NULL</span>,<br />
data1 <span style="color: #0000ff;">VARCHAR</span>(100),<br />
data2 <span style="color: #0000ff;">VARCHAR</span>(12))</code></p>
<p style="padding-left: 30px;"><span style="color: #0000ff;"><code>SET NOCOUNT ON<br />
GO</code></span></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">DECLARE</span> @LoopIteration <span style="color: #0000ff;">INT</span> = 1</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">WHILE</span> @LoopIteration &lt; 10001<br />
<span style="color: #0000ff;">BEGIN</span></code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> #TestData(data1)<br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">NEWID</span>()</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">SET</span> @LoopIteration = @LoopIteration + 1</code></p>
<p style="padding-left: 30px;"><span style="color: #0000ff;"><code>END</code></span></p>
<p>Now that we have test data, let&#8217;s run an update in a while loop and monitor it&#8217;s progress.</p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">DECLARE</span> @LoopIteration <span style="color: #0000ff;">INT</span> = 1</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">DECLARE</span> @RowsToProcess <span style="color: #0000ff;">CHAR</span>(5) = <span style="color: #ff00ff;">CONVERT</span>(<span style="color: #0000ff;">VARCHAR</span>,(<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">COUNT</span>(0) <span style="color: #0000ff;">FROM</span> #TestData))</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">DECLARE</span> @Data1 <span style="color: #0000ff;">VARCHAR</span>(100) = (<span style="color: #0000ff;">SELECT</span> data1 <span style="color: #0000ff;">FROM</span> #TestData <span style="color: #0000ff;">WHERE</span> ID = @LoopIteration)</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">DECLARE</span> @MaxIteration <span style="color: #0000ff;">INT</span> = (<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">COUNT</span>(0) <span style="color: #0000ff;">FROM</span> #TestData) + 1</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">DECLARE</span> @Msg <span style="color: #0000ff;">VARCHAR</span>(256) = <span style="color: #ff0000;">'['</span> + <span style="color: #ff00ff;">CAST</span>(@LoopIteration <span style="color: #0000ff;">AS</span> <span style="color: #0000ff;">VARCHAR</span>) + <span style="color: #ff0000;">'/'</span> + @RowsToProcess + <span style="color: #ff0000;">']'</span> + <span style="color: #ff0000;">' Processing '</span> + @Data1 + <span style="color: #ff0000;">' at '</span> + <span style="color: #ff00ff;">CONVERT</span>(<span style="color: #0000ff;">CHAR</span>(8),<span style="color: #ff00ff;">GETDATE</span>(),14)</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">WHILE</span> @LoopIteration &lt; @MaxIteration<br />
<span style="color: #0000ff;">BEGIN</span></code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">RAISERROR</span>(@Msg, 0, 1) <span style="color: #0000ff;">WITH</span> <span style="color: #0000ff;">NOWAIT</span></code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">UPDATE</span> #TestData<br />
<span style="color: #0000ff;">SET</span> data2 = <span style="color: #ff00ff;">REVERSE</span>(<span style="color: #999999;">LEFT</span>(data1,12)) <span style="color: #339966;">-- let's have some fun!</span><br />
<span style="color: #0000ff;">WHERE</span> ID = @LoopIteration</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">SET</span> @LoopIteration = @LoopIteration + 1;<br />
<span style="color: #0000ff;">SET</span> @Data1 = (<span style="color: #0000ff;">SELECT</span> data1 <span style="color: #0000ff;">FROM</span> #TestData <span style="color: #0000ff;">WHERE</span> ID = @LoopIteration)<br />
<span style="color: #0000ff;">SET</span> @Msg = <span style="color: #ff0000;">'['</span> + <span style="color: #ff00ff;">CAST</span>(@LoopIteration <span style="color: #0000ff;">AS</span> <span style="color: #0000ff;">VARCHAR</span>) + <span style="color: #ff0000;">'/'</span> + @RowsToProcess + <span style="color: #ff0000;">']'</span> + <span style="color: #ff0000;">'</span> <span style="color: #ff0000;">Processing '</span> + (<span style="color: #0000ff;">SELECT</span> data1 <span style="color: #0000ff;">FROM</span> #TestData <span style="color: #0000ff;">WHERE</span> ID = @LoopIteration) + <span style="color: #ff0000;">' at '</span> + <span style="color: #ff00ff;">CONVERT</span>(<span style="color: #0000ff;">CHAR</span>(8),<span style="color: #ff00ff;">GETDATE</span>(),14)</code></p>
<p style="padding-left: 30px;"><span style="color: #0000ff;"><code>END</code></span></p>
<p>Voila! You should see detailed progress in your Messages window. This script is flexible, and I&#8217;ve added @@ROWCOUNT and other relevant tidbits for different use cases. Below is the output from the script:</p>
<p><img class="aligncenter size-full wp-image-106" title="WhileLoop" src="http://ksql.net/wp-content/uploads/2011/08/WhileLoop.jpg" alt="WhileLoop" width="1222" height="930" /></p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2011/08/29/monitoring-a-while-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Importing and Exporting Data with BCP</title>
		<link>http://ksql.net/2011/08/29/importing-and-exporting-data-with-bcp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=importing-and-exporting-data-with-bcp</link>
		<comments>http://ksql.net/2011/08/29/importing-and-exporting-data-with-bcp/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 18:16:32 +0000</pubDate>
		<dc:creator>ksqldotnet</dc:creator>
				<category><![CDATA[BCP]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[.data]]></category>
		<category><![CDATA[.log]]></category>
		<category><![CDATA[Export Data]]></category>
		<category><![CDATA[Import Data]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://ksql.net/?p=69</guid>
		<description><![CDATA[<table cellpadding='10'><tr><td valign='top' align='left'><p>Categories: <a href="http://ksql.net/category/development/bcp/" title="View all posts in BCP" rel="category tag">BCP</a>, <a href="http://ksql.net/category/development/" title="View all posts in Development" rel="category tag">Development</a></p><p>Tags: <a href="http://ksql.net/tag/data/" rel="tag">.data</a>, <a href="http://ksql.net/tag/log/" rel="tag">.log</a>, <a href="http://ksql.net/tag/bcp/" rel="tag">BCP</a>, <a href="http://ksql.net/tag/export-data/" rel="tag">Export Data</a>, <a href="http://ksql.net/tag/import-data/" rel="tag">Import Data</a>, <a href="http://ksql.net/tag/sql-server-2008/" rel="tag">SQL Server 2008</a></p>Before I was a DBA my job was pulling email campaigns for Marketing. Back then I didn&#8217;t have DBA permissions or experience with SSIS, so I couldn&#8217;t have deployed a package even if I knew how to make one. Instead, I got around that limitation by moving data via BCP. Some claim that BCP is [...]<table width='100%'><tr><td align=right><p><b>(<a href='http://ksql.net/2011/08/29/importing-and-exporting-data-with-bcp/' title='Importing and Exporting Data with BCP'>Read more...</a>)</b></p></td></tr></table></td></tr></table>]]></description>
			<content:encoded><![CDATA[<p>Before I was a DBA my job was pulling email campaigns for Marketing. Back then I didn&#8217;t have DBA permissions or experience with SSIS, so I couldn&#8217;t have deployed a package even if I knew how to make one. Instead, I got around that limitation by moving data via BCP. Some claim that BCP is faster then SSIS. I&#8217;ve personally never done a speed test, but it&#8217;s a useful tool to have. You can run the BCP commands either in the cmd console on your local desktop or wrap the command in a stored procedure using the xp_cmdshell function.</p>
<p>First, let&#8217;s create a table and insert some fake numeric data using a fictional database named KSQL01.</p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">USE</span> [KSQL01]<br />
<span style="color: #0000ff;">GO</span></code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span> dbo.scratch_bcp_testing(ID <span style="color: #0000ff;">INT</span>)</code></p>
<p style="padding-left: 30px;"><code><span style="color: #0000ff;">INSERT</span> <span style="color: #0000ff;">INTO</span> dbo.scratch_bcp_testing(ID)<br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">CONVERT</span>(<span style="color: #0000ff;">INT</span>,<span style="color: #ff00ff;">RAND</span>()*1000) <span style="color: #0000ff;">UNION</span><br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">CONVERT</span>(<span style="color: #0000ff;">INT</span>,<span style="color: #ff00ff;">RAND</span>()*1000) <span style="color: #0000ff;">UNION</span><br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">CONVERT</span>(<span style="color: #0000ff;">INT</span>,<span style="color: #ff00ff;">RAND</span>()*1000) <span style="color: #0000ff;">UNION</span><br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">CONVERT</span>(<span style="color: #0000ff;">INT</span>,<span style="color: #ff00ff;">RAND</span>()*1000) <span style="color: #0000ff;">UNION</span><br />
<span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">CONVERT</span>(<span style="color: #0000ff;">INT</span>,<span style="color: #ff00ff;">RAND</span>()*1000)</code></p>
<p>Our data should look something like this:</p>
<p><img class="aligncenter size-full wp-image-84" title="BCP_Fake_Data_Screenshot" src="http://ksql.net/wp-content/uploads/2011/08/bcpfakedata1.png" alt="BCP_Fake_Data_Screenshot" width="78" height="162" /></p>
<p>&nbsp;</p>
<p>Next, let&#8217;s export the data to a .data file on a destination server. For your destination you can choose the SQL server you&#8217;re developing with, your local PC, or another SQL server entirely.</p>
<p><span style="color: #339966;"><code>-- Export via SSMS or a stored procedure</code></span><br />
<code><span style="color: #0000ff;">EXEC</span> <span style="color: #0000ff;">master</span>.dbo.<span style="color: #800000;">xp_cmdshell</span> <span style="color: #ff0000;">'bcp "<strong>KSQL01.dbo.scratch_bcp_testing</strong>" out "<strong>c:\\SourceData.data</strong>" -E -k -c -t"\t" -T -S<strong>SOURCESQLSERVER</strong>' &gt;&gt; "<strong>c:\logfile.log</strong>"'</span></code></p>
<p>I&#8217;ve bolded the strings you&#8217;ll need to replace with your own data. You could end the statement after the SOURCESERVER as exporting a log of actions taken is optional. If this is ran in SSMS, the results window displays the same data that is exported in the .log file. The .log file is useful to have for troubleshooting if this code is called multiple times in a stored procedure.</p>
<p><img class="aligncenter size-full wp-image-86" title="BCP_Results_Window" src="http://ksql.net/wp-content/uploads/2011/08/BCP_Results_Window.png" alt="BCP_Results_Window" width="490" height="179" /></p>
<p>When we run this in SSMS, it should export two files, the .data and the .log file to the path specified. Both can be opened in a text editor.</p>
<p><img class="aligncenter size-full wp-image-88" title="BCPLog" src="http://ksql.net/wp-content/uploads/2011/08/BCPLog.jpg" alt="BCPLog" width="634" height="500" /></p>
<p>Now we can reverse the process and import the data back to the table.</p>
<p><span style="color: #339966;"><code>-- Import .data file back into the table</code></span></p>
<p><code><span style="color: #0000ff;">EXEC</span> <span style="color: #0000ff;">master</span>.dbo.<span style="color: #800000;">xp_cmdshell</span> <span style="color: #ff0000;">'bcp "<strong>KSQL01.dbo.scratch_bcp_testing</strong>" in<strong> "\\DESTINATIONSERVER\c$\SourceData.data</strong>" -k -c -t"\t" -T<strong> -</strong>S<strong>SOURCESERVER'</strong></span></code></p>
<p>And there you have it! And old school alternative to SSIS on the off chance you need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://ksql.net/2011/08/29/importing-and-exporting-data-with-bcp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

