<?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>SQL Fool &#187; Miscellaneous</title>
	<atom:link href="http://sqlfool.com/category/miscellaneous/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com</link>
	<description>Self-Professed SQL Scripting Junkie!</description>
	<lastBuildDate>Wed, 02 May 2012 21:25:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Tidbits I Discovered Today&#8230;</title>
		<link>http://sqlfool.com/2012/05/things-i-discovered-today/</link>
		<comments>http://sqlfool.com/2012/05/things-i-discovered-today/#comments</comments>
		<pubDate>Wed, 02 May 2012 21:23:56 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1624</guid>
		<description><![CDATA[I&#8217;ve figured out a couple of tidbits today that I wanted to share. First and foremost, I&#8217;ve discovered a (new to me) option in SSMS to convert tabs to spaces. I had previously seen the &#8220;Untabify Selected Lines&#8221; option (under Edit &#8211;> Advanced), but this only seemed to remove the tabs at the beginning of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve figured out a couple of tidbits today that I wanted to share. First and foremost, I&#8217;ve discovered a (new to me) option in SSMS to convert tabs to spaces. I had previously seen the &#8220;Untabify Selected Lines&#8221; option (under Edit &#8211;> Advanced), but this only seemed to remove the tabs at the beginning of the line; it would not remove in-line tabs. I&#8217;ve now found another option that will remove the tabs throughout the selected code. </p>
<p>Here&#8217;s how you can do it:</p>
<p><strong>Disclaimer: I&#8217;m using SSMS 2008 and have not confirmed this in other versions of SSMS.</strong></p>
<p>From your toolbar, right-click in the empty gray space, or click on the drop-down arrow. Navigate to Customize:</p>
<p><a href="http://sqlfool.com/wp-content/uploads/2012/05/ssms1.jpg" target="_blank"><img src="http://sqlfool.com/wp-content/uploads/2012/05/ssms1-300x98.jpg" alt="" title="ssms1" width="300" height="98" /></a></p>
<p>Click on the Commands tab, then navigate down to Edit &#8211;> Convert Tabs to Spaces.<br />
<strong>Note:</strong> if you&#8217;re one of those weirdos who like tabs instead of spaces, you can convert in the other (wrong) direction, too. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><a href="http://sqlfool.com/wp-content/uploads/2012/05/ssms2.jpg" target="_blank"><img src="http://sqlfool.com/wp-content/uploads/2012/05/ssms2-300x232.jpg" alt="" title="ssms2" width="300" height="232"/></a></p>
<p>Once you have found the option you want, click on it and drag it to your toolbar.</p>
<p><a href="http://sqlfool.com/wp-content/uploads/2012/05/ssms3.jpg" target="_blank"><img src="http://sqlfool.com/wp-content/uploads/2012/05/ssms3-300x45.jpg" alt="" title="ssms3" width="300" height="45"/></a><br />
Cool, huh? </p>
<p>Another little thing I ran into today was a mismatch between nullable and non-nullable columns in related tables. I started to perform a manual audit before I said, &#8220;There has to be a faster way.&#8221; And you know what? There was. Here&#8217;s the little script I used:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> c1.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>columnName<span style="color: #808080;">&#93;</span>
    , c1.<span style="color: #202020;">is_nullable</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>FactInternetSales_isNullable<span style="color: #808080;">&#93;</span>
    , c2.<span style="color: #202020;">is_nullable</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>FactResellerSales_isNullable<span style="color: #808080;">&#93;</span>
<span style="color: #008080;">/* replace AdventureWorksDW2008R2 with your database */</span>
<span style="color: #0000FF;">FROM</span> AdventureWorksDW2008R2.<span style="color: #202020;">sys</span>.<span style="color: #202020;">columns</span> <span style="color: #0000FF;">AS</span> c1 
<span style="color: #808080;">JOIN</span> AdventureWorksDW2008R2.<span style="color: #202020;">sys</span>.<span style="color: #202020;">columns</span> <span style="color: #0000FF;">AS</span> c2
    <span style="color: #0000FF;">ON</span> c1.<span style="color: #202020;">name</span> <span style="color: #808080;">=</span> c2.<span style="color: #202020;">name</span>
<span style="color: #0000FF;">WHERE</span> c1.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'dbo.FactInternetSales'</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- replace with your table</span>
    <span style="color: #808080;">AND</span> c2.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'dbo.FactResellerSales'</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- replace with your table</span>
    <span style="color: #808080;">AND</span> c1.<span style="color: #202020;">is_nullable</span> <span style="color: #808080;">&lt;&gt;</span> c2.<span style="color: #202020;">is_nullable</span>; <span style="color: #008080;">-- find discrepancies</span></pre></div></div>

<p>This returned the following results:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">columnName             FactInternetSales_isNullable FactResellerSales_isNullable
---------------------- ---------------------------- ----------------------------
RevisionNumber         0                            1
OrderQuantity          0                            1
UnitPrice              0                            1
ExtendedAmount         0                            1
UnitPriceDiscountPct   0                            1
DiscountAmount         0                            1
ProductStandardCost    0                            1
TotalProductCost       0                            1
SalesAmount            0                            1
TaxAmt                 0                            1
Freight                0                            1
&nbsp;
(11 row(s) affected)</pre></div></div>

<p>This script isn&#8217;t as polished as my usual, but it&#8217;s really just a quick-and-dirty way of checking for mismatches in NULL properties. </p>
<p>Okay, that&#8217;s all I have for now, my SQL friends. Oh, and if you know any other cool little tidbits, please feel free to share with me. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/05/things-i-discovered-today/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>BCP Script Generator</title>
		<link>http://sqlfool.com/2012/04/bcp-script-generator/</link>
		<comments>http://sqlfool.com/2012/04/bcp-script-generator/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 21:36:13 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[bcp]]></category>
		<category><![CDATA[bulk copy program]]></category>
		<category><![CDATA[data transfer]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1598</guid>
		<description><![CDATA[I&#8217;m currently working on the logic migration of data marts from SQL Server to Teradata. While another team is working on the actual table migration, it&#8217;s still helpful to have some data in the tables for developing against. The easiest method I&#8217;ve found to do this is to use BCP to export some sample data. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on the logic migration of data marts from SQL Server to Teradata. While another team is working on the actual table migration, it&#8217;s still helpful to have some data in the tables for developing against. The easiest method I&#8217;ve found to do this is to <a href="http://sqlfool.com/2008/12/bcp-basics/" target="_blank">use BCP</a> to export some sample data. So of course, I&#8217;ve created a SQL script that will generate the BCP code for me. Because that&#8217;s what I like to do on Sunday evenings.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- User-defined variables --</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @tableToBCP <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   <span style="color: #808080;">=</span> <span style="color: #FF0000;">'AdventureWorksDW2008R2.dbo.DimCustomer'</span>
    , @<span style="color: #0000FF;">Top</span>          <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>     <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span> <span style="color: #008080;">-- Leave NULL for all rows</span>
    , @Delimiter    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>      <span style="color: #808080;">=</span> <span style="color: #FF0000;">'|'</span>
    , @Use<span style="color: #808080;">NULL</span>      <span style="color: #0000FF;">BIT</span>             <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    , @OverrideChar <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>         <span style="color: #808080;">=</span> <span style="color: #FF0000;">'~'</span>
    , @MaxDop       <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>         <span style="color: #808080;">=</span> <span style="color: #FF0000;">'1'</span>
    , @Directory    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>    <span style="color: #808080;">=</span> <span style="color: #FF0000;">'C:\bcp_output\'</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">-- Script-defined variables -- </span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @columnList <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span>columnID <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @bcpStatement <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BCP &quot;SELECT '</span>
    , @currentID <span style="color: #0000FF;">INT</span>
    , @firstID <span style="color: #0000FF;">INT</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @columnList
<span style="color: #0000FF;">SELECT</span> column_id 
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> 
<span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>@tableToBCP<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> column_id;
&nbsp;
<span style="color: #0000FF;">IF</span> @<span style="color: #0000FF;">Top</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">'TOP ('</span> <span style="color: #808080;">+</span> @<span style="color: #0000FF;">Top</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">') '</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @firstID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>columnID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> @columnList;
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> @columnList<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @currentID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>columnID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> @columnList;
&nbsp;
    <span style="color: #0000FF;">IF</span> @currentID <span style="color: #808080;">&lt;&gt;</span> @firstID
        <span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">','</span>;
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> name
    <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> 
    <span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>@tableToBCP<span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">AND</span> column_id <span style="color: #808080;">=</span> @currentID;
&nbsp;
    <span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @columnList <span style="color: #0000FF;">WHERE</span> columnID <span style="color: #808080;">=</span> @currentID;
&nbsp;
&nbsp;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">' FROM '</span> <span style="color: #808080;">+</span> @tableToBCP 
    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' WITH (NOLOCK) OPTION (MAXDOP 1);&quot; queryOut '</span>
    <span style="color: #808080;">+</span> @Directory <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span>@tableToBCP, <span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'_'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.dat -S'</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">@@SERVERNAME</span>
    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' -T -t&quot;'</span> <span style="color: #808080;">+</span> @Delimiter <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&quot; -c -C;'</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> @bcpStatement;</pre></div></div>

<p>This will generate a standard BCP script:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">BCP &quot;SELECT CustomerKey,GeographyKey,CustomerAlternateKey,Title,FirstName,MiddleName,
LastName,NameStyle,BirthDate,MaritalStatus,Suffix,Gender,EmailAddress,YearlyIncome,
TotalChildren,NumberChildrenAtHome,EnglishEducation,SpanishEducation,FrenchEducation,
EnglishOccupation,SpanishOccupation,FrenchOccupation,HouseOwnerFlag,NumberCarsOwned,
AddressLine1,AddressLine2,Phone,DateFirstPurchase,CommuteDistance 
FROM AdventureWorksDW2008R2.dbo.DimCustomer WITH (NOLOCK) OPTION (MAXDOP 1);&quot; queryOut 
C:\bcp_output\AdventureWorksDW2008R2_dbo_DimCustomer.dat -SSQLFOOL -T -t&quot;|&quot; -c -C;</pre></div></div>

<p>However, I&#8217;ve been running into some issues with the data load. See, the thing&#8230; I&#8217;m finding that some of my tables don&#8217;t&#8230; that is to say&#8230; they may possibly contain&#8230; [whisper]<em>uncleansed data</em>[/whisper]. I know, I know&#8230; this may come as a shock to many of you, and all I ask is that you please don&#8217;t judge me for it. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>What do I mean by &#8220;uncleansed data?&#8221; I mostly mean user-inputted VARCHAR columns that contain pipes (|), tabs, carriage returns, and line feeds. These types of characters tend to mess with the data import process. Also, I&#8217;ve not yet found a way to import a data file into Teradata where a non-nullable character column contains an empty string (&#8221;). Obviously, the vast majority of the data is fine, but even one of these issues can throw an error during the import process. I&#8217;ve modified the script above to handle these specific exceptions.</p>
<p>Since I&#8217;m only using this data for testing purposes, I found it pretty easy to simply replace the offending records with ~. I&#8217;m not sure if anyone else has a need for this particular script, but I figured you could modify it pretty easily to do whatever you need.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- User-defined variables --</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @tableToBCP <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>   <span style="color: #808080;">=</span> <span style="color: #FF0000;">'AdventureWorksDW2008R2.dbo.DimCustomer'</span>
    , @<span style="color: #0000FF;">Top</span>          <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>     <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span> <span style="color: #008080;">-- Leave NULL for all rows</span>
    , @Delimiter    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span>      <span style="color: #808080;">=</span> <span style="color: #FF0000;">'|'</span>
    , @Use<span style="color: #808080;">NULL</span>      <span style="color: #0000FF;">BIT</span>             <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    , @OverrideChar <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>         <span style="color: #808080;">=</span> <span style="color: #FF0000;">'~'</span>
    , @MaxDop       <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>         <span style="color: #808080;">=</span> <span style="color: #FF0000;">'1'</span>
    , @Directory    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>    <span style="color: #808080;">=</span> <span style="color: #FF0000;">'C:\bcp_output\'</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">-- Script-defined variables -- </span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @columnList <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span>columnID <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @bcpStatement <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BCP &quot;SELECT '</span>
    , @currentID <span style="color: #0000FF;">INT</span>
    , @firstID <span style="color: #0000FF;">INT</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @columnList
<span style="color: #0000FF;">SELECT</span> column_id 
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> 
<span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>@tableToBCP<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> column_id;
&nbsp;
<span style="color: #0000FF;">IF</span> @<span style="color: #0000FF;">Top</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">'TOP ('</span> <span style="color: #808080;">+</span> @<span style="color: #0000FF;">Top</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">') '</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @firstID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>columnID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> @columnList;
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> @columnList<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @currentID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>columnID<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> @columnList;
&nbsp;
    <span style="color: #0000FF;">IF</span> @currentID <span style="color: #808080;">&lt;&gt;</span> @firstID
        <span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">','</span>;
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> 
                            <span style="color: #0000FF;">CASE</span> 
                                <span style="color: #0000FF;">WHEN</span> user_type_id <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">231</span>, <span style="color: #000;">167</span>, <span style="color: #000;">175</span>, <span style="color: #000;">239</span><span style="color: #808080;">&#41;</span> 
                                <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'CASE WHEN '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' = '</span><span style="color: #FF0000;">''</span><span style="color: #FF0000;">' THEN '</span> 
                                    <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> 
                                        <span style="color: #0000FF;">WHEN</span> is_nullable <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'NULL'</span> 
                                        <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLICATE</span><span style="color: #808080;">&#40;</span>@OverrideChar, max_length<span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span>
                                      <span style="color: #0000FF;">END</span>
                                    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' WHEN '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' LIKE '</span><span style="color: #FF0000;">'%'</span> <span style="color: #808080;">+</span> @Delimiter <span style="color: #808080;">+</span> <span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">''</span>
                                        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' OR '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' LIKE '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">' + CHAR(9) + '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">''</span> <span style="color: #008080;">-- tab</span>
                                        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' OR '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' LIKE '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">' + CHAR(10) + '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">''</span> <span style="color: #008080;">-- line feed</span>
                                        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' OR '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' LIKE '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">' + CHAR(13) + '</span><span style="color: #FF0000;">'%'</span><span style="color: #FF0000;">''</span> <span style="color: #008080;">-- carriage return</span>
                                        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' THEN '</span> 
                                        <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> 
                                            <span style="color: #0000FF;">WHEN</span> is_nullable <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'NULL'</span> 
                                            <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLICATE</span><span style="color: #808080;">&#40;</span>@OverrideChar, max_length<span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span>
                                          <span style="color: #0000FF;">END</span>
                                    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' ELSE '</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' END'</span> 
                                <span style="color: #0000FF;">ELSE</span> name 
                            <span style="color: #0000FF;">END</span> 
    <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">columns</span> 
    <span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span>@tableToBCP<span style="color: #808080;">&#41;</span>
        <span style="color: #808080;">AND</span> column_id <span style="color: #808080;">=</span> @currentID;
&nbsp;
    <span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @columnList <span style="color: #0000FF;">WHERE</span> columnID <span style="color: #808080;">=</span> @currentID;
&nbsp;
&nbsp;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">SET</span> @bcpStatement <span style="color: #808080;">=</span> @bcpStatement <span style="color: #808080;">+</span> <span style="color: #FF0000;">' FROM '</span> <span style="color: #808080;">+</span> @tableToBCP 
    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' WITH (NOLOCK) OPTION (MAXDOP 1);&quot; queryOut '</span>
    <span style="color: #808080;">+</span> @Directory <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span>@tableToBCP, <span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'_'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.dat -S'</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">@@SERVERNAME</span>
    <span style="color: #808080;">+</span> <span style="color: #FF0000;">' -T -t&quot;'</span> <span style="color: #808080;">+</span> @Delimiter <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&quot; -c -C;'</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> @bcpStatement;</pre></div></div>

<p>The sample output of this would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">BCP &quot;SELECT CustomerKey,GeographyKey,CASE WHEN CustomerAlternateKey = '' THEN 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' WHEN CustomerAlternateKey LIKE '%|%' OR 
CustomerAlternateKey LIKE '%' + CHAR(9) + '%' OR CustomerAlternateKey LIKE 
'%' + CHAR(10) + '%' OR CustomerAlternateKey LIKE '%' + CHAR(13) + '%' 
THEN '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' ELSE CustomerAlternateKey END,
CASE WHEN Title = '' THEN NULL WHEN Title LIKE '%|%' OR Title LIKE '%' + CHAR(9)
 + '%' OR Title LIKE '%' + CHAR(10) + '%' OR Title LIKE '%' + CHAR(13) + '%' 
THEN NULL ELSE Title END,CASE WHEN FirstName = '' THEN NULL WHEN FirstName 
LIKE '%|%' OR FirstName LIKE '%' + CHAR(9) + '%' OR FirstName LIKE '%' + 
CHAR(10) + '%' OR FirstName LIKE '%' + CHAR(13) + '%' THEN NULL ELSE 
FirstName END,CASE WHEN MiddleName = '' THEN NULL WHEN MiddleName LIKE '%|%'
 OR MiddleName LIKE '%' + CHAR(9) + '%' OR MiddleName LIKE '%' + CHAR(10) +
 '%' OR MiddleName LIKE '%' + CHAR(13) + '%' THEN NULL ELSE MiddleName END,
CASE WHEN LastName = '' THEN NULL WHEN LastName LIKE '%|%' OR LastName LIKE
 '%' + CHAR(9) + '%' OR LastName LIKE '%' + CHAR(10) + '%' OR LastName LIKE
 '%' + CHAR(13) + '%' THEN NULL ELSE LastName END,NameStyle,BirthDate,CASE 
WHEN MaritalStatus = '' THEN NULL WHEN MaritalStatus LIKE '%|%' OR 
MaritalStatus LIKE '%' + CHAR(9) + '%' OR MaritalStatus LIKE '%' + CHAR(10) 
+ '%' OR MaritalStatus LIKE '%' + CHAR(13) + '%' THEN NULL ELSE MaritalStatus 
END,CASE WHEN Suffix = '' THEN NULL WHEN Suffix LIKE '%|%' OR Suffix LIKE '%' 
+ CHAR(9) + '%' OR Suffix LIKE '%' + CHAR(10) + '%' OR Suffix LIKE '%' + 
CHAR(13) + '%' THEN NULL ELSE Suffix END,CASE WHEN Gender = '' THEN NULL 
WHEN Gender LIKE '%|%' OR Gender LIKE '%' + CHAR(9) + '%' OR Gender LIKE '%' 
+ CHAR(10) + '%' OR Gender LIKE '%' + CHAR(13) + '%' THEN NULL ELSE Gender 
END,CASE WHEN EmailAddress = '' THEN NULL WHEN EmailAddress LIKE '%|%' OR 
EmailAddress LIKE '%' + CHAR(9) + '%' OR EmailAddress LIKE '%' + CHAR(10) + 
'%' OR EmailAddress LIKE '%' + CHAR(13) + '%' THEN NULL ELSE EmailAddress END,
YearlyIncome,TotalChildren,NumberChildrenAtHome, CASE WHEN EnglishEducation = '' 
THEN NULL WHEN EnglishEducation LIKE '%|%' OR 
EnglishEducation LIKE '%' + CHAR(9) + '%' OR EnglishEducation LIKE '%' + 
CHAR(10) + '%' OR EnglishEducation LIKE '%' 
+ CHAR(13) + '%' THEN NULL ELSE EnglishEducation END,CASE WHEN 
SpanishEducation = '' THEN NULL WHEN SpanishEducation LIKE '%|%' OR 
SpanishEducation LIKE '%' + CHAR(9) + '%' OR SpanishEducation LIKE '%' + 
CHAR(10) + '%' OR SpanishEducation LIKE '%' + CHAR(13) + '%' THEN NULL 
ELSE SpanishEducation END,CASE WHEN FrenchEducation = '' THEN NULL WHEN 
FrenchEducation LIKE '%|%' OR FrenchEducation LIKE '%' + CHAR(9) + '%' 
OR FrenchEducation LIKE '%' + CHAR(10) + '%' OR FrenchEducation LIKE '%' 
+ CHAR(13) + '%' THEN NULL ELSE FrenchEducation END,CASE WHEN 
EnglishOccupation = '' THEN NULL WHEN EnglishOccupation LIKE '%|%' OR 
EnglishOccupation LIKE '%' + CHAR(9) + '%' OR EnglishOccupation LIKE '%' 
+ CHAR(10) + '%' OR EnglishOccupation LIKE '%' + CHAR(13) + '%' THEN 
NULL ELSE EnglishOccupation END,CASE WHEN SpanishOccupation = '' THEN 
NULL WHEN SpanishOccupation LIKE '%|%' OR SpanishOccupation LIKE '%' 
+ CHAR(9) + '%' OR SpanishOccupation LIKE '%' + CHAR(10) + '%' OR 
SpanishOccupation LIKE '%' + CHAR(13) + '%' THEN NULL ELSE SpanishOccupation 
END,CASE WHEN FrenchOccupation = '' THEN NULL WHEN FrenchOccupation LIKE 
'%|%' OR FrenchOccupation LIKE '%' + CHAR(9) + '%' OR FrenchOccupation 
LIKE '%' + CHAR(10) + '%' OR FrenchOccupation LIKE '%' + CHAR(13) + '%' 
THEN NULL ELSE FrenchOccupation END,CASE WHEN HouseOwnerFlag = '' THEN 
NULL WHEN HouseOwnerFlag LIKE '%|%' OR HouseOwnerFlag LIKE '%' + CHAR(9) 
+ '%' OR HouseOwnerFlag LIKE '%' + CHAR(10) + '%' OR HouseOwnerFlag LIKE 
'%' + CHAR(13) + '%' THEN NULL ELSE HouseOwnerFlag END,NumberCarsOwned,CASE 
WHEN AddressLine1 = '' THEN NULL WHEN AddressLine1 LIKE '%|%' OR AddressLine1 
LIKE '%' + CHAR(9) + '%' OR AddressLine1 LIKE '%' + CHAR(10) + '%' OR 
AddressLine1 LIKE '%' + CHAR(13) + '%' THEN NULL ELSE AddressLine1 END,CASE 
WHEN AddressLine2 = '' THEN NULL WHEN AddressLine2 LIKE '%|%' OR AddressLine2 
LIKE '%' + CHAR(9) + '%' OR AddressLine2 LIKE '%' + CHAR(10) + '%' OR 
AddressLine2 LIKE '%' + CHAR(13) + '%' THEN NULL ELSE AddressLine2 END,CASE 
WHEN Phone = '' THEN NULL WHEN Phone LIKE '%|%' OR Phone LIKE '%' + CHAR(9) 
+ '%' OR Phone LIKE '%' + CHAR(10) + '%' OR Phone LIKE '%' + CHAR(13) + '%' 
THEN NULL ELSE Phone END,DateFirstPurchase,CASE WHEN CommuteDistance = '' 
THEN NULL WHEN CommuteDistance LIKE '%|%' OR CommuteDistance LIKE '%' + 
CHAR(9) + '%' OR CommuteDistance LIKE '%' + CHAR(10) + '%' OR CommuteDistance 
LIKE '%' + CHAR(13) + '%' THEN NULL ELSE CommuteDistance END 
FROM AdventureWorksDW2008R2.dbo.DimCustomer WITH (NOLOCK) OPTION (MAXDOP 1);&quot; queryOut 
C:\bcp_output\AdventureWorksDW2008R2_dbo_DimCustomer.dat -SSQLFOOL -T -t&quot;|&quot; -c -C;</pre></div></div>

<p>I don&#8217;t know about you, but that would take me a bit longer to write manually than to execute the script above. <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>One note: I&#8217;ve found that copying this code into a batch file will actually render the CHAR functions, i.e. CHAR(13) will be replaced with a carriage return in the script. To avoid this, copy and paste the BCP script directly into your command window. </p>
<p>Not familiar with BCP? Refer to my blog post on <a href="http://sqlfool.com/2008/12/bcp-basics/" target="_blank">BCP Basics</a> to help get you started.</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/04/bcp-script-generator/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A question for my blog readers&#8230;</title>
		<link>http://sqlfool.com/2012/02/a-question-for-my-blog-readers/</link>
		<comments>http://sqlfool.com/2012/02/a-question-for-my-blog-readers/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 15:42:52 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Teradata]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1594</guid>
		<description><![CDATA[My role at work has recently changed. While I will continue to be working with SQL Server, I will also be spending a good chunk of my time working in our newly acquired Teradata environment. My plan is to blog about both as time permits, but I am trying to determine whether I should create [...]]]></description>
			<content:encoded><![CDATA[<p>My role at work has recently changed. While I will continue to be working with SQL Server, I will also be spending a good chunk of my time working in our newly acquired <a href="http://teradata.com/" target="_blank">Teradata</a> environment. My plan is to blog about both as time permits, but I am trying to determine whether I should create a separate blog for the Teradata content. So my question for you, dear reader, is whether you think I should blog on this site about both SQL Server *and* Teradata, or whether I should separate the content? Any input is appreciated. </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2012/02/a-question-for-my-blog-readers/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Are You Approaching Your Partition Range Limits?</title>
		<link>http://sqlfool.com/2011/11/are-you-approaching-your-partition-range-limits/</link>
		<comments>http://sqlfool.com/2011/11/are-you-approaching-your-partition-range-limits/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 20:32:01 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[maintenace]]></category>
		<category><![CDATA[partitioning]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1586</guid>
		<description><![CDATA[In my post last week, How To Estimate Data Utilization, I said that it may be my last post for a while. Well&#8230; apparently I lied. For those of you who use table partitioning, you know that you need to define a partitioning scheme and function prior to applying partitioning to an index. Personally, I [...]]]></description>
			<content:encoded><![CDATA[<p>In my post last week, <a href="http://sqlfool.com/2011/10/how-to-estimate-data-utilization/" target="_blank">How To Estimate Data Utilization</a>, I said that it may be my last post for a while.  Well&#8230; apparently I lied.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>For those of you who use <a href="http://sqlfool.com/2008/11/102/" target="_blank">table partitioning</a>, you know that you need to define a partitioning scheme and function prior to applying partitioning to an index.  Personally, I tend to build the function for a couple of years out, and I tend to create them through the end of a calendar year.  Now, if I failed to expand a partition range at the end of the year, then come January 1st, all of my data would be written to the same partition.  Not the end of the world, no, but it causes all kinds of nasty performance and maintenance issues.  Thus, as part of my end-of-year / maternity-leave preparations, I&#8217;m in the process of examining all partitioned functions to identify those that need to have their partition ranges expanded. For those interested, here&#8217;s the script I used:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#Results'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Results;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #Results
<span style="color: #808080;">&#40;</span>
      databaseName  <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , schemaName    <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , functionName  <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , data_space_id <span style="color: #0000FF;">INT</span>
    , maxRangeValue <span style="color: #0000FF;">SQL_VARIANT</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">/* Grab results for each database and store in our temp table.  
   And no, I don't *need* to select from sys.indexes and perform 
   left joins, but I'm overly cautious and want to make sure 
   I'm not accidentally missing any databases. :) */</span>
&nbsp;
<span style="color: #008080;">--EXECUTE master.dbo.sp_msforeachdb</span>
<span style="color: #0000FF;">EXECUTE</span> sp_foreachdb <span style="color: #FF0000;">'USE ?;
INSERT INTO #Results
SELECT DB_NAME() AS databaseName
    , sps.name AS schemaName
    , spf.name AS functionName
    , sps.data_space_id 
    , MAX(prv.value) AS maxRangeValue
FROM sys.indexes AS i
LEFT JOIN sys.partition_schemes AS sps WITH (NOLOCK)
    ON i.data_space_id = sps.data_space_id
LEFT JOIN sys.partition_functions AS spf WITH (NOLOCK)
    ON sps.function_id = spf.function_id
LEFT JOIN sys.partition_range_values AS prv WITH (NOLOCK)
    ON spf.function_id = prv.function_id
GROUP BY sps.name
    , spf.name
    , sps.data_space_id;'</span>;
<span style="color: #008080;">/*  
    sp_foreachdb was written by SQL MVP Aaron Bertrand and can be downloaded 
    at http://www.mssqltips.com/sqlservertip/2201/making-a-more-reliable-and-flexible-spmsforeachdb/
    Alternatively, you can also use sys.sp_MSforeachdb
*/</span>
&nbsp;
<span style="color: #008080;">/* Make sure we're not missing any major databases */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> databaseName <span style="color: #0000FF;">FROM</span> #Results<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Retrieve our results */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> #Results
<span style="color: #0000FF;">WHERE</span> schemaName <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> maxRangeValue;</pre></div></div>

<p>Example Results:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">databaseName        schemaName                      functionName                          data_space_id   maxRangeValue
------------------- ------------------------------- ------------------------------------- --------------- -------------------------
HistoricalMart      dailyRangeDate_ps               dailyRangeDate_pf                     65609           2011-12-31 00:00:00.000
AdventureWorks      yearlyRangeSmallDateTime_ps     yearlyRangeSmallDateTime_pf           65605           2012-01-01 00:00:00.000
dbaTools            monthlyRangeDateTime_ps         monthlyRangeDateTime_pf               65604           2012-12-01 00:00:00.000</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/11/are-you-approaching-your-partition-range-limits/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How To Estimate Data Utilization</title>
		<link>http://sqlfool.com/2011/10/how-to-estimate-data-utilization/</link>
		<comments>http://sqlfool.com/2011/10/how-to-estimate-data-utilization/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 19:11:12 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Business Intelligence]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[DMV]]></category>
		<category><![CDATA[indexes]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1578</guid>
		<description><![CDATA[Recently, on a conference call presenting data growth rates and database capacity projections, I had a top-line executive ask, &#8220;But how much of that data are we actually using today?&#8221; The question was met with silence; unless you have rigorous auditing in place &#8212; and kudos to you if you do &#8212; it&#8217;s a difficult [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, on a conference call presenting data growth rates and database capacity projections, I had a top-line executive ask, &#8220;But how much of that data are we actually <em>using</em> today?&#8221;  The question was met with silence; unless you have rigorous auditing in place &#8212; and kudos to you if you do &#8212; it&#8217;s a difficult question to answer.  But it begs the question, is there some way to gleam this information from SQL Server?  I think the answer is &#8220;yes,&#8221; if you make some assumptions and understand what you&#8217;re looking at.  </p>
<p>SQL Server collects stats about every time an index is used and how it is used (i.e. whether a user seeked or scanned the index, etc.).  It also provides a DMV to view these stats: <a href="http://msdn.microsoft.com/en-us/library/ms188755.aspx" target="_blank">sys.dm_db_index_usage_stats</a>.</p>
<p>This DMV provides a wealth of great information, but to answer our question of &#8220;What data is actually being used?&#8221;, we have to refine our criteria.  Are we talking in terms of table counts or data size?  I&#8217;d argue that data size is more important than table counts; one unqueried millow-row table is more wasteful than a hundred ten-row tables.  </p>
<p>Also, are we looking at indexes or content?  From a database perspective, I&#8217;m more interested in indexes: how much space are we wasting on unused indexes?  To identify this, I need to look at the activity on each individual index.</p>
<p>From a business perspective, I would be more interested in content (i.e. tables): how much business information is being stored that no one is even looking at?  To answer this question, I need to roll up all index usage to see if *any* of the indexes on a table were used.  Since both were of interest to me, I decided to write queries to answer both questions.  </p>
<p>Lastly, we need to understand the flaws with this data.  Chiefly, I cannot tell whether a user requested one row from a million-row table, or if [s]he needed all of the data in the table.  This is a pretty important issue, especially with large historical data stores, and it&#8217;s where I have to make the biggest assumption: if even one person looked at one row in the table, I count all pages in the table as having been accessed.  </p>
<p>Now, you may make different decisions than I did above, and that&#8217;s fine&#8230; each environment and project has different needs.  But these assumptions are very important to understanding the output of the query below:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">USE</span> master;
GO
&nbsp;
<span style="color: #008080;">/* 
    This will give you an approximation of how much data is being utilized on a server.
    Since the data is only valid as of the last server reboot, we should start off with
    an idea of how much data we've accrued.  
*/</span>
&nbsp;
<span style="color: #008080;">/* Find out when the server was last rebooted */</span>
<span style="color: #008080;">-- 2008</span>
<span style="color: #0000FF;">SELECT</span> sqlserver_start_time <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_os_sys_info</span>;
<span style="color: #008080;">-- 2005</span>
<span style="color: #0000FF;">SELECT</span> create_date <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">=</span> <span style="color: #FF0000;">'tempdb'</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">/* Create a temporary table to hold our data, since we're going to iterate through databases */</span>
<span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#Results'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Results;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #Results
<span style="color: #808080;">&#40;</span>
      databaseName  <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , tableName     <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , indexID       <span style="color: #0000FF;">INT</span>
    , records       <span style="color: #0000FF;">BIGINT</span>
    , activity      <span style="color: #0000FF;">BIGINT</span>
    , totalPages    <span style="color: #0000FF;">BIGINT</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/*  
    sp_foreachdb was written by SQL MVP Aaron Bertrand and can be downloaded 
    at http://www.mssqltips.com/sqlservertip/2201/making-a-more-reliable-and-flexible-spmsforeachdb/
    Alternatively, you can also use sys.sp_MSforeachdb
*/</span>
<span style="color: #008080;">--EXECUTE master.dbo.sp_foreachdb</span>
<span style="color: #0000FF;">EXECUTE</span> sys.<span style="color: #202020;">sp_MSforeachdb</span>
<span style="color: #FF0000;">'   USE ?; 
&nbsp;
    -- You can gleam a lot of information about historical data usage from partitions
    -- but for now, we will just roll up any partitions we may have
    WITH myCTE AS
    (
        SELECT p.[object_id] AS objectID
            , p.index_id
            , SUM(p.[rows]) AS records
            , SUM(au.total_pages) AS totalPages
        FROM sys.partitions AS p WITH (NOLOCK)
        JOIN sys.allocation_units AS au WITH (NOLOCK)
            ON p.hobt_id = au.container_id
        GROUP BY p.[object_id] 
            , p.index_id
    )
&nbsp;
    -- Grab all tables and join to our usage stats DMV
    INSERT INTO #Results
    SELECT DB_NAME() AS databaseName
        , t.name
        , x.index_id
        , MAX(x.records) AS records
        , ISNULL(SUM(us.user_lookups + us.user_scans + us.user_seeks), 0) AS activity
        , SUM(x.totalPages) AS totalPages
    FROM sys.tables AS t WITH (NOLOCK)
    JOIN myCTE AS x
        ON t.[object_id] = x.objectID
    LEFT JOIN sys.dm_db_index_usage_stats AS us WITH (NOLOCK)
        ON us.[object_id] = x.objectID
        AND us.index_id = x.index_id
        AND us.database_id = DB_ID()
    GROUP BY t.name
    , x.index_id;'</span>
&nbsp;
<span style="color: #008080;">/* Because we're looping through databases, make sure we're not missing any major ones */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> databaseName <span style="color: #0000FF;">FROM</span> #Results<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Retrieve actual % data utilization, which is performed at the index level */</span>
<span style="color: #0000FF;">SELECT</span> databaseName
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>queriedPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> TotalQueriedPages
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> TotalPages
    , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>queriedPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span>, <span style="color: #000;">0</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'%DataUtil'</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">SELECT</span> databaseName
        , tableName
        , indexID
        , <span style="color: #0000FF;">CASE</span> <span style="color: #008080;">-- If we have any activity at all on an index, count it as activity</span>
            <span style="color: #0000FF;">WHEN</span> activity <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #000;">0.0</span>
            <span style="color: #0000FF;">ELSE</span> totalPages
          <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> queriedPages
        , totalPages
    <span style="color: #0000FF;">FROM</span> #Results
    <span style="color: #0000FF;">WHERE</span> databaseName <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'master'</span>, <span style="color: #FF0000;">'tempdb'</span>, <span style="color: #FF0000;">'msdb'</span>, <span style="color: #FF0000;">'model'</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span> x
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> databaseName
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> databaseName;
&nbsp;
<span style="color: #008080;">/* Retrieve % content utilization, which is performed at the table level */</span>
<span style="color: #0000FF;">SELECT</span> databaseName
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>queriedPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> TotalQueriedPages
    , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> TotalPages
    , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>queriedPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span>, <span style="color: #000;">0</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'%ContentUtil'</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">SELECT</span> databaseName
        , tableName
        , <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span>records<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> records
        , <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>activity<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> queriedPages
        , <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>totalPages<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> totalPages
    <span style="color: #0000FF;">FROM</span> #Results
    <span style="color: #0000FF;">WHERE</span> databaseName <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'master'</span>, <span style="color: #FF0000;">'tempdb'</span>, <span style="color: #FF0000;">'msdb'</span>, <span style="color: #FF0000;">'model'</span><span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> databaseName
        , tableName
<span style="color: #808080;">&#41;</span> x
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> databaseName
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> databaseName;</pre></div></div>

<p>Results:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">databaseName               TotalQueriedPages   TotalPages           %DataUtil
-------------------------- ------------------- -------------------- ----------------------
Database1 		   127618701           130607247            0.969619893356378
Database2 		   567188              1614958              0.351209133612143
Database3 		   34269036            34579469             0.991022620966216
Database4 		   137970594           170733391            0.803399928206158
Database5 		   74632930            101543575            0.66909214627557
Database6 		   55809933            72884205             0.765734157938039
Database7 		   560810026           620609815            0.902175272517656
&nbsp;
databaseName               TotalQueriedPages   TotalPages           %ContentUtil
-------------------------- ------------------- -------------------- ----------------------
Database1 		   127763715           130607247            0.970721679051682
Database2 		   571125              1614958              0.353646967908763
Database3 		   34269036            34579469             0.991022620966216
Database4 		   137970921           170733391            0.803399928206158
Database5 		   96144726            101543575            0.861947682777784
Database6 		   72269666            72884205             0.991568146820268
Database7 		   620525938           620609815            0.998240279711804</pre></div></div>

<p>The first result set examines the utilization of indexes, and the second result set examines the utilization of data at the content (table) level.  For example, if we look at Database6, we&#8217;ll see that we are only utilizing 77% of our indexes, but we&#8217;re looking at 99% of our table data.  So this is a good indicator that we have some unused indexes to clean up in that database.  </p>
<p>Know a better way to answer this question using SQL Server DMV&#8217;s?  Please leave me a comment so I can learn from your experience.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In unrelated news, this may be my last blog post for a little while.  I&#8217;m due with my second child a week from today and expect all of my free time to be consumed by him for a little while.  That and, quite frankly, I do not trust myself near a computer, especially a database, in such a sleep-deprived state.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/10/how-to-estimate-data-utilization/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>11-Word Warning</title>
		<link>http://sqlfool.com/2011/04/11-word-warning/</link>
		<comments>http://sqlfool.com/2011/04/11-word-warning/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 15:58:32 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[chainblogging]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1524</guid>
		<description><![CDATA[Tom LaRock posted a new Meme Monday challenge: &#8220;Write a SQL blog post in 11 words or less.&#8221; Donabel Santos tagged me, and I couldn&#8217;t resist the challenge. So here&#8217;s my entry: Hasty coding, error prone. No backups, data loss. Company for sale. This was inspired by the recent spate of stories I&#8217;ve heard about [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.com/sqlrockstar" target="_blank">Tom LaRock</a> posted a <a href="http://www.thomaslarock.com/2011/04/welcome-to-meme-monday/" target="_blank">new Meme Monday</a> challenge: &#8220;Write a SQL blog post in 11 words or less.&#8221;</p>
<p><a href="http://www.sqlmusings.com/2011/04/04/meme-monday-sql-server-story-in-11-words-or-less/" target="_blank">Donabel Santos</a> tagged me, and I couldn&#8217;t resist the challenge.  So here&#8217;s my entry:</p>
<p><em>Hasty coding, error prone. No backups, data loss. Company for sale.</em></p>
<p>This was inspired by the recent spate of stories I&#8217;ve heard about companies that have failed because they did not properly manage their data and databases.  </p>
<p>I don&#8217;t know who&#8217;s been tagged or not, so I&#8217;m gagging some of my SQL Saturday Chicago friends: </p>
<ul>
<li><a href="http://www.lessthandot.com" target="_blank">Ted Krueger</a></li>
<li><a href="http://blogs.lessthandot.com/index.php?disp=authdir&#038;author=420" target="_blank">Jes Borland</a></li>
<li><a href="http://www.Made2Mentor.com" target="_blank">David Stein</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/04/11-word-warning/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Identity Columns: Are You Nearing The Limits?</title>
		<link>http://sqlfool.com/2011/01/identity-columns-are-you-nearing-the-limits/</link>
		<comments>http://sqlfool.com/2011/01/identity-columns-are-you-nearing-the-limits/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 16:03:00 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[maintenace]]></category>
		<category><![CDATA[monitor]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1475</guid>
		<description><![CDATA[I use identity columns frequently. After all, identity columns make great clustering keys. But it&#8217;s important when using identity columns to check on the amount of values you have left before you reach the limit of your data type. An identity column has a fixed amount of values it can use based upon whether you [...]]]></description>
			<content:encoded><![CDATA[<p>I use identity columns frequently.  After all, <a href="http://www.simple-talk.com/sql/learn-sql-server/effective-clustered-indexes/" target="_blank">identity columns make great clustering keys</a>.  But it&#8217;s important when using identity columns to check on the amount of values you have left before you reach the limit of your data type.  An identity column has a fixed amount of values it can use based upon whether you specified tinyint, smallint, int, or bigint when you defined the column.  If you reach this limit, your inserts <del datetime="2011-01-13T15:41:01+00:00">will blow up and cause a Chernobyl-like SQL meltdown</del> will begin to fail.  I just finished an audit of my tables and thought I&#8217;d share the script.  I would like to warn that this script is *not* perfect&#8230; namely, it doesn&#8217;t handle negative integer values very elegantly.  It also doesn&#8217;t know if you started your seed at zero, approached your max positive limit, then reseeded to the negative limit (see my &#8220;quick and dirty fix&#8221; tip at the end of this article).</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Define how close we are to the value limit
   before we start throwing up the red flag.
   The higher the value, the closer to the limit. */</span>
<span style="color: #0000FF;">Declare</span> @threshold <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> .85;
&nbsp;
<span style="color: #008080;">/* Create a temp table */</span>
<span style="color: #0000FF;">Create</span> <span style="color: #0000FF;">Table</span> #identityStatus
<span style="color: #808080;">&#40;</span>
      database_name     <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , table_name        <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , column_name       <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , data_type         <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
    , last_value        <span style="color: #0000FF;">bigint</span>
    , max_value         <span style="color: #0000FF;">bigint</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* Use an undocumented command to run a SQL statement
   in each database on a server */</span>
<span style="color: #0000FF;">Execute</span> sp_msforeachdb <span style="color: #FF0000;">'
    Use [?];
    Insert Into #identityStatus
    Select '</span><span style="color: #FF0000;">'?'</span><span style="color: #FF0000;">' As [database_name]
        , Object_Name(id.object_id, DB_ID('</span><span style="color: #FF0000;">'?'</span><span style="color: #FF0000;">')) As [table_name]
        , id.name As [column_name]
        , t.name As [data_type]
        , Cast(id.last_value As bigint) As [last_value]
        , Case 
            When t.name = '</span><span style="color: #FF0000;">'tinyint'</span><span style="color: #FF0000;">'   Then 255 
            When t.name = '</span><span style="color: #FF0000;">'smallint'</span><span style="color: #FF0000;">'  Then 32767 
            When t.name = '</span><span style="color: #FF0000;">'int'</span><span style="color: #FF0000;">'       Then 2147483647 
            When t.name = '</span><span style="color: #FF0000;">'bigint'</span><span style="color: #FF0000;">'    Then 9223372036854775807
          End As [max_value]
    From sys.identity_columns As id
    Join sys.types As t
        On id.system_type_id = t.system_type_id
    Where id.last_value Is Not Null'</span>;
&nbsp;
<span style="color: #008080;">/* Retrieve our results and format it all prettily */</span>
<span style="color: #0000FF;">Select</span> database_name
    , table_name
    , column_name
    , data_type
    , last_value
    , <span style="color: #0000FF;">Case</span> 
        <span style="color: #0000FF;">When</span> last_value <span style="color: #808080;">&lt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Then</span> <span style="color: #000;">100</span>
        <span style="color: #0000FF;">Else</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span> <span style="color: #808080;">-</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>last_value <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">float</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> max_value<span style="color: #808080;">&#41;</span> <span style="color: #808080;">*</span> <span style="color: #000;">100</span> 
      <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #808080;">&#91;</span>percentLeft<span style="color: #808080;">&#93;</span>
    , <span style="color: #0000FF;">Case</span> 
        <span style="color: #0000FF;">When</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>last_value <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">float</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> max_value <span style="color: #808080;">&gt;=</span> @threshold
            <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'warning: approaching max limit'</span>
        <span style="color: #0000FF;">Else</span> <span style="color: #FF0000;">'okay'</span>
        <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #808080;">&#91;</span>id_status<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">From</span> #identityStatus
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> percentLeft;
&nbsp;
<span style="color: #008080;">/* Clean up after ourselves */</span>
<span style="color: #0000FF;">Drop</span> <span style="color: #0000FF;">Table</span> #identityStatus;</pre></div></div>

<p>If you find yourself quickly approaching your max limit and need to implement a quick and dirty fix, you can <a href="http://sqlfool.com/2008/11/max-int-identity-value-reached-dbcc-checkident" target-"_blank">reseed your identity column</a>.  Of course, this only works if you started at zero instead of the actual lower, negative limit.</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/01/identity-columns-are-you-nearing-the-limits/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Effective Clustered Indexing</title>
		<link>http://sqlfool.com/2011/01/effective-clustered-indexing/</link>
		<comments>http://sqlfool.com/2011/01/effective-clustered-indexing/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 15:03:49 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Internals]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[article]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[clustered indexes]]></category>
		<category><![CDATA[clustering]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[redgate]]></category>
		<category><![CDATA[simple-talk]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1467</guid>
		<description><![CDATA[My first Simple-Talk article was published yesterday! I&#8217;m pretty excited about it and wanted to share the link. In the article, I give an overview of how clustered and nonclustered indexes work, and I demonstrate why clustered index best practices &#8212; narrow, unique, static, and ever-increasing &#8212; are important design considerations. You can find the [...]]]></description>
			<content:encoded><![CDATA[<p>My first <a href="http://www.simple-talk.com/" target="_blank">Simple-Talk</a> article was published yesterday!  I&#8217;m pretty excited about it and wanted to share the link.  In the article, I give an overview of how clustered and nonclustered indexes work, and I demonstrate why clustered index best practices &#8212; narrow, unique, static, and ever-increasing &#8212; are important design considerations.</p>
<p>You can find the article on Simple-Talk&#8217;s website at:<br />
<a href="http://www.simple-talk.com/sql/learn-sql-server/effective-clustered-indexes/" target="_blank">http://www.simple-talk.com/sql/learn-sql-server/effective-clustered-indexes/</a></p>
<p>Please let me know your thoughts!  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/01/effective-clustered-indexing/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Yet Another PASS Summit Recap &amp; Thoughts on PDW</title>
		<link>http://sqlfool.com/2010/11/yet-another-pass-summit-recap/</link>
		<comments>http://sqlfool.com/2010/11/yet-another-pass-summit-recap/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 15:43:10 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Business Intelligence]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[PASS]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[2008]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[data warehouse]]></category>
		<category><![CDATA[parallel data warehouse]]></category>
		<category><![CDATA[pdw]]></category>
		<category><![CDATA[Summit]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1427</guid>
		<description><![CDATA[The SQL blogosphere has been lit up with PASS Summit recaps. I debated about whether or not to write my own post, until I remembered that this blog serves as a mini-journal for me too. I have a notoriously poor memory&#8211;my husband likes to say that my CPU and memory are good, but I must [...]]]></description>
			<content:encoded><![CDATA[<p>The SQL blogosphere has been <a href="http://www.sqlmusings.com/2010/11/10/pass-summit-day-1-keynote-mcm-sqlpasstv-servicebroker-bi-power-hour-mdx-who-dunnit/" target="_blank">lit</a> <a href="http://cwebbbi.wordpress.com/2010/11/10/pass-summit-2010-day-1/" target="_blank">up</a> <a href="http://www.sqlservercentral.com/blogs/kathi_kellenberger/archive/2010/11/15/pass-report-1-why-you-should-have-been-there.aspx" target="_blank">with</a> <a href="http://blogs.lessthandot.com/index.php/ITProfessionals/ProfessionalDevelopment/sql-pass-2010" target="_blank">PASS</a> <a href="http://www.sqlservercentral.com/blogs/stratesql/archive/2010/11/15/david-dewitt-at-the-pass-summit.aspx" target="_blank">Summit</a> <a href="http://www.sqlservercentral.com/blogs/sqlchicken/archive/2010/11/15/pass-summit-magic_3A00_-the-turn.aspx" target="_blank">recaps</a>.</p>
<p>I debated about whether or not to write my own post, until I remembered that this blog serves as a mini-journal for me too.  I have a notoriously poor memory&#8211;my husband likes to say that my CPU and memory are good, but I must have an unusual clustering strategy&#8211;so maybe this blog post will be a good pointer for me when I start prepping for next year&#8217;s Summit.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>This was definitely the best PASS Summit conference ever.  While there will always be opportunities to do things better&#8211;improvement is a never-ending process&#8211;it was clear that the organizers of this event listened to the feedback they had received the previous year.  One of the best changes?  Backpacks.  These were very useful, as evidenced by their presence everywhere.  Nice job, organizers!</p>
<p>My absolute favorite thing about Summit is the chance to meet and reconnect with so many amazing SQL folks.  There were entirely too many people to list out, but some highlights include meeting <a href="http://twitter.com/crysmanson">Crys Manson</a>, <a href="http://twitter.com/sqlchicken">Jorge Segarra</a>, and <a href="http://twitter.com/datachick">Karen Lopez</a> for the first time.  I also had a chance encounter with <a href="http://ola.hallengren.com/" target="_blank">Ola Hallengren</a> in the Sheraton elevator.  Apparently we were only staying a few rooms apart this year.  We ended up having a couple of really great discussions about index fragmentation, the differences between our scripts, and things we&#8217;d like to see changed in future releases of SQL Server.  </p>
<p>I had the opportunity to sit on the panel at the WIT luncheon.  All of the women on the panel were amazing, and I was honored just to be sitting at the same table as them.  I was especially pleased to meet Nora Denzel, a Senior Vice President at Intuit.  Intelligent, confident, and witty, she is a great role model for young technical women, myself included.  I can only hope that some of her gumption rubbed off on me due to our close proximity.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   After the event, I was pleasantly surprised by how many folks&#8211;men and women both&#8211;came up to me to tell me how much they enjoyed it.  Thanks to the WIT VC for organizing another great event!</p>
<p>The lightning talk sessions were a new feature this year, and I think I like it.  The format of the lightning session is 7 speakers presenting on a topic for 5 quick minutes.  Watching these sessions is kind of like skipping right to the center of a tootsie pop: all content and no fluff.  The standout lightning talk presentation for me was <a href="http://sqlblog.com/blogs/adam_machanic/" target="_blank">Adam Machanic&#8217;s</a>.  It was beautifully rehearsed and choreographed.  Nice job, Adam!</p>
<p>Another of the many highlights of the week was meeting the Microsoft execs.  In addition to meeting Ted Kummert, Mark Souza, and Donald Farmer&#8211;all very nice gentlemen&#8211;I had the opportunity to speak at length with Jose Blakely about Parallel Data Warehouse (PDW).  PDW, formerly codenamed Madison, was officially launched at Summit.  Jose was kind enough to explain the PDW architecture, both where it came from and the vision for where it&#8217;s going.  I&#8217;d attempt to regurgitate it here, but I think the probability of me misquoting would be high.  </p>
<p>Suffice it to say, this technology has me excited.  Why?  Quite frankly, I think PDW will do for data warehousing what SQL Server did for databases, and what Analysis Services did for BI: make it affordable.  With a compelling cost-per-terabyte, an attractive scale-out approach, and an entry point at under $1 million, we&#8217;ll see more small-to-midsized companies implementing data warehousing and business intelligence.  This is good news for those of us looking for an affordable data warehouse solution and for those of us who make our living with SQL Server.  And for those of you who might suggest that few companies need a datawarehouse that can support multi-terabyte data, I&#8217;d like to point out that just 3 or 4 years ago, 100 GB was considered a lot of data.  </p>
<p>I spent most of my week digging into the PDW architecture.  It&#8217;s not all roses&#8211;it&#8217;s a first release and, as such, is immature compared to the much older and more established data warehouse systems&#8211;but again, it has a lot going for it, not least of all it&#8217;s easy integration within a SQL Server environment and the relatively low cost.  We&#8217;re currently investigating this as a possible data warehouse solution for our business intelligence environment, so expect to see more from me about PDW as I learn more about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/11/yet-another-pass-summit-recap/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Not attending PASS Summit? Watch LIVE streaming events FOR FREE!</title>
		<link>http://sqlfool.com/2010/10/not-attending-pass-summit/</link>
		<comments>http://sqlfool.com/2010/10/not-attending-pass-summit/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 19:10:13 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[PASS]]></category>
		<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Syndication]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1396</guid>
		<description><![CDATA[If you&#8217;ve not yet heard, the annual PASS Summit is less than 2 weeks away. This is the largest SQL Server and Business Intelligence conference _in the world_, sponsored by Microsoft and Dell. The return on investment of attending this conference is pretty huge, and I highly recommend you attend if you can swing it. [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve not yet heard, the <a href="http://www.sqlpass.org/summit/na2010/" target="_blank">annual PASS Summit</a> is less than 2 weeks away.  This is the largest SQL Server and Business Intelligence conference _in the world_, sponsored by Microsoft and Dell.  The return on investment of attending this conference is pretty huge, and I highly recommend you attend if you can swing it.  </p>
<p>I am once more fortunate to be attending and presenting at the Summit.  Here&#8217;s where you can find me speaking throughout the week:</p>
<p><strong>Tuesday at 3PM</strong><br />
<a href="http://sqlpass.eventpoint.com/topic/details/LT100T" target="_blank">Lightning Talk &#8211; Page Compression</a><br />
This year, PASS has decided to try something new.  A daily Lightning Talk session will be held where speakers present for 5 quick minutes on interesting SQL topics.  I&#8217;ll be presenting on Tuesday with 6 amazingly talented speakers.  My topic is page compression &#8212; what is it, how to do it, and (most importantly, of course) how it affects performance.</p>
<p><strong>Wednesday at 11:30am in the ballroom</strong><br />
<a href="http://www.sqlpass.org/summit/na2010/Connect/SpecialEvents.aspx#WITLuncheon" target="_blank">Women-In-Technology (WIT) Luncheon</a><br />
I&#8217;ll be speaking on this year&#8217;s WIT luncheon panel, which is sponsored by <a href="http://GoDaddy.com/jobs">GoDaddy.com</a>.  Contrary to common misconception, the luncheon is NOT just for women.  In fact, men are encouraged to attend!  If memory serves, last year&#8217;s luncheon had about 300 attendees, with a good mix of both genders.  This year&#8217;s topic is focused on the recruitment, retention, and advancement of WIT.  If you&#8217;re worried that this event will end up being a feminist bitch-fest, rest assured that&#8217;s most definitely not the case.  I&#8217;ve always found the WIT events I&#8217;ve attended to be informative and thought-provoking.  Plus, free lunch!  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Thursday at 2:30PM (room 3AB)</strong><br />
<a href="http://sqlpass.eventpoint.com/topic/details/DBA248" target="_blank">Heaps of Trouble, Clusters of Glory &#8211; A Look At Index Internals</a><br />
 You can click the link above to read my abstract, but in short, I&#8217;ll be taking attendees on a journey through indexes.  You&#8217;ll come away with a much better understanding of the internal structures of indexes, which should help DBA&#8217;s with database design and performance tuning.</p>
<p>If you&#8217;re not able to attend in person, Summit does sell DVD&#8217;s of the event afterwards, which are well worth the investment.  But this year, to make the event more accessible to the community, PASS and Dell have teamed up to present live streaming of the keynotes and WIT luncheon sessions.  </p>
<p>Here&#8217;s details of the keynotes from the PASS press release:<br />
<em><br />
Ted Kummert, Senior Vice President of the Business Platform Division at Microsoft Corp., will kick off PASS Summit on November 9 by highlighting the continued innovation across Microsoft’s business and information platform. Kummert will explore Microsoft’s key technical investments, as well as Mission Critical applications and the accessibility of Business Intelligence.</p>
<p>Quentin Clark, General Manager of Database Systems Group at Microsoft Corp., will showcase the next version of SQL Server on November 10 and will share how features in this upcoming product milestone continue to deliver on Microsoft’s Information Platform vision. Clark will also demonstrate how developers can leverage new industry-leading tools with powerful features for data developers and a unified database development experience.</p>
<p>David DeWitt, Technical Fellow, Data and Storage Platform Division at Microsoft Corp., will be discussing SQL query optimization and address why it is difficult to always execute good plans in his highly anticipated technical keynote. DeWitt will also cover new technologies that offer the promise of better plans in future releases of SQL Server.</em></p>
<p>While all of the keynotes are interesting and definitely worth watching, I cannot recommend the David DeWitt keynote more highly.  His keynote last Summit was outstanding.  It was technical, thought provoking, and one of the best things of last year&#8217;s Summit.  </p>
<p>You can find more information and register for the PASS Summit 2010 live streaming events at their website, <a href="http://www.sqlpass.org/LiveKeynotes" target="_blank">www.sqlpass.org/LiveKeynotes</a></p>
<p>If you ARE attending Summit, make sure to swing by and say &#8220;hi&#8221; or <a href="http://twitter.com/sqlfool" target="_blank">message me via Twitter</a> to see if there&#8217;s a time we can meet up.  Meeting people is one of my favorite things about Summit.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/10/not-attending-pass-summit/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

