<?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>Aaron Lowe &#187; SQL Server</title>
	<atom:link href="http://www.aaronlowe.net/archive/category/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aaronlowe.net</link>
	<description>SELECT my.Thoughts FROM Vendoran.dbo.Brain my</description>
	<lastBuildDate>Fri, 23 Jul 2010 13:00:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>SSIS CDC LSN Debugging nightmare second problem</title>
		<link>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-second-problem/</link>
		<comments>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-second-problem/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 09:00:00 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[2008]]></category>
		<category><![CDATA[CDC]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">/post/2009/12/18/SSIS-CDC-LSN-Debugging-nightmare-second-problem.aspx</guid>
		<description><![CDATA[Continuing the CDC debugging posts: Intro First Problem Now that I have the StartLSN and EndLSN I was ready to go.  Hit my breakpoint and the variables were set appropriately.  Woo-hoo! Then…wait a minute…what? vSQLQuery.Value = "SELECT " + " c.__$operation " + " , c.Column1 " + " , c.Column2 " + " , [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing the CDC debugging posts:</p>
<ul>
<li><a href="http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-introduction/">Intro</a></li>
<li><a href="http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-first-problem/">First Problem</a></li>
</ul>
<p>Now that I have the StartLSN and EndLSN I was ready to go.  Hit my breakpoint and the variables were set appropriately.  Woo-hoo!</p>
<p>Then…wait a minute…what?</p>
<pre class="brush: csharp;">vSQLQuery.Value =
    "SELECT " +
    "    c.__$operation " +
    "    , c.Column1 " +
    "    , c.Column2 " +
    "    , ColumnN " +
    "FROM " +
    "    cdc.fn_cdc_get_net_changes_&lt;instance_name&gt;( " + sStartLSN + ",  " + sEndLSN + ", 'all') c";</pre>
<p>That statement wasn’t changing the value of vSQLQuery.  huh?</p>
<p>Ok, I don’t by any stretch of the imagination claim to be an expert at C# but that looked pretty straight forward to me, however it wasn’t changing the value.</p>
<p>So now I start looking in earnest, <a href="http://www.google.com" target="_blank">google</a>, <a href="http://www.bing.com" target="_blank">bing</a>, <a href="http://stackoverflow.com/" target="_blank">Stackoverflow</a>, <a href="http://serverfault.com/" target="_blank">Serverfault</a>, all to no avail.  This just wasn’t making sense.  I was about to call over an expert C# person (<a href="http://www.magenic.com" target="_blank">Magenic</a> is full of them <img src='http://www.aaronlowe.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) when I remembered that my previous problem was not a problem with the code but a problem with the configuration of the task.  So I start looking into the configuration of the script task and the variables that I was using and that’s where I saw it. </p>
<p>vSQLValue was set to evaluate as an expression.  I had done this so I could copy and paste the SQL query I had developed in SSMS as opposed to making the query 1 line and pasting it into the value of the variable (the variable box in SSIS can’t interpret multi-line values, it only pastes the first line).</p>
<p>Turned off the fact that it was an expression and since it had already been evaluated it set the variable correctly (score!) and now I could manipulate the vSQLQuery.Value via the script task.</p>
<p>Now as to why I couldn’t modify a variable that is set to evaluate as an expression, I don’t know I’m sure there’s some sort of order of operations or locking catch here, but I still haven’t been able to find anything official about this.  Best I saw was <a href="http://weblogs.sqlteam.com/dmauri/archive/2006/04/02/9489.aspx" target="_blank">this</a> which would indicate to me that it would work.</p>
<p>Onto the next problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-second-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSIS CDC LSN Debugging nightmare first problem</title>
		<link>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-first-problem/</link>
		<comments>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-first-problem/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 07:00:00 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[2008]]></category>
		<category><![CDATA[CDC]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">/post/2009/12/17/SSIS-CDC-LSN-Debugging-nightmare-first-problem.aspx</guid>
		<description><![CDATA[So now that we have the idea let’s look as the four main problems I encountered: The first problem I noticed was that the SQL query that was running still had nulls in on the StartLSN and EndLSN so I was querying: cdc.fn_cdc_get_net_changes_&#60;instance name&#62; (null, null, &#8216;all&#8217;) Which as I said last post, returns an [...]]]></description>
			<content:encoded><![CDATA[<p>So now that we have the idea let’s look as the four main problems I encountered:</p>
<p>The first problem I noticed was that the SQL query that was running still had nulls in on the StartLSN and EndLSN</p>
<p>so I was querying:</p>
<p>cdc.fn_cdc_get_net_changes_&lt;instance name&gt; (null, null, &#8216;all&#8217;)</p>
<p>Which as I said last post, returns an error. So throw a break point into the Script task and figure out why it’s not setting properly.&#160; This took me awhile, here’s the code:</p>
<pre class="brush: csharp;">var vStartLSN = Dts.Variables[&quot;User::sStartLSN&quot;];
var vEndLSN = Dts.Variables[&quot;User::sEndLSN&quot;];
var vSQLQuery = Dts.Variables[&quot;User::sSQLQuery&quot;];

string sStartLSN;
string sEndLSN;

Dts.VariableDispenser.LockForRead(&quot;User::sStartLSN&quot;);
Dts.VariableDispenser.LockForRead(&quot;User::sEndLSN&quot;);
Dts.VariableDispenser.LockForWrite(&quot;User::sSQLQuery&quot;);

sStartLSN = vStartLSN.Value.ToString();
sEndLSN = vEndLSN.Value.ToString();

vSQLQuery.Value =
    &quot;SELECT &quot; +
    &quot;    c.__$operation &quot; +
    &quot;    , c.Column1 &quot; +
    &quot;    , c.Column2 &quot; +
    &quot;    , ColumnN &quot; +
    &quot;FROM &quot; +
    &quot;    cdc.fn_cdc_get_net_changes_&lt;instance_name&gt;( &quot; + sStartLSN + &quot;,  &quot; + sEndLSN + &quot;, 'all') c&quot;;
Dts.Variables.Unlock();</pre>
<p>So hitting my first breakpoint, I first realized that the StartLSN variable was receiving a null.&#160; That wasn’t supposed to happen, so I looked at the SQL code I used to set the variables in the SQL Task:</p>
<p>SELECT sys.fn_cdc_get_min_lsn(&lt;instance_name&gt;) as @StartLSN</p>
<p>A-ha, that’s it. The function returns a record set and I had the task to just get a return value, not a record set.</p>
<p>So changed to:</p>
<p>SELECT @StartLSN = sys.fn_cdc_get_min_lsn(&lt;instance_name&gt;);<br />
  <br />SELECT @StartLSN as StartLSN</p>
<p>That got me past the first problem, now onto the next problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-first-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SSIS CDC LSN Debugging nightmare introduction</title>
		<link>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-introduction/</link>
		<comments>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-introduction/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 07:00:00 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[2008]]></category>
		<category><![CDATA[CDC]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">/post/2009/12/16/SSIS-CDC-LSN-Debugging-nightmare-introduction.aspx</guid>
		<description><![CDATA[The other day I was working on an SSIS implementation that was utilizing Change Data Capture (CDC) to identify the source data that needed to be transformed into the destination.&#160; This is the first time that I had used CDC other than a quick demo and I was grateful to have Whitney’s help with understanding [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was working on an SSIS implementation that was utilizing <a href="http://msdn.microsoft.com/en-us/library/bb522489.aspx" target="_blank">Change Data Capture</a> (CDC) to identify the source data that needed to be transformed into the destination.&#160; This is the first time that I had used CDC other than a quick demo and I was grateful to have <a href="http://whitneyweaver.com/" target="_blank">Whitney’s</a> help with understanding some of the…shall we say eccentricities of CDC.</p>
<p>You can see <a href="http://www.codeplex.com/wikipage?ProjectName=MSFTISProdSamples&amp;title=SS2008!Change%20Data%20Capture%20since%20Last%20Request%20Package%20Sample" target="_blank">Change Data Capture since Last Request Package Sample</a> or <a href="http://www.codeplex.com/wikipage?ProjectName=MSFTISProdSamples&amp;title=SS2008!Change%20Data%20Capture%20for%20Specified%20Interval%20Package%20Sample" target="_blank">Change Data Capture for Specified Interval Package Sample</a> for the official MS examples which is the basis of what I was trying to accomplish, although in the specified interval sample they cheated by using a database snapshot to get the starting LSN and then just ran continuously from there keeping the LSNs in variables, they didn’t address the concept of true interval run that would be come to a stopping point and then start again after an interval passes.</p>
<p>So to give a little context to this post you need to understand some basics of SSIS and CDC.&#160; CDC utilizes <a href="http://msdn.microsoft.com/en-us/library/ms190411.aspx" target="_blank">Log Sequence Numbers</a> (LSNs) to know the start and stop point of the changes you are trying to identify.&#160; CDC is table based and logs those changes to an “instance” of the table that you specify, which is basically just a copy of the table DDL with a few added columns, the new table exists within the cdc schema and the table name is what you specify via the “instance name” parameter. You can track changes on the entire table or just specific columns, and there are functions to get all the changes or just the net changes.</p>
<p>Within CDC you have some very helpful system functions to assist with figuring out what LSNs you should be selecting, including:</p>
<ul>
<li>sys.fn_cdc_get_max_lsn      <br />sys.fn_cdc_get_min_lsn       <br />sys.fn_cdc_map_time_to_lsn       <br />sys.fn_cdc_map_lsn_to_time </li>
</ul>
<p>The other thing to remember is that LSNs are binary.&#160; In SSIS there is no variable type binary.&#160; The first odd thing I noticed (and this should have been a clue to the difficulties that lie ahead) was that the sys.fn_map_time_to_lsn has a strange parameter called the ‘relational operator’ and I kid you not these are the potential values:</p>
<ul>
<li>largest less than      <br />largest less than or equal       <br />smallest greater than       <br />smallest greater than or equal </li>
</ul>
<p>Yes the actual text strings are what is needed to be entered there.&#160; The idea being that LSNs aren’t directly time related to so you need to specify the relationship in relation to the datetime your passing.&#160; </p>
<p>The next&#160; thing I noticed that is strange was that sys.fn_cdc_get_min_lsn must be instance specific, while sys.fn_cdc_get_max_lsn is the entire server.&#160; So hopefully this will be a little more straight forward in the next version. &lt;TangentRant&gt;although that’s what I thought about Service Broker which I love, but sadly still isn’t that straight forward, however that’s for another post&lt;/TangentRant&gt;&#160; </p>
<p>So to cover all bases this was my process:</p>
<p>1.) The parent package would get the LSN boundaries based upon the last successful run of the package (StartLSN) and the current run (EndLSN), passing those variables to the children packages.</p>
<p>2.) The child package which is instance specific, would then verify that that StartLSN was not less than the minimum LSN for that instance, if it was, than use the MinLSN instead.&#160; </p>
<p>3.) Utilize the TVF to get the net changes cdc.fn_cdc_get_net_changes_&lt;capture_instance&gt; (to get all changes you would use cdc.fn_cdc_get_all_changes_&lt;capture_instance&gt;)    </p>
<p>A couple other issues here when using with SSIS here:</p>
<p>1.) It’s a TVF so if you pass bad parameters as start and end LSNs, you get an error in addition to the column header information:</p>
<p>“An insufficient number of arguments were supplied for the procedure or function cdc.fn_cdc_get_net_changes_ &#8230; .”</p>
<p>2.) It’s a TVF so you can’t use it as an OLEDB data source, instead you have to use an ssis variable as the data source, which mean the query needs to be set with the proper query with null values for the Start and End LSNs (remember even though it brings back an error it will bring back column information).&#160; This also means the script task is needed to change the alter the query string variable to substitute the proper start and end LSNs at runtime.</p>
<p>So now that we have the basis of what I was doing, now onto the debugging…</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2009/12/ssis-cdc-lsn-debugging-nightmare-introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>VSDBPro project dependency</title>
		<link>http://www.aaronlowe.net/archive/2009/10/vsdbpro-project-dependency/</link>
		<comments>http://www.aaronlowe.net/archive/2009/10/vsdbpro-project-dependency/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 08:00:00 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[VSDBPro]]></category>

		<guid isPermaLink="false">/post/2009/10/02/VSDBPro-project-dependency.aspx</guid>
		<description><![CDATA[So I&#8217;ve been using Visual Studio for Database Professionals (aka VSDBPro, aka DataDude) consistently this year for primarily new database development, both for Data Warehouses and OLTP Databases.&#160; This will be the first in what hopefully will become a series of posts about Database projects. In Database Professional there is are two types of projects, [...]]]></description>
			<content:encoded><![CDATA[<p>So I&rsquo;ve been using Visual Studio for Database Professionals (aka VSDBPro, aka DataDude) consistently this year for primarily new database development, both for Data Warehouses and OLTP Databases.&nbsp; This will be the first in what hopefully will become a series of posts about Database projects.</p>
<p>In Database Professional there is are two types of projects, the database project and the database server project.&nbsp; The database project would be the user database while the database server project would be the master database.&nbsp; Neither one is specific to a particular server or instance, it&rsquo;s a Visual Studio project which can be deployed on a single or multiple instances as you need.</p>
<p>You can also create dependencies between projects so you can have a database project which had a dependency on a database server project.&nbsp; Why would you do this you ask?&nbsp; The most straight forward example would be the login to user relationship.&nbsp; You could have a database server project which creates your logins, and a database project which then creates the users in that user database mapped back to the logins in the server project.</p>
<p>You can reference a database project one of three ways:</p>
<ol>
<li>Project within the solution</li>
<li>.schema file (DB projects don&rsquo;t create .dll files)</li>
<li>Server and database variables</li>
</ol>
<p>The first two are pretty straight forward as you would just select a project or a .schema from your workspace, so let&rsquo;s look at how you would utilize variables for reference.&nbsp; First here&rsquo;s the screen to create a dependency: <br /><a href="http://www.flickr.com/photos/vendoran/3967650458/" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="DBReference" src="http://www.aaronlowe.net/wp-content/uploads/DBReference_1.jpg" border="0" alt="DBReference" width="244" height="194" /></a></p>
<p>Easy enough, just enter the variables and you would have:</p>
<p><a href="http://www.flickr.com/photos/vendoran/3967695646/" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="AddDBReference" src="http://www.aaronlowe.net/wp-content/uploads/AddDBReference_1.png" border="0" alt="AddDBReference" width="244" height="195" /></a></p>
<p>You&rsquo;ll notice in the variables the characters %(), this is what tells the Project that these are variables, and I didn&rsquo;t enter the variables this way, I just entered ServerName and when I tabbed out of the filed the $() were automatically added.&nbsp; So now you can see the variables in your command variables dialog (under project properties).&nbsp; The first two are created with every project and you&rsquo;ll see since I specified a value up above, the value is already used, instead of needing to set it at deployment time.</p>
<p><a href="http://www.flickr.com/photos/vendoran/3966923575/" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="CMdVars" src="http://www.aaronlowe.net/wp-content/uploads/CMdVars_1.png" border="0" alt="CMdVars" width="244" height="120" /></a></p>
<p>So now that we have the variables, how does this actually get used when we go to deployment?&nbsp; I&rsquo;m glad you asked, on deployment a .sql file is created that utilized sqlcmd mode.&nbsp; So the output at deployment time time will be:</p>
<p><a href="http://www.flickr.com/photos/vendoran/3966931021/" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="SQLCmd" src="http://www.aaronlowe.net/wp-content/uploads/SQLCmd_1.png" border="0" alt="SQLCmd" width="244" height="213" /></a></p>
<p>Notice that there is no value for the DefaultDataPath, as I never did set it at deployment time.&nbsp; So there you have it, you can now parameterize your project dependencies!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2009/10/vsdbpro-project-dependency/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ISNUMERIC maybe…</title>
		<link>http://www.aaronlowe.net/archive/2009/09/isnumeric-maybe%e2%80%a6/</link>
		<comments>http://www.aaronlowe.net/archive/2009/09/isnumeric-maybe%e2%80%a6/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 10:00:00 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">/post/2009/09/30/ISNUMERIC-maybee280a6.aspx</guid>
		<description><![CDATA[So the other day I was working on a T-SQL query that I was using to take data from generic data types and insert it into a strongly typed table.&#160; One of the columns was moving from an nvarchar(255) to an decimal data type.&#160; As some of the rows had character data in it, I [...]]]></description>
			<content:encoded><![CDATA[<p>So the other day I was working on a T-SQL query that I was using to take data from generic data types and insert it into a strongly typed table.&nbsp; One of the columns was moving from an nvarchar(255) to an decimal data type.&nbsp; As some of the rows had character data in it, I wrote something like this:</p>
<pre class="brush: sql;">CASE ISNUMERIC(Col1)
    WHEN 1 THEN CAST(Col1 as decimal(9,4))
    ELSE NULL
END as Col1</pre>
<p>However when I ran the query, I received this error:</p>
<p><span style="font-family: cou; color: #ff0000; font-size: x-small;">Error converting data type nvarchar to numeric.</span></p>
<p>It took me awhile to figure this one out as you can see I&rdquo;m using ISNUMERIC to check before I convert it to a number, seems pretty straight forward, so what&rsquo;s the bid deal?</p>
<p>The big deal is that I needed to read the manual&hellip;according to the ISNUMERIC function in <a href="http://msdn.microsoft.com/en-us/library/ms186272.aspx" target="_blank">BOL</a>:</p>
<p><span style="font-family: cou; font-size: x-small;">ISNUMERIC returns 1 for some characters that are not numbers, such as plus (+), minus (-), and valid currency symbols such as the dollar sign ($). For a complete list of currency symbols, see <a href="http://msdn.microsoft.com/en-us/library/ms188688.aspx">Using Monetary Data</a>.</span></p>
<p>It turned out I had a row that had a value of a single decimal point and no numbers which caused the problem.&nbsp; So remember, understand what the functions you use actually do, not what you think they do and test!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2009/09/isnumeric-maybe%e2%80%a6/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
