<?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/"
	>

<channel>
	<title>SQL Tidbits</title>
	<atom:link href="http://sql-tidbits.lincoln.se/feed/" rel="self" type="application/rss+xml" />
	<link>http://sql-tidbits.lincoln.se</link>
	<description>Some musing on the wonderful world of SQL, T-SQL and SQL Server</description>
	<pubDate>Fri, 16 Jan 2009 13:13:44 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Searching with optional parameters</title>
		<link>http://sql-tidbits.lincoln.se/2009/01/16/searching-with-optional-parameters/</link>
		<comments>http://sql-tidbits.lincoln.se/2009/01/16/searching-with-optional-parameters/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 13:12:43 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
		<category><![CDATA[sql]]></category>

		<category><![CDATA[tsql]]></category>

		<category><![CDATA[isnull]]></category>

		<category><![CDATA[searching]]></category>

		<guid isPermaLink="false">http://sql-tidbits.lincoln.se/?p=28</guid>
		<description><![CDATA[Sometimes we need to have optional parameters to a query. This is a very common scenario for searches, for instances. So how do you handle optional parameters? I&#8217;ve created a small script below where I query a table with two parameters, where I can use either one or two. By using ISNULL() I get a [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we need to have optional parameters to a query. This is a very common scenario for searches, for instances. So how do you handle optional parameters? I&#8217;ve created a small script below where I query a table with two parameters, where I can use either one or two. By using ISNULL() I get a clean and nice way to set the parameters if necessary. </p>
<p>Please note query #1, where both parameters are null. Then all rows are returned. </p>
<pre name="code" class="sql">

CREATE TABLE #temp (n NVARCHAR(1), f NVARCHAR(1))

INSERT INTO #temp VALUES (&#039;a&#039;, &#039;b&#039;)
INSERT INTO #temp VALUES (&#039;a&#039;, &#039;c&#039;)
INSERT INTO #temp VALUES (&#039;b&#039;, &#039;c&#039;)
INSERT INTO #temp VALUES (&#039;e&#039;, &#039;d&#039;)

DECLARE @n AS NVARCHAR(1)
DECLARE @f AS NVARCHAR(1)

SELECT * FROM #temp WHERE n = ISNULL(@n, n) AND f = ISNULL(@f, f)

SET @n = &#039;a&#039;
SELECT * FROM #temp WHERE n = ISNULL(@n, n) AND f = ISNULL(@f, f)

SET @f = &#039;c&#039;
SELECT * FROM #temp WHERE n = ISNULL(@n, n) AND f = ISNULL(@f, f)

SET @n = NULL
SELECT * FROM #temp WHERE n = ISNULL(@n, n) AND f = ISNULL(@f, f)

DROP TABLE #temp   
</pre>
<p>Results:</p>
<pre>
n    f
---- ----
a    b
a    c
b    c
e    d

n    f
---- ----
a    b
a    c

n    f
---- ----
a    c

n    f
---- ----
a    c
b    c
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sql-tidbits.lincoln.se/2009/01/16/searching-with-optional-parameters/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to keep a transaction from being escalated to the DTC</title>
		<link>http://sql-tidbits.lincoln.se/2008/12/21/how-to-keep-a-transaction-from-being-escalated-to-thedtc/</link>
		<comments>http://sql-tidbits.lincoln.se/2008/12/21/how-to-keep-a-transaction-from-being-escalated-to-thedtc/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 21:01:06 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
		<category><![CDATA[csharp]]></category>

		<category><![CDATA[sql server 2005]]></category>

		<category><![CDATA[dtc]]></category>

		<category><![CDATA[transactions]]></category>

		<category><![CDATA[wrapper]]></category>

		<guid isPermaLink="false">http://sql-tidbits.lincoln.se/?p=19</guid>
		<description><![CDATA[When working on a project at a client, I some problems with a few functions in my code that wouldn&#8217;t work when we uploaded the code to our test environment. It turned out that the functions all contained transactions which were escalated to use the DTC (Distributed Transaction Coordinator) instead of plain old transactions. This [...]]]></description>
			<content:encoded><![CDATA[<p>When working on a project at a client, I some problems with a few functions in my code that wouldn&#8217;t work when we uploaded the code to our test environment. It turned out that the functions all contained transactions which were escalated to use the DTC (Distributed Transaction Coordinator) instead of plain old transactions. This made me quite confused, because all connections and transactions were against the same database, which should keep them out of the DTC. Anyways, to fix the problem in the short term, we started the DTC in the test environment and everything worked again. But I decided to take a closer look at the problem.</p>
<p>First, our setup. We wrote c# code in .Net 3.0, using a SQL Server 2005 Standard Edition database. Some actions in our business layer needed transactions, so we used TransactionScopes, developed just for this scenario. As we were using SQL Server 2005, which supports light-weight transactions (2000 does not), we shouldn&#8217;t ever need the DTC since we were using only one data source. But apparently, this wasn&#8217;t the case. So what was happening?</p>
<p>Here&#8217;s a snippet showing our situation:</p>
<pre name="code" class="csharp">

using (TransactionScope ts = new TransactionScope())
{
    // First database connection, might happen in Business Logic -&gt; Data Access Layer or anywhere
    using (SqlConnection conn = new SqlConnection( ... ))
    {
        // ...
    }

    // Second database connection, might happen in Business Logic -&gt; Data Access Layer or anywhere. This is escalated to DTC. But why?
    using (SqlConnection conn = new SqlConnection( ... ))
    {
        // ...
    }

    ts.Complete();
}
</pre>
<p>When running this code in debug mode, reaching the second using-scope triggered an escalation to the DTC. But why? Apparently, we have two connections running at the same time, but that seems weird, since we&#8217;ve already left the using-scope so that connection should be closed in the dispose()-part of the using-scope.</p>
<p>It&#8217;s obvious when you think about it, really. When leaving the first using-scope, the connection should be closed. But that doesn&#8217;t happen, since we are in a transaction. The transaction scope needs to keep the connection open when we either commit or rollback. So when we reach the second using-scope, a second connection is opened and we are escalated to the DTC. </p>
<p>So, what can we do about this? There are at least two solutions:</p>
<ul>
<li>We overload the TransactionScope class with an <a href="http://69.10.233.10/KB/database/TsFix.aspx" target="_blank">implementation that re-uses the connection</a> if a connection is already in use.</li>
<li>Instead of calling <code>new SqlConnection()...</code> you can call a <a href="http://blogs.msdn.com/dataaccess/attachment/532026.ashx" target="_blank">wrapper which checks if there already is one connection running</a> and reuses that one.</li>
</ul>
<p>If your are using SQL Server 2008 and Visual Studio 2008, this is implemented from the beginning and you don&#8217;t need any wrappers or overloads. </p>
<p>Recommended reading:</p>
<ul>
<li><a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3083270&#038;SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3083270&#038;SiteID=1</a></li>
<li><a href="http://blogs.msdn.com/adonet/archive/2008/03/26/extending-lightweight-transactions-in-sqlclient.aspx">http://blogs.msdn.com/adonet/archive/2008/03/26/extending-lightweight-transactions-in-sqlclient.aspx</a></li>
<li><a href="http://blogs.msdn.com/dataaccess/archive/2006/02/14/532026.aspx ">http://blogs.msdn.com/dataaccess/archive/2006/02/14/532026.aspx</a> </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sql-tidbits.lincoln.se/2008/12/21/how-to-keep-a-transaction-from-being-escalated-to-thedtc/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SP3 out now</title>
		<link>http://sql-tidbits.lincoln.se/2008/12/19/sp3-out-now/</link>
		<comments>http://sql-tidbits.lincoln.se/2008/12/19/sp3-out-now/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 09:25:22 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
		<category><![CDATA[sql server]]></category>

		<category><![CDATA[sql server 2005]]></category>

		<category><![CDATA[bug fix]]></category>

		<category><![CDATA[patch]]></category>

		<category><![CDATA[sp3]]></category>

		<guid isPermaLink="false">http://sql-tidbits.lincoln.se/?p=15</guid>
		<description><![CDATA[SP3 for SQL Server has been released! Microsoft has a list of what&#8217;s new and a list of the bug fixes. Go on and download SP3 now!
]]></description>
			<content:encoded><![CDATA[<p>SP3 for SQL Server has been released! Microsoft has a <a href="http://msdn.microsoft.com/en-us/library/dd353312(SQL.90).aspx" target="_blank">list of what&#8217;s new</a> and a <a href="http://support.microsoft.com/?kbid=955706" target="_blank">list of the bug fixes</a>. Go on and <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=ae7387c3-348c-4faa-8ae5-949fdfbe59c4&amp;displaylang=en" target="_blank">download SP3</a> now!</p>
]]></content:encoded>
			<wfw:commentRss>http://sql-tidbits.lincoln.se/2008/12/19/sp3-out-now/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The perils of not specifying a variable or parameter length</title>
		<link>http://sql-tidbits.lincoln.se/2008/12/12/the-perils-of-not-specifying-a-variable-or-parameter-length/</link>
		<comments>http://sql-tidbits.lincoln.se/2008/12/12/the-perils-of-not-specifying-a-variable-or-parameter-length/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 10:30:10 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
		<category><![CDATA[sql]]></category>

		<category><![CDATA[sql server]]></category>

		<category><![CDATA[sql server 2005]]></category>

		<category><![CDATA[sql server 2008]]></category>

		<category><![CDATA[tsql]]></category>

		<category><![CDATA[data types]]></category>

		<category><![CDATA[declare]]></category>

		<category><![CDATA[length]]></category>

		<category><![CDATA[nvarchar]]></category>

		<category><![CDATA[varchar]]></category>

		<guid isPermaLink="false">http://sql-tidbits.lincoln.se/?p=8</guid>
		<description><![CDATA[When declaring a variable or parameter as varchar or nvarchar, you might get into some nasty surprises if you don&#8217;t specify the number of characters in the data. If you are declaring a variable like DECLARE @string AS VARCHAR, you are going to end up with a string that only holds 1 character. That&#8217;s probably [...]]]></description>
			<content:encoded><![CDATA[<p>When declaring a variable or parameter as varchar or nvarchar, you might get into some nasty surprises if you don&#8217;t specify the number of characters in the data. If you are declaring a variable like DECLARE @string AS VARCHAR, you are going to end up with a string that only holds 1 character. That&#8217;s probably not what you intended, but rather DECLARE @string AS VARCHAR(50) or similar.</p>
<p>The behaviour is similar when using CAST and CONVERT. Then, the default is to limit the data to 30 characters.</p>
<p>Some examples of this:</p>
<pre><code style="font-size: 12px;"><span style="color: blue;">DECLARE </span><span style="color: #434343;">@string </span><span style="color: blue;">AS VARCHAR
SET </span><span style="color: #434343;">@string </span><span style="color: blue;">= </span><span style="color: red;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit'

</span><span style="color: blue;">SELECT
</span><span style="color: red;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit' </span><span style="color: blue;">AS </span><span style="color: black;">[Original string]</span><span style="color: gray;">,
</span><span style="color: magenta;">LEN</span><span style="color: gray;">(</span><span style="color: red;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit'</span><span style="color: gray;">) </span><span style="color: blue;">AS </span><span style="color: black;">[Length of original string]</span><span style="color: gray;">,
</span><span style="color: #434343;">@string </span><span style="color: blue;">AS </span><span style="color: black;">[varchar]</span><span style="color: gray;">,
</span><span style="color: magenta;">CAST</span><span style="color: gray;">(</span><span style="color: red;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit' </span><span style="color: blue;">AS VARCHAR</span><span style="color: gray;">) </span><span style="color: blue;">AS </span><span style="color: black;">[Casted varchar]</span><span style="color: gray;">,
</span><span style="color: magenta;">LEN</span><span style="color: gray;">(</span><span style="color: magenta;">CAST</span><span style="color: gray;">(</span><span style="color: red;">'Lorem ipsum dolor sit amet, consectetur adipisicing elit' </span><span style="color: blue;">AS VARCHAR</span><span style="color: gray;">)) </span><span style="color: blue;">AS </span><span style="color: black;">[Length of casted varchar]
GO

</span><span style="color: blue;">DECLARE </span><span style="color: #434343;">@string </span><span style="color: blue;">AS NVARCHAR
SET </span><span style="color: #434343;">@string </span><span style="color: blue;">= </span><span style="color: red;">N'Lorem ipsum dolor sit amet, consectetur adipisicing elit'

</span><span style="color: blue;">SELECT
</span><span style="color: red;">N'Lorem ipsum dolor sit amet, consectetur adipisicing elit' </span><span style="color: blue;">AS </span><span style="color: black;">[Original string]</span><span style="color: gray;">,
</span><span style="color: magenta;">LEN</span><span style="color: gray;">(</span><span style="color: red;">N'Lorem ipsum dolor sit amet, consectetur adipisicing elit'</span><span style="color: gray;">) </span><span style="color: blue;">AS </span><span style="color: black;">[Length of original string]</span><span style="color: gray;">,
</span><span style="color: #434343;">@string </span><span style="color: blue;">AS </span><span style="color: black;">[nvarchar]</span><span style="color: gray;">,
</span><span style="color: magenta;">CAST</span><span style="color: gray;">(</span><span style="color: red;">N'Lorem ipsum dolor sit amet, consectetur adipisicing elit' </span><span style="color: blue;">AS NVARCHAR</span><span style="color: gray;">) </span><span style="color: blue;">AS </span><span style="color: black;">[Casted nvarchar]</span><span style="color: gray;">,
</span><span style="color: magenta;">LEN</span><span style="color: gray;">(</span><span style="color: magenta;">CAST</span><span style="color: gray;">(</span><span style="color: red;">N'Lorem ipsum dolor sit amet, consectetur adipisicing elit' </span><span style="color: blue;">AS NVARCHAR</span><span style="color: gray;">)) </span><span style="color: blue;">AS </span><span style="color: black;">[Length of casted nvarchar]</span></code></pre>
<p>Results:</p>
<pre>Original string                                          Length of original string varchar Casted varchar                 Length of casted varchar
-------------------------------------------------------- ------------------------- ------- ------------------------------ ------------------------
Lorem ipsum dolor sit amet, consectetur adipisicing elit 56                        L       Lorem ipsum dolor sit amet, co 30

(1 row(s) affected)

Original string                                          Length of original string nvarchar Casted nvarchar                Length of casted nvarchar
-------------------------------------------------------- ------------------------- -------- ------------------------------ -------------------------
Lorem ipsum dolor sit amet, consectetur adipisicing elit 56                        L        Lorem ipsum dolor sit amet, co 30

(1 row(s) affected)</pre>
]]></content:encoded>
			<wfw:commentRss>http://sql-tidbits.lincoln.se/2008/12/12/the-perils-of-not-specifying-a-variable-or-parameter-length/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Introduction to SQL Tidbits</title>
		<link>http://sql-tidbits.lincoln.se/2008/12/10/introduction-to-sql-tidbits/</link>
		<comments>http://sql-tidbits.lincoln.se/2008/12/10/introduction-to-sql-tidbits/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 14:19:09 +0000</pubDate>
		<dc:creator>Jonas</dc:creator>
		
		<category><![CDATA[about]]></category>

		<guid isPermaLink="false">http://sql-tidbits.lincoln.se/?p=6</guid>
		<description><![CDATA[
Hello everyone!
SQL Tidbits is a place for me to ramble on about SQL and post tips on how to do weird or complicated stuff in SQL, T-SQL and SQL Server.
My name is Jonas Lincoln, I am currently employed at Netlight AS in Oslo, Norway. I’m usually located in Stockholm, Sweden, but I’m doing a wonderful [...]]]></description>
			<content:encoded><![CDATA[<div class="entry-page">
<p>Hello everyone!</p>
<p>SQL Tidbits is a place for me to ramble on about SQL and post tips on how to do weird or complicated stuff in SQL, T-SQL and SQL Server.</p>
<p>My name is Jonas Lincoln, I am currently employed at <a title="Netlight AS" href="http://www.netlight.no/" target="_blank">Netlight AS</a> in Oslo, Norway. I’m usually located in Stockholm, Sweden, but I’m doing a wonderful year in Oslo starting up our business here.</p>
<p>I started using SQL Server 2000 back in 2002, while working on a large online game called <a href="http://www.hattrick.org/" target="_blank">Hattrick</a>, one of the largest games in the world actually. As time passed, I got more and more involved and since we were on a tight budget, we had to do some really cool optimizations do get everything running. And that was when my interested in SQL started for real.</p>
<p>Now, I’m a senior consultant, mostly coding in C# and using the .Net framework.</p>
<p>If you wish to contact me, check out my <a href="http://jonas.lincoln.se/" target="_self">personal page</a>.</div>
]]></content:encoded>
			<wfw:commentRss>http://sql-tidbits.lincoln.se/2008/12/10/introduction-to-sql-tidbits/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
