<?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>Tim Van Wassenhove &#187; Uncategorized</title>
	<atom:link href="http://www.timvw.be/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timvw.be</link>
	<description>The journey of a thousand miles begins with one step.</description>
	<lastBuildDate>Fri, 28 Oct 2011 09:51:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Say no to primitives in your API.. and make your software more explicit</title>
		<link>http://www.timvw.be/2011/10/28/say-no-to-primitives-in-your-api-and-make-your-software-more-explicit/</link>
		<comments>http://www.timvw.be/2011/10/28/say-no-to-primitives-in-your-api-and-make-your-software-more-explicit/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 09:51:52 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2245</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/10/28/say-no-to-primitives-in-your-api-and-make-your-software-more-explicit/" title="Say no to primitives in your API.. and make your software more explicit"></a>A while ago I wrote some code like this: A bit later the requirements changed and from now on it was required to specify the topic: In case you were using Broadcast(string message) the compiler would rightfully inform you that &#8230;<p class="read-more"><a href="http://www.timvw.be/2011/10/28/say-no-to-primitives-in-your-api-and-make-your-software-more-explicit/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/10/28/say-no-to-primitives-in-your-api-and-make-your-software-more-explicit/" title="Say no to primitives in your API.. and make your software more explicit"></a><p>A while ago I wrote some code like this:</p>
<pre class="brush: csharp; title: ; notranslate">
public interface ICanBroadcast
{
 public void Broadcast(string message) { ... }
 public void Broadcast(string message, string author) { ... }
}
</pre>
<p>A bit later the requirements changed and from now on it was required to specify the topic:</p>
<pre class="brush: csharp; title: ; notranslate">
public interface ICanBroadcast
{
 public void Broadcast(string message, string topic) { ... }
 public void Broadcast(string message, string author, string topic) { ... }
}
</pre>
<p>In case you were using Broadcast(string message) the compiler would rightfully inform you that no such method exists. In case you were using Broadcast(string message, string author) the compiler does not catch the error and incorrectly uses the author as topic. I can only hope that you have a suite of tests that makes you notice that something is wrong when you upgrade to my latest release.
<p/>
<p>Let&#8217;s make the difference between an Author and a Topic more explicit (to our API consumers and the compiler) by creating explicit types to represent the concepts:</p>
<pre class="brush: csharp; title: ; notranslate">
public interface ICanBroadcast
{
 public void Broadcast(Message message, Topic topic) { ... }
 public void Broadcast(Message message, Author author, Topic topic) { ... }
}
</pre>
<p>The joy of using a typed language <img src='http://www.timvw.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/10/28/say-no-to-primitives-in-your-api-and-make-your-software-more-explicit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Force the removal of a file with PowerShell</title>
		<link>http://www.timvw.be/2011/10/18/force-the-removal-of-a-file-with-powershell/</link>
		<comments>http://www.timvw.be/2011/10/18/force-the-removal-of-a-file-with-powershell/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 18:44:08 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2240</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/10/18/force-the-removal-of-a-file-with-powershell/" title="Force the removal of a file with PowerShell"></a>Last couple of weeks I have been generating a lot of files (and restricting their ACLs) and today I decided to remove all those files. The problem is that my user account did not have permissions on those files. Here &#8230;<p class="read-more"><a href="http://www.timvw.be/2011/10/18/force-the-removal-of-a-file-with-powershell/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/10/18/force-the-removal-of-a-file-with-powershell/" title="Force the removal of a file with PowerShell"></a><p>Last couple of weeks I have been generating a lot of files (and restricting their ACLs) and today I decided to remove all those files. The problem is that my user account did not have permissions on those files. Here is a small script that will first take ownership of the file, then grants FullControl permissions, and finally removes the file <img src='http://www.timvw.be/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<pre class="brush: powershell; title: ; notranslate">
function RemoveFile
{
	param($FileName)

	&amp;takeown /F $FileName

	$User = [System.Security.Principal.WindowsIdentity]::GetCurrent().User

	$Acl = Get-Acl $FileName
	$Acl.SetOwner($User)
	$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($User, &quot;FullControl&quot;, &quot;Allow&quot;)
	$Acl.SetAccessRule($AccessRule)
	Set-Acl $FileName $Acl

	Remove-Item $FileName
}

Get-ChildItem *.txt -R | % { RemoveFile $_.FullName; }
</pre>
<p><b>Edit on 2011-10-19</b></p>
<p>Resetting the permissions with icacls c:\output /reset /t and then calling Remove-Item c:\output -R does the trick.</p>
<pre class="brush: powershell; title: ; notranslate">
function RemoveFiles
{
 param($Directory)
 icacls $Directory /reset /t
 Remove-Item $Directory -R
}

RemoveFiles c:\output;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/10/18/force-the-removal-of-a-file-with-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove all access rules from a directory</title>
		<link>http://www.timvw.be/2011/09/22/remove-all-access-rules-from-a-directory/</link>
		<comments>http://www.timvw.be/2011/09/22/remove-all-access-rules-from-a-directory/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 17:56:04 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2226</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/09/22/remove-all-access-rules-from-a-directory/" title="Remove all access rules from a directory "></a>A while ago i needed to write some code that removes all (existing/inherited) access rules from a given directory. It was pretty frustrating to notice that all my attempts seemed to fail (RemoveAccessRule, PurgeAccessRule, &#8230;) Finally i found that SetAccessRuleProtection &#8230;<p class="read-more"><a href="http://www.timvw.be/2011/09/22/remove-all-access-rules-from-a-directory/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/09/22/remove-all-access-rules-from-a-directory/" title="Remove all access rules from a directory "></a><p>A while ago i needed to write some code that removes all (existing/inherited) access rules from a given directory. It was pretty frustrating to notice that all my attempts seemed to fail (<a href="http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.commonobjectsecurity.removeaccessrule.aspx">RemoveAccessRule</a>, <a href="http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.objectsecurity.purgeaccessrules.aspx">PurgeAccessRule</a>, &#8230;)</p>
<p>Finally i found that <a href="">SetAccessRuleProtection</a> was the method that i needed to invoke.</p>
<pre class="brush: csharp; title: ; notranslate">
const string Folder = @&quot;c:\temp\secured&quot;;
var directory = new DirectoryInfo(Folder);
var directorySecurity = directory.GetAccessControl();
directorySecurity.SetAccessRuleProtection(true,false);
directory.SetAccessControl(directorySecurity);
</pre>
<div id="attachment_2235" class="wp-caption alignnone" style="width: 387px"><a href="http://www.timvw.be/wp-content/uploads/2011/09/directorysecurity.png"><img src="http://www.timvw.be/wp-content/uploads/2011/09/directorysecurity.png" alt="Image of the security properties tab in windows" title="directorysecurity" width="377" height="461" class="size-full wp-image-2235" /></a><p class="wp-caption-text">Image of the security properties tab in windows</p></div>
<p>There you go <img src='http://www.timvw.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/09/22/remove-all-access-rules-from-a-directory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use SQL Server Profiler to see if a connection is pooled</title>
		<link>http://www.timvw.be/2011/08/19/use-sql-server-profiler-to-see-if-a-connection-is-pooled/</link>
		<comments>http://www.timvw.be/2011/08/19/use-sql-server-profiler-to-see-if-a-connection-is-pooled/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 23:13:27 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[pooling]]></category>
		<category><![CDATA[profiler]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2216</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/08/19/use-sql-server-profiler-to-see-if-a-connection-is-pooled/" title="Use SQL Server Profiler to see if a connection is pooled"></a>It took me a couple of websearches to discover how i can see in SQL Server Profiler whether or not a connection is pooled. Apparently you have to check &#8216;Show all columns&#8217; and then you can check the &#8216;EventSubClass&#8217; column: &#8230;<p class="read-more"><a href="http://www.timvw.be/2011/08/19/use-sql-server-profiler-to-see-if-a-connection-is-pooled/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/08/19/use-sql-server-profiler-to-see-if-a-connection-is-pooled/" title="Use SQL Server Profiler to see if a connection is pooled"></a><p>It took me a couple of websearches to discover how i can see in SQL Server Profiler whether or not a connection is pooled. Apparently you have to check &#8216;Show all columns&#8217; and then you can check the &#8216;EventSubClass&#8217; column:</p>
<p><a href="http://www.timvw.be/wp-content/uploads/2011/08/sql_server_profiler_eventsubclass.png"><img src="http://www.timvw.be/wp-content/uploads/2011/08/sql_server_profiler_eventsubclass.png" alt="" title="sql_server_profiler_eventsubclass" width="847" height="536" class="alignnone size-full wp-image-2217" /></a></p>
<p>This is how it looks like in your trace window:</p>
<p><a href="http://www.timvw.be/wp-content/uploads/2011/08/sql_server_profiler_eventsubclass_trace.png"><img src="http://www.timvw.be/wp-content/uploads/2011/08/sql_server_profiler_eventsubclass_trace.png" alt="" title="sql_server_profiler_eventsubclass_trace" width="769" height="407" class="alignnone size-full wp-image-2221" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/08/19/use-sql-server-profiler-to-see-if-a-connection-is-pooled/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SqlConnectionStringBuilder sets the Pooling property to true by default</title>
		<link>http://www.timvw.be/2011/08/18/sqlconnectionstringbuilder-sets-the-pooling-property-to-true-by-default/</link>
		<comments>http://www.timvw.be/2011/08/18/sqlconnectionstringbuilder-sets-the-pooling-property-to-true-by-default/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 22:57:35 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2188</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/08/18/sqlconnectionstringbuilder-sets-the-pooling-property-to-true-by-default/" title="SqlConnectionStringBuilder sets the Pooling property to true by default"></a>Here is something that surprised me: SqlConnectionStringBuilder sets the Pooling property to true by default.]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/08/18/sqlconnectionstringbuilder-sets-the-pooling-property-to-true-by-default/" title="SqlConnectionStringBuilder sets the Pooling property to true by default"></a><p>Here is something that surprised me: SqlConnectionStringBuilder sets the <a href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.pooling.aspx">Pooling</a> property to true by default.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/08/18/sqlconnectionstringbuilder-sets-the-pooling-property-to-true-by-default/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>TryGetResult</title>
		<link>http://www.timvw.be/2011/08/01/trygetresult/</link>
		<comments>http://www.timvw.be/2011/08/01/trygetresult/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 10:55:18 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2186</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/08/01/trygetresult/" title="TryGetResult"></a>I think this entry has been in the pipeline for a couple of years now and today i have decided to finally post it I got frustrated with the annoying out parameter in TryGet methods so i decided to use &#8230;<p class="read-more"><a href="http://www.timvw.be/2011/08/01/trygetresult/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/08/01/trygetresult/" title="TryGetResult"></a><p>I think this entry has been in the pipeline for a couple of years now and today i have decided to finally post it <img src='http://www.timvw.be/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I got frustrated with the annoying out parameter in TryGet methods so i decided to use a different signature using TryGetResult:</p>
<pre class="brush: csharp; title: ; notranslate">
public class TryGetResult&lt;T&gt;
{
 public TryGetResult()
 {
  Success = false;
 }

 public TryGetResult(T result)
 {
  Success = true;
  Result = result;
 }

 public bool Success { get; private set; }
 public T Result { get; private set; }
}
</pre>
<p>And now your TryGet methods can have the following signature:</p>
<pre class="brush: csharp; title: ; notranslate">
public TryGetResult&lt;Person&gt; TryGetPersonByName(string name)
{
 // person is not available
 if(name.IsInvalidPersonName()) return new TryGetResult();
 // return the person
 return new TryGetResult(new Person(name));
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/08/01/trygetresult/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Specialized solution for aggregate string concatenation</title>
		<link>http://www.timvw.be/2011/08/01/specialized-solution-for-aggregate-string-concatenation/</link>
		<comments>http://www.timvw.be/2011/08/01/specialized-solution-for-aggregate-string-concatenation/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 10:35:36 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2189</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/08/01/specialized-solution-for-aggregate-string-concatenation/" title="Specialized solution for aggregate string concatenation"></a>I have noticed that most people come up with the following solution to build a string in T-SQL: Important! Microsoft has no official documentation describing this aggregate concatenation technique that is based on the assignment SELECT syntax. The behavior described &#8230;<p class="read-more"><a href="http://www.timvw.be/2011/08/01/specialized-solution-for-aggregate-string-concatenation/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/08/01/specialized-solution-for-aggregate-string-concatenation/" title="Specialized solution for aggregate string concatenation"></a><p>I have noticed that most people come up with the following solution to build a string in T-SQL:</p>
<pre class="brush: sql; title: ; notranslate">
WITH [Numbers] AS (
	SELECT TOP(10) [n]
	FROM [Nums]
)
	SELECT @message = COALESCE(@message, '') + ', ' + CAST([n] AS nvarchar(2))
	FROM [Numbers];

SELECT @message = STUFF(@message, 1, 2, '');
SELECT @message;
</pre>
<blockquote><p>Important! Microsoft has no official documentation describing this aggregate concatenation<br />
technique that is based on the assignment SELECT syntax. The behavior described here is<br />
based on observation alone. The current implementation of the ConcatOrders function doesn’t<br />
incorporate<br />
an ORDER BY clause and does not guarantee the order of concatenation. According<br />
to a blog entry by Microsoft’s Conor Cunningham, it seems that SQL Server will respect an<br />
ORDER BY clause if specified (http://blogs.msdn.com/sqltips/archive/2005/07/20/441053.aspx).<br />
Conor is a very credible source, but I should stress that besides<br />
this blog entry I haven’t found<br />
any official documentation describing how a multi-row assignment<br />
SELECT should behave—with<br />
or without an ORDER BY clause.</p></blockquote>
<p>With the aid of FOR XML PATH (as mentionned in <a href="http://www.sql.co.il/books/insidetsql2008/">Inside Microsoft SQL Server 2008: T-SQL Programming</a> we can solve this problem using a documented approach:</p>
<pre class="brush: sql; title: ; notranslate">
WITH [Numbers] AS (
	SELECT TOP(10) [n]
	FROM [Nums]
)
	SELECT @message = (SELECT ', ' + CAST([n] AS nvarchar(2)) AS 1[/text]
	FROM [Numbers]
	FOR XML PATH(''));

SELECT @message = STUFF(@message, 1, 2, '');
SELECT @message;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/08/01/specialized-solution-for-aggregate-string-concatenation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Launch DtExec from PowerShell</title>
		<link>http://www.timvw.be/2011/07/15/launch-dtexec-from-powershell/</link>
		<comments>http://www.timvw.be/2011/07/15/launch-dtexec-from-powershell/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 17:47:01 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SSIS]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2163</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/07/15/launch-dtexec-from-powershell/" title="Launch DtExec from PowerShell"></a>Running an SSIS package from PowerShell (using DTExec) can be as simple as: Here are the functions that make it this simple:]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/07/15/launch-dtexec-from-powershell/" title="Launch DtExec from PowerShell"></a><p>Running an SSIS package from PowerShell (using DTExec) can be as simple as:</p>
<pre class="brush: powershell; title: ; notranslate">
RunPackage -File 'C:\test.dtsx' -DatabaseHost '.' -DatabaseName 'TEST';
</pre>
<p>Here are the functions that make it this simple:</p>
<pre class="brush: powershell; title: ; notranslate">
function GetDtExecPath {
    $DtsPath = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\100\DTS\Setup').SQLPath;
    $DtExecPath = (Resolve-Path &quot;$DtsPath\Binn\DTExec.exe&quot;);
    $DtExecPath;
}

function GetDtExecPropertyPathValue() {
    param(
        $PropertyPath = '',
        $Value = ''
    );
    &quot;$PropertyPath;\`&quot;`&quot;$Value\`&quot;`&quot;&quot;;
}

function RunPackage {
    param(
        $DtExecPath = (GetDtExecPath),
        $File = 'test.dtsx'
    );

    $Params = &quot;/FILE $File&quot;;
    for($i = 0; $i -lt $Args.Length; $i += 2) {
        $PropertyPath = $Args[$i].SubString(1);
        $Value = $Args[$i+1];
        $PropertyPathValue = GetDtExecPropertyPathValue -PropertyPath $PropertyPath -Value $Value;
        $Params += &quot; /SET $PropertyPathValue&quot;;
    } 

    &amp;&quot;$DtExecPath&quot; $Params;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/07/15/launch-dtexec-from-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a Nums table (quickly)</title>
		<link>http://www.timvw.be/2011/07/13/building-a-nums-table-quickly/</link>
		<comments>http://www.timvw.be/2011/07/13/building-a-nums-table-quickly/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 08:45:14 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2159</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/07/13/building-a-nums-table-quickly/" title="Building a Nums table (quickly)"></a>A while ago i presented my approach to generate a nums table here. Because we only use this code once to fill the table we don&#8217;t really care that it is not very fast. Today i discovered there is a &#8230;<p class="read-more"><a href="http://www.timvw.be/2011/07/13/building-a-nums-table-quickly/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/07/13/building-a-nums-table-quickly/" title="Building a Nums table (quickly)"></a><p>A while ago i presented my approach to generate a nums table <a href="http://www.timvw.be/2010/12/11/techniques-learned-in-sqltopia-look-ma-no-loops/">here</a>. </p>
<pre class="brush: sql; title: ; notranslate">
DECLARE @count INT = 1000;

WITH
    [Nums1] AS ( SELECT 1 AS [Value] UNION SELECT 2 AS [Value] )
  , [Nums2] AS ( SELECT A.* FROM [Nums1] AS A, [Nums1] AS B, [Nums1] AS C)
  , [Nums3] AS ( SELECT A.* FROM [Nums2] AS A, [Nums2] AS B, [Nums2] AS C)
  , [Nums4] AS ( SELECT A.* FROM [Nums3] AS A, [Nums3] AS B )
  , [Numbers] AS ( SELECT TOP(@count) -1 + ROW_NUMBER() OVER(ORDER BY [Value]) AS [Value] FROM[Nums4] )
SELECT * FROM [Numbers];
</pre>
<p>Because we only use this code once to fill the table we don&#8217;t really care that it is not very fast. Today i discovered there is a way to speed it up in this wonderful book: <a href="http://oreilly.com/catalog/9780735626034/">Inside Microsoft® SQL Server® 2008: T-SQL Querying</a>:</p>
<pre class="brush: sql; title: ; notranslate">
WITH
    [Nums1] AS ( SELECT 1 AS [Value] UNION SELECT 2 AS [Value] )
  , [Nums2] AS ( SELECT A.* FROM [Nums1] AS A, [Nums1] AS B, [Nums1] AS C)
  , [Nums3] AS ( SELECT A.* FROM [Nums2] AS A, [Nums2] AS B, [Nums2] AS C)
  , [Nums4] AS ( SELECT A.* FROM [Nums3] AS A, [Nums3] AS B )
  , [Numbers] AS ( SELECT TOP(@count) -1 + ROW_NUMBER() OVER(ORDER BY (SELECT 0)) AS [Value] FROM[Nums4] )
SELECT * FROM [Numbers];
</pre>
<p>Yay for features like ORDER BY (SELECT &lt;Constant&gt;).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/07/13/building-a-nums-table-quickly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using User-Defined Table Type with Identity column in ADO.NET</title>
		<link>http://www.timvw.be/2011/07/13/using-user-defined-table-type-with-identity-column-in-ado-net/</link>
		<comments>http://www.timvw.be/2011/07/13/using-user-defined-table-type-with-identity-column-in-ado-net/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 08:33:29 +0000</pubDate>
		<dc:creator>timvw</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ado.net]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.timvw.be/?p=2157</guid>
		<description><![CDATA[<a href="http://www.timvw.be/2011/07/13/using-user-defined-table-type-with-identity-column-in-ado-net/" title="Using User-Defined Table Type with Identity column in ADO.NET"></a>A while ago i wanted to use a User-Defined Table Type to pass in a set of records. Nothing special about this except that the first column of the UDTT was an Identity column: After finding a lot of posts &#8230;<p class="read-more"><a href="http://www.timvw.be/2011/07/13/using-user-defined-table-type-with-identity-column-in-ado-net/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.timvw.be/2011/07/13/using-user-defined-table-type-with-identity-column-in-ado-net/" title="Using User-Defined Table Type with Identity column in ADO.NET"></a><p>A while ago i wanted to use a <a href="http://msdn.microsoft.com/en-us/library/bb522526.aspx">User-Defined Table Type</a> to pass in a set of records. Nothing special about this except that the first column of the UDTT was an Identity column:</p>
<pre class="brush: sql; title: ; notranslate">
CREATE TYPE [Star].[example] AS TABLE(
  [Ordinal] [int] IDENTITY(1,1) NOT NULL,
  [Name] [nvarchar](200) NOT NULL,
)
</pre>
<p>After finding a lot of posts saying that this is not supported a colleague of mine, <a href="http://stevehorsfield.wordpress.com/">Stephen Horsfield</a>, found a way to do it as following:</p>
<pre class="brush: csharp; title: ; notranslate">
var sqlMetaData = new[]
{
  new SqlMetaData(&quot;Ordinal&quot;, SqlDbType.Int, true, false, SortOrder.Unspecified, -1),
  new SqlMetaData(&quot;Name&quot;, SqlDbType.NVarChar, 200)
};

sqlRecords = new HashSet&lt;SqlDataRecord&gt;(usersToInclude.Select(user =&gt;
{
  var record = new SqlDataRecord(sqlMetaData);
  record.SetString(1, user.Name);
  return record;
}));
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.timvw.be/2011/07/13/using-user-defined-table-type-with-identity-column-in-ado-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

