<?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; T-SQL</title>
	<atom:link href="http://www.aaronlowe.net/archive/category/sql-server/t-sql/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>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>
		<item>
		<title>Did you know? SQL Server 2008 includes Transact-SQL Debugger</title>
		<link>http://www.aaronlowe.net/archive/2008/06/did-you-know-sql-server-2008-includes-transact-sql-debugger/</link>
		<comments>http://www.aaronlowe.net/archive/2008/06/did-you-know-sql-server-2008-includes-transact-sql-debugger/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 01:39:35 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[2008]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">/post/2008/06/14/Did-you-know-SQL-Server-2008-includes-Transact-SQL-Debugger.aspx</guid>
		<description><![CDATA[The Transact-SQL debugger in SQL Server Management Studio enables you to find errors in Transact-SQL scripts, stored procedures, triggers, and functions by observing their run-time behavior. You can start the debugger when you are using the Database Engine Query Editor window. By using the Transact-SQL debugger, you can do the following: Step through the Transact-SQL [...]]]></description>
			<content:encoded><![CDATA[<p><span class="sbmLink"></span>
<p><i>The Transact-SQL debugger in SQL Server Management Studio enables you to find errors in Transact-SQL scripts, stored procedures, triggers, and functions by observing their run-time behavior. You can start the debugger when you are using the Database Engine Query Editor window. </i></p>
<p><i></i></p>
<p><i>By using the Transact-SQL debugger, you can do the following:</i> </p>
<p><i></i></p>
<ul>
<li><i>Step through the Transact-SQL statements in the editor line by line, or set breakpoints to stop at specific lines.</i> </li>
<li><i>Step into or over Transact-SQL stored procedures, functions, or triggers that are run by the code in the editor window.</i> </li>
<li><i>Watch the values that are assigned to variables, and observe system objects such as the call stack and threads.&#160; </i></li>
</ul>
<p>Check out a screencast about the new T-SQL Debugger in SQL Server 2008 via MSN Video below, or if you prefer <a href="http://www.youtube.com/watch?v=618LE_FZCxI" target="_blank">Youtube</a> or <a href="http://cid-a1aa2222a8b0305c.skydrive.live.com/self.aspx/Public/Screencasts/ss2008%7C_dyn14%7C_t-sql%7C_debugger.wmv" target="_blank">download</a>.</p>
<div class="wlWriterSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:1291f541-7874-4ebb-a34e-3b8cd586b76f" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div><embed pluginspage="http://macromedia.com/go/getflashplayer" src="http://images.video.msn.com/flash/soapbox1_1.swf" width="432" height="364" type="application/x-shockwave-flash" flashvars="c=v&amp;v=b4af876b-d796-412d-b9f2-dad3f9845195&amp;from=writer" wmode="transparent" quality="high" /></div>
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2008/06/did-you-know-sql-server-2008-includes-transact-sql-debugger/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Did you know? SQL Server 2008 includes Merge Statement</title>
		<link>http://www.aaronlowe.net/archive/2008/04/did-you-know-sql-server-2008-includes-merge-statement/</link>
		<comments>http://www.aaronlowe.net/archive/2008/04/did-you-know-sql-server-2008-includes-merge-statement/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 00:51:24 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[2008]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">/post/2008/04/09/Did-you-know-SQL-Server-2008-includes-Merge-Statement.aspx</guid>
		<description><![CDATA[Common data scenarios require developers to write and maintain logic to determine whether to insert, update or delete information (sometimes called upsert) such as: 1: IF FOUND 2: THEN UPDATE 3: ELSE 4: INSERT SQL Server 2008 provides the new MERGE statement within the SQL language to enable developers to provide this functionality with a [...]]]></description>
			<content:encoded><![CDATA[<p><span class="sbmLink"></span>
<p>Common data scenarios require developers to write and maintain logic to determine whether to insert, update or delete information (sometimes called upsert) such as: </p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #0000ff">IF</span> <span style="color: #0000ff">FOUND</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span>     <span style="color: #0000ff">THEN</span> <span style="color: #0000ff">UPDATE</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span> <span style="color: #0000ff">ELSE</span> </pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   4:</span>     INSERT </pre>
</p></div>
</div>
<p>SQL Server 2008 provides the new MERGE statement within the SQL language to enable developers to provide this functionality with a single statement. This allows for code reduction and easier maintainability. </p>
<p>Check out a screencast about the new MERGE Statement in SQL Server 2008 via MSN Soapbox below or <a href="http://www.youtube.com/watch?v=aSalPkVNQKU">youtube</a> or download <a href="http://cid-a1aa2222a8b0305c.skydrive.live.com/self.aspx/Public/Screencasts/ss2008_dyn05_merge.wmv" target="_blank">here</a>. </p>
<p>Read more about this in the SQL Server 2008 BOL &#8216;<a href="http://msdn2.microsoft.com/en-us/library/bb522522(SQL.100).aspx" target="_blank">Inserting, Updating, and Deleting Data with MERGE&#8217;.</a></p>
</p>
<div class="wlWriterSmartContent" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:8a298f8c-f84c-40db-80a4-9c7b9b2098b0" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div><embed pluginspage="http://macromedia.com/go/getflashplayer" src="http://images.video.msn.com/flash/soapbox1_1.swf" width="432" height="364" type="application/x-shockwave-flash" flashvars="c=v&amp;v=be484979-face-439e-8689-9bebae05d8cb&amp;from=writer" wmode="transparent" quality="high" /></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2008/04/did-you-know-sql-server-2008-includes-merge-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugger returns in Management Studio</title>
		<link>http://www.aaronlowe.net/archive/2008/03/debugger-returns-in-management-studio/</link>
		<comments>http://www.aaronlowe.net/archive/2008/03/debugger-returns-in-management-studio/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 02:18:59 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[2008]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">/post/2008/03/01/Debugger-returns-in-Management-Studio.aspx</guid>
		<description><![CDATA[Finally we get a T-SQL debugger back into SQL Server Management tools. Evidently it was announced at the Launch Event in LA that Microsoft has decided to put the T-SQL Debugger into SQL Server Management Studio.&#160; This is something that was in Query Analyzer for SQL 2000 but we haven&#8217;t had it for 2005 up [...]]]></description>
			<content:encoded><![CDATA[<p>Finally we get a T-SQL debugger back into SQL Server Management tools. Evidently it was announced at the Launch Event in LA that Microsoft has decided to put the T-SQL Debugger into SQL Server Management Studio.&#160; This is something that was in Query Analyzer for SQL 2000 but we haven&#8217;t had it for 2005 up until now.&#160; See <a href="http://blogs.msdn.com/dtjones/archive/2008/02/28/sql-server-2008-launch-the-day-after.aspx">here</a> for more information.&#160; However here&#8217;s the pertinent excerpt:</p>
<p>&lt;excerpt&gt; </p>
<p>So what are the surprise features? </p>
<p>#1: we&#8217;ve rewritten the activity monitor to be far more useful and performant (funny word) than ever before. This was work Ken Henderson started before he passed away. One of Ken&#8217;s good friends and a dev on the team, Bart Duncan, took over the work to complete it in Ken&#8217;s honor. I showed this at launch. </p>
<p>#2: object search: yes, we&#8217;re bringing back object search. In Object Explorer Details we&#8217;ve added a search bar that will search for database objects. It&#8217;s a simple yet powerful search. Unfortunately I wasn&#8217;t able to demo it at launch. </p>
<p>Drumroll please&#8230; </p>
<p>#3: the debugger: we&#8217;ve brought back the T-SQL debugger in Management Studio. This is huge and incredibly valuable. And yes, I demoed it at the launch event. </p>
<p>BTW: #3 had the highest number of votes in Connect and #2 was close behind. We did these because customers told us they are important feature to them. While Connect does have its flaws, it also has its good things. Keep the feedback coming! </p>
<p>&lt;/excerpt&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2008/03/debugger-returns-in-management-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Move HEAP Tables</title>
		<link>http://www.aaronlowe.net/archive/2007/09/move-heap-tables/</link>
		<comments>http://www.aaronlowe.net/archive/2007/09/move-heap-tables/#comments</comments>
		<pubDate>Wed, 12 Sep 2007 01:47:50 +0000</pubDate>
		<dc:creator>Aaron Lowe</dc:creator>
				<category><![CDATA[2005]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">/post/2007/09/12/Move-HEAP-Tables.aspx</guid>
		<description><![CDATA[One of the things I&#8217;ve been doing lately for an upgrade from SQL 2000 to SQL 2005 is restructuring databases, adding filegroups and files to split out the data and non clustered indexes, leaving only the system tables in the primary filegroup.&#160; This is actually fairly simple as we&#8217;re just dropping the clustered indexes and [...]]]></description>
			<content:encoded><![CDATA[<p><span></span>
<p>One of the things I&#8217;ve been doing lately for an upgrade from SQL 2000 to SQL 2005 is restructuring databases, adding filegroups and files to split out the data and non clustered indexes, leaving only the system tables in the primary filegroup.&#160; </p>
<p>This is actually fairly simple as we&#8217;re just dropping the clustered indexes and recreating them in the new filegroup which moves the data.&#160; However we have some tables that don&#8217;t have clustered indexes (i.e., HEAP tables) so I wrote a script that moves the HEAP tables into another filegroup.&#160; First it checks for an identity column and if one exists, I create a clustered index on the identity column for the new filegroup, then remove the clustered index.&#160; If an identity column doesn&#8217;t exist, I create one with a clustered index on the new filegroup then drop the index and the column.&#160; Fairly simple. </p>
<p>On another note if anyone knows an easy way on how to move text, ntext data between to a different filegroup, let me know.</p>
<pre><span>-- Aaron Lowe</span>
<span>-- 8/24/2007</span>

<span>SET</span> NOCOUNT <span>ON</span>
<span>Print</span> <span>'Starting'</span>
<span>DECLARE</span> @SchemaName nvarchar(128),
        @TableName nvarchar(128),
        @SQLcmd nvarchar (1024),
        @ColumnName nvarchar(128),
        @FullQualTable nvarchar(261),
        @FileGroup nvarchar(128)

<span>SELECT</span> @FileGroup = DB_NAME() + <span>'_DATA'</span>

<span>DECLARE</span> HeapTables_CRSR <span>Cursor</span> <span>FOR</span>
    <span>select</span> ss.name, so.name
    <span>from</span>
        sys.indexes si
        <span>join</span> sys.objects so <span>on</span> si.object_id = so.object_id
        <span>join</span> sys.schemas ss <span>on</span> so.schema_id = ss.schema_id
        <span>join</span> sys.database_files sdf <span>on</span> sdf.data_space_id = si.data_space_id
        <span>join</span> sys.filegroups sfg <span>on</span> sfg.data_space_id = sdf.data_space_id
    <span>where</span>
        si.type = 0
        <span>and</span> so.type = <span>'U'</span>
        <span>and</span> sfg.name = <span>'PRIMARY'</span>
    <span>order</span> <span>by</span>
        so.name

<span>OPEN</span> HeapTables_CRSR

<span>FETCH</span> <span>NEXT</span> <span>FROM</span> HeapTables_CRSR <span>INTO</span> @SchemaName, @TableName
<span>WHILE</span> <span>@@FETCH_STATUS</span> = 0
    <span>BEGIN</span>
        <span>SET</span> @FullQualTable = <span>'['</span> + @SchemaName + <span>'].['</span> + @TableName + <span>']'</span>
        <span>Print</span> <span>'Currently working on: '</span> + @FullQualTable
        <span>IF</span> <span>EXISTS</span> (<span>SELECT</span> * <span>FROM</span> sys.columns <span>WHERE</span> OBJECT_ID = OBJECT_ID(@FullQualTable)
             <span>and</span> is_identity = 1)
            <span>BEGIN</span>
                <span>SELECT</span> @ColumnName = name <span>from</span> sys.columns <span>WHERE</span> OBJECT_ID =
                    OBJECT_ID(@FullQualTable) <span>and</span> is_identity = 1
                <span>SET</span> @SQLCmd = N<span>'CREATE CLUSTERED INDEX [IX_TempIDent] ON
                ['</span> + @SchemaName + <span>'].['</span> + @TableName + <span>'] (['</span> +
                @ColumnName + <span>'] ASC) ON ['</span> + @FileGroup + <span>'];'</span>
                <span>exec</span> sp_executesql @SQLCmd
                <span>SET</span> @SQLCmd = N<span>'DROP INDEX [IX_TempIDent] ON ['</span> + @SchemaName + <span>'].['</span> +
                @TableName + <span>'];'</span>
                <span>exec</span> sp_executesql @SQLCmd
            <span>END</span>
        <span>ELSE</span>
            <span>BEGIN</span>
                <span>SET</span> @SQLCmd = N<span>'ALTER TABLE ['</span> + @SchemaName + <span>'].['</span> + @TableName
                + <span>'] ADD [TempIdent] int IDENTITY (1,1);'</span>
                <span>exec</span> sp_executesql @SQLCmd
                <span>SET</span> @SQLCmd = N<span>'CREATE CLUSTERED INDEX [IX_TempIDent] ON ['</span> +
                @SchemaName + <span>'].['</span> + @TableName +
                <span>'] ([TempIdent] ASC) ON ['</span> + @FileGroup + <span>'];'</span>
                <span>exec</span> sp_executesql @SQLCmd
                <span>SET</span> @SQLCmd = N<span>'DROP INDEX [IX_TempIDent] ON ['</span> + @SchemaName + <span>'].['</span> +
                @TableName + <span>'];'</span>
                <span>exec</span> sp_executesql @SQLCmd
                <span>SET</span> @SQLCmd = N<span>'ALTER TABLE ['</span> + @SchemaName + <span>'].['</span> + @TableName +
                <span>'] DROP COLUMN [TempIdent];'</span>
                <span>exec</span> sp_executesql @SQLCmd
            <span>END</span>
        <span>FETCH</span> <span>NEXT</span> <span>FROM</span> HeapTables_CRSR <span>INTO</span> @SchemaName, @TableName
    <span>END</span>
<span>CLOSE</span> HeapTables_CRSR
<span>DEALLOCATE</span> HeapTables_CRSR
<span>Print</span> <span>'Done'</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.aaronlowe.net/archive/2007/09/move-heap-tables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
