<?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; SQL Tips</title>
	<atom:link href="http://sqlfool.com/category/tips-suggestions/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>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>Calculate Rows Inserted per Second for All Tables</title>
		<link>http://sqlfool.com/2011/07/calculate-rows-inserted-per-second/</link>
		<comments>http://sqlfool.com/2011/07/calculate-rows-inserted-per-second/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 21:24:06 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<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[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1565</guid>
		<description><![CDATA[Ever needed to calculate the number of rows inserted every second, for every table in every database on a server? Or, have you ever needed to validate that all processes have stopped writing to tables? These types of questions come up routinely for me. To help with this, I&#8217;ve written the following script, which examines [...]]]></description>
			<content:encoded><![CDATA[<p>Ever needed to calculate the number of rows inserted every second, for every table in every database on a server?  Or, have you ever needed to validate that all processes have stopped writing to tables?  These types of questions come up routinely for me.  To help with this, I&#8217;ve written the following script, which examines metadata values using sys.partitions.  This method isn&#8217;t as accurate as running SELECT COUNT(*) FROM, but it&#8217;s much faster.  Keep in mind, since it&#8217;s just looking at row counts, it&#8217;s not much help on tables that have a lot of update/delete activity.  But it does what I need it to do, and I use it pretty regularly, so I thought I&#8217;d share in case anyone else can benefit from it too.  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Declare Parameters */</span>
<span style="color: #0000FF;">DECLARE</span> @newBaseline <span style="color: #0000FF;">BIT</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #008080;">-- change to 0 when you don't want to replace the baseline, i.e. after initial run</span>
  , @delay <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'00:00:30'</span>; <span style="color: #008080;">-- change as needed</span>
&nbsp;
<span style="color: #0000FF;">IF</span> @newBaseline <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
<span style="color: #0000FF;">BEGIN</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..#baseline'</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> #baseline;
&nbsp;
    <span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #baseline
    <span style="color: #808080;">&#40;</span>
         database_name  SYSNAME
       , table_name     SYSNAME
       , table_rows     <span style="color: #0000FF;">BIGINT</span>
       , captureTime    <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NULL</span>
    <span style="color: #808080;">&#41;</span>;
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'tempdb..#current'</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> #current;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #current
<span style="color: #808080;">&#40;</span>
     database_name  SYSNAME
   , table_name     SYSNAME
   , table_rows     <span style="color: #0000FF;">BIGINT</span>
   , captureTime    <span style="color: #0000FF;">DATETIME</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">IF</span> @newBaseline <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
<span style="color: #0000FF;">BEGIN</span>
    <span style="color: #0000FF;">EXECUTE</span> sp_MSforeachdb <span style="color: #FF0000;">'USE ?; 
        INSERT INTO #baseline
        SELECT DB_NAME()
            , o.name As [tableName]
            , SUM(p.[rows]) As [rowCnt]
            , GETDATE() As [captureTime]
        FROM sys.indexes As i
        JOIN sys.partitions As p
            ON i.[object_id] = p.[object_id]
           AND i.index_id  = p.index_id
        JOIN sys.objects As o
            ON i.[object_id] = o.[object_id]
        WHERE i.[type] = 1
        GROUP BY o.name;'</span>
&nbsp;
    <span style="color: #0000FF;">WAITFOR</span> DELAY @delay;
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">EXECUTE</span> sp_MSforeachdb <span style="color: #FF0000;">'USE ?; 
INSERT INTO #current
SELECT DB_NAME()
    , o.name As [tableName]
    , SUM(p.[rows]) As [rowCnt]
    , GETDATE() As [captureTime]
FROM sys.indexes As i
JOIN sys.partitions As p
    ON i.[object_id] = p.[object_id]
   AND i.index_id  = p.index_id
JOIN sys.objects As o
    ON i.[object_id] = o.[object_id]
WHERE i.[type] = 1
GROUP BY o.name;'</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span>  c.<span style="color: #808080;">*</span>
      , c.<span style="color: #202020;">table_rows</span> <span style="color: #808080;">-</span> b.<span style="color: #202020;">table_rows</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'new_rows'</span>
      , <span style="color: #FF00FF;">DATEDIFF</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">second</span>, b.<span style="color: #202020;">captureTime</span>, c.<span style="color: #202020;">captureTime</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'time_diff'</span>
      , <span style="color: #808080;">&#40;</span>c.<span style="color: #202020;">table_rows</span> <span style="color: #808080;">-</span> b.<span style="color: #202020;">table_rows</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #FF00FF;">DATEDIFF</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">second</span>, b.<span style="color: #202020;">captureTime</span>, c.<span style="color: #202020;">captureTime</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'rows_per_sec'</span>
<span style="color: #0000FF;">FROM</span> #baseline <span style="color: #0000FF;">AS</span> b
<span style="color: #808080;">JOIN</span> #current <span style="color: #0000FF;">AS</span> c
    <span style="color: #0000FF;">ON</span> b.<span style="color: #202020;">table_name</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">table_name</span>
   <span style="color: #808080;">AND</span> b.<span style="color: #202020;">database_name</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">database_name</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> new_rows <span style="color: #0000FF;">DESC</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/07/calculate-rows-inserted-per-second/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>T-SQL Script for Estimating Compression Savings</title>
		<link>http://sqlfool.com/2011/06/estimate-compression-savings/</link>
		<comments>http://sqlfool.com/2011/06/estimate-compression-savings/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 14:51:40 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[2008]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[partitioning]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1539</guid>
		<description><![CDATA[A couple of weeks ago, I was working on a Microsoft PDW proof-of-concept (POC) and had to measure compression ratios. In order to do this, I fired up SSMS and wrote a little script. The script will iterate through all tables in a database and run the sp_estimate_data_compression_savings stored procedure. This will only work in [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I was working on a <a href="http://www.microsoft.com/sqlserver/en/us/editions/data-warehouse.aspx" target="_blank">Microsoft PDW</a> proof-of-concept (POC) and had to measure compression ratios.  In order to do this, I fired up SSMS and wrote a little script.  The script will iterate through all tables in a database and run the <a href="http://msdn.microsoft.com/en-us/library/cc280574.aspx" target="_blank">sp_estimate_data_compression_savings</a> stored procedure.  <strong>This will only work in SQL Server 2008+ versions running Enterprise edition</strong>.</p>
<p>If you&#8217;re not familiar with this stored procedure, it basically will tell you what effect PAGE or ROW compression will have on your table/index/partition, etc.  There are pro&#8217;s and con&#8217;s with compression.  What I&#8217;ve tended to see is that compression has very positive results on space, IO, and query duration, with a negative impact on CPU and write speed.  Like most things, it&#8217;s a trade-off and the results will vary by environment, so I recommend you do some testing before you apply compression to all tables.  I tend to use compression mostly for my historical tables and partitions and leave my recent data uncompressed.  And, back to the script, I use this stored procedure to estimate the impact of compression and to determine whether to use PAGE or ROW compression.  PAGE is a higher level of compression, which means it&#8217;s also more expensive in terms of CPU, so if the difference between the two results is negligible, I&#8217;m more apt to just use ROW compression.  </p>
<p>Now that my impromptu compression discussion is done, let&#8217;s get to the actual script.  One final word of caution, however. <strong>This is an IO intensive process</strong>, so you may want to run it after peak business hours.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @printOnly  <span style="color: #0000FF;">BIT</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #008080;">-- change to 1 if you don't want to execute, just print commands</span>
    , @tableName    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>
    , @schemaName   <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
    , @sqlStatement <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>
    , @tableCount   <span style="color: #0000FF;">INT</span>
    , @statusMsg    <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">IF</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> tempdb.<span style="color: #202020;">sys</span>.<span style="color: #202020;">tables</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%#tables%'</span><span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #tables; 
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #tables
<span style="color: #808080;">&#40;</span>
      database_name     sysname
    , schemaName        sysname <span style="color: #808080;">NULL</span>
    , tableName         sysname <span style="color: #808080;">NULL</span>
    , processed         <span style="color: #0000FF;">bit</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">IF</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> tempdb.<span style="color: #202020;">sys</span>.<span style="color: #202020;">tables</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%#compression%'</span><span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #compressionResults;
&nbsp;
<span style="color: #0000FF;">IF</span> <span style="color: #808080;">NOT</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> tempdb.<span style="color: #202020;">sys</span>.<span style="color: #202020;">tables</span> <span style="color: #0000FF;">WHERE</span> name <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%#compression%'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span> 
&nbsp;
    <span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #compressionResults
    <span style="color: #808080;">&#40;</span>
          objectName                    <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
        , schemaName                    <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>
        , index_id                      <span style="color: #0000FF;">int</span>
        , partition_number              <span style="color: #0000FF;">int</span>
        , size_current_compression      <span style="color: #0000FF;">bigint</span>
        , size_requested_compression    <span style="color: #0000FF;">bigint</span>
        , sample_current_compression    <span style="color: #0000FF;">bigint</span>
        , sample_requested_compression  <span style="color: #0000FF;">bigint</span>
    <span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #tables
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>
    , SCHEMA_NAME<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>schema_id<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
    , name
    , <span style="color: #000;">0</span> <span style="color: #008080;">-- unprocessed</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">tables</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> @tableCount <span style="color: #808080;">=</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> #tables;
&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> #tables <span style="color: #0000FF;">WHERE</span> processed <span style="color: #808080;">=</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> @tableName <span style="color: #808080;">=</span> tableName
        , @schemaName <span style="color: #808080;">=</span> schemaName
    <span style="color: #0000FF;">FROM</span> #tables <span style="color: #0000FF;">WHERE</span> processed <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
&nbsp;
    <span style="color: #0000FF;">SELECT</span> @statusMsg <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Working on '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>@tableCount <span style="color: #808080;">-</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</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;">&#41;</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF0000;">' of '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@tableCount <span style="color: #0000FF;">AS</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;">&#41;</span>
    <span style="color: #0000FF;">FROM</span> #tables
    <span style="color: #0000FF;">WHERE</span> processed <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
&nbsp;
    <span style="color: #0000FF;">RAISERROR</span><span style="color: #808080;">&#40;</span>@statusMsg, <span style="color: #000;">0</span>, <span style="color: #000;">42</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">WITH</span> NOWAIT;
&nbsp;
    <span style="color: #0000FF;">SET</span> @sqlStatement <span style="color: #808080;">=</span> <span style="color: #FF0000;">'EXECUTE sp_estimate_data_compression_savings '</span><span style="color: #FF0000;">''</span> 
                        <span style="color: #808080;">+</span> @schemaName <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @tableName <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', NULL, NULL, '</span><span style="color: #FF0000;">'PAGE'</span><span style="color: #FF0000;">';'</span> <span style="color: #008080;">-- ROW, PAGE, or NONE</span>
&nbsp;
    <span style="color: #0000FF;">IF</span> @printOnly <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    <span style="color: #0000FF;">BEGIN</span> 
&nbsp;
        <span style="color: #0000FF;">SELECT</span> @sqlStatement;
&nbsp;
    <span style="color: #0000FF;">END</span>
    <span style="color: #0000FF;">ELSE</span>
    <span style="color: #0000FF;">BEGIN</span>
&nbsp;
        <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #compressionResults
        <span style="color: #0000FF;">EXECUTE</span> <span style="color: #AF0000;">sp_executesql</span> @sqlStatement;
&nbsp;
    <span style="color: #0000FF;">END</span>;
&nbsp;
    <span style="color: #0000FF;">UPDATE</span> #tables
    <span style="color: #0000FF;">SET</span> processed <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
    <span style="color: #0000FF;">WHERE</span> tableName <span style="color: #808080;">=</span> @tableName
        <span style="color: #808080;">AND</span> schemaName <span style="color: #808080;">=</span> @schemaName;
&nbsp;
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> #compressionResults;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2011/06/estimate-compression-savings/feed/</wfw:commentRss>
		<slash:comments>7</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>SQL Agent Job Script</title>
		<link>http://sqlfool.com/2010/12/sql-agent-job-script/</link>
		<comments>http://sqlfool.com/2010/12/sql-agent-job-script/#comments</comments>
		<pubDate>Tue, 21 Dec 2010 00:00:28 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1457</guid>
		<description><![CDATA[This blog post is brought to you by the awesomeness that is the SQL Server Twitter community. Contributions were made by several awesome Tweeples, including Denny Cherry, Kendra Little, Ken Simmons, and Magnus Ahlkvist, among others. What started this is something you&#8217;ve all probably run into at one time or another. We&#8217;re currently having some [...]]]></description>
			<content:encoded><![CDATA[<p>This blog post is brought to you by the awesomeness that is the SQL Server Twitter community.  Contributions were made by several awesome Tweeples, including <a href="http://www.twitter.com/mrdenny" target="_blank">Denny Cherry</a>, <a href="http://www.twitter.com/Kendra_Little" target="_blank">Kendra Little</a>, <a href="http://www.twitter.com/KenSimmons" target="_blank">Ken Simmons</a>, and <a href="http://www.twitter.com/MagnusAhlkvist" target="_blank">Magnus Ahlkvist</a>, among others.  </p>
<p>What started this is something you&#8217;ve all probably run into at one time or another.  We&#8217;re currently having some resource contention on our server when a ton of processes kickoff and overlap in the morning.  Now, <a href="http://sqlfool.com/2009/02/view-sql-agent-jobs-tsql/" target="_blank">I have a script that I&#8217;ve used in the past</a> for monitoring SQL agent jobs, but this time I wanted to add some historical run-time information.  I know the sysjobhistory table contains the information I need, but it has some&#8230; let&#8217;s say, creative ways of storing the data.  Opinions on the reasons why vary:<br />
<br />
<a href="http://sqlfool.com/wp-content/uploads/2010/12/tweets.jpg"><img src="http://sqlfool.com/wp-content/uploads/2010/12/tweets-187x300.jpg" alt="" title="tweets" width="187" height="300" class="aligncenter size-medium wp-image-1458" /></a><br />
<br />
Regardless the reason, I needed to do some conversions. <a href="http://itknowledgeexchange.techtarget.com/sql-server/" target="_blank">Denny</a> shared with us the msdb.dbo.agent_datetime function to convert run_date and run_time into an actual datetime value.  I have to say, this certainly cleans up the code quite a bit!  Then Magnus shared a method to convert run_duration into seconds, which he modified from a script on <a href="http://www.sqlservercentral.com/Forums/Topic206218-5-1.aspx" target="_blank">SQLServerCentral</a>.  I was able to use these two tidbits to update my old script.  You can now run this script to get back a list of all enabled procs, a script that will disable them, a breakdown of the schedule, and a historical run-time average.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/*  How many days do you want to include in your run-time average?
    Recent values tend to be more useful. */</span>
<span style="color: #0000FF;">Declare</span> @daysToAverage <span style="color: #0000FF;">smallint</span> <span style="color: #808080;">=</span> <span style="color: #000;">30</span>;
&nbsp;
<span style="color: #0000FF;">Declare</span> @avgRunTime <span style="color: #0000FF;">Table</span>
<span style="color: #808080;">&#40;</span>
      job_id      <span style="color: #0000FF;">uniqueidentifier</span>
    , avgRunTime  <span style="color: #0000FF;">int</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">/* We need to parse the schedule into something we can understand */</span>
<span style="color: #0000FF;">Declare</span> @weekDay <span style="color: #0000FF;">Table</span> <span style="color: #808080;">&#40;</span>
      mask       <span style="color: #0000FF;">int</span>
    , maskValue  <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">32</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> @weekDay
<span style="color: #0000FF;">Select</span> <span style="color: #000;">1</span>, <span style="color: #FF0000;">'Sunday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">2</span>, <span style="color: #FF0000;">'Monday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">4</span>, <span style="color: #FF0000;">'Tuesday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">8</span>, <span style="color: #FF0000;">'Wednesday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">16</span>, <span style="color: #FF0000;">'Thursday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">32</span>, <span style="color: #FF0000;">'Friday'</span>  <span style="color: #0000FF;">Union</span> All
<span style="color: #0000FF;">Select</span> <span style="color: #000;">64</span>, <span style="color: #FF0000;">'Saturday'</span>;
&nbsp;
<span style="color: #008080;">/* First, let's get our run-time average */</span>
<span style="color: #0000FF;">Insert</span> <span style="color: #0000FF;">Into</span> @avgRunTime
<span style="color: #0000FF;">Select</span> job_id
    , <span style="color: #FF00FF;">Avg</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>run_duration<span style="color: #808080;">/</span><span style="color: #000;">10000</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">*</span> <span style="color: #000;">3600</span> <span style="color: #808080;">+</span> <span style="color: #808080;">&#40;</span>run_duration<span style="color: #808080;">/</span><span style="color: #000;">100</span><span style="color: #808080;">%</span>100<span style="color: #808080;">&#41;</span><span style="color: #808080;">*</span><span style="color: #000;">60</span> <span style="color: #808080;">+</span> run_duration<span style="color: #808080;">%</span>100<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'avgRunTime'</span> <span style="color: #008080;">/* convert HHMMSS to seconds */</span>
<span style="color: #0000FF;">From</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobhistory</span>
<span style="color: #0000FF;">Where</span> step_id <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #008080;">-- only grab our total run-time</span>
    And run_status <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #008080;">-- only grab successful executions</span>
    And msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">agent_datetime</span><span style="color: #808080;">&#40;</span>run_date, run_time<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;=</span> <span style="color: #FF00FF;">DateAdd</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">day</span>, <span style="color: #808080;">-</span>@daysToAverage, <span style="color: #FF00FF;">GetDate</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">Group</span> <span style="color: #0000FF;">By</span> job_id;
&nbsp;
<span style="color: #008080;">/* Now let's get our schedule information */</span>
<span style="color: #0000FF;">With</span> myCTE
<span style="color: #0000FF;">As</span><span style="color: #808080;">&#40;</span>
    <span style="color: #0000FF;">Select</span> sched.<span style="color: #202020;">name</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'scheduleName'</span>
        , sched.<span style="color: #202020;">schedule_id</span>
        , jobsched.<span style="color: #202020;">job_id</span>
        , <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Once'</span> 
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> 
                And sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
                    <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Daily'</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> 
                <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Every '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_interval</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' days'</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">Then</span> 
                <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">Replace</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span> 
                    <span style="color: #0000FF;">Select</span> maskValue 
                    <span style="color: #0000FF;">From</span> @weekDay <span style="color: #0000FF;">As</span> x 
                    <span style="color: #0000FF;">Where</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">&amp;</span> x.<span style="color: #202020;">mask</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                    <span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> mask <span style="color: #0000FF;">For</span> XML Raw<span style="color: #808080;">&#41;</span>
                , <span style="color: #FF0000;">'&quot;/&gt;&lt;row maskValue=&quot;'</span>, <span style="color: #FF0000;">', '</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&lt;row maskValue=&quot;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'&quot;/&gt;'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                        And sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> 
                            <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'; weekly'</span> 
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'; every '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">As</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;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' weeks'</span> <span style="color: #0000FF;">End</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">16</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'On day '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_interval</span> <span style="color: #0000FF;">As</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;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' of every '</span>
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">As</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;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' months'</span> 
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">32</span> <span style="color: #0000FF;">Then</span> 
                <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'First'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Second'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Third'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Fourth'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_relative_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">16</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Last'</span>
                <span style="color: #0000FF;">End</span> <span style="color: #808080;">+</span> 
                <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Sunday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Monday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">3</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Tuesday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Wednesday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">5</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Thursday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">6</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Friday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">7</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Saturday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Day'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">9</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Weekday'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_interval</span> <span style="color: #808080;">=</span> <span style="color: #000;">10</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' Weekend'</span>
                <span style="color: #0000FF;">End</span>
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> 
                        And sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'; monthly'</span>
                    <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'; every '</span> 
                <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_recurrence_factor</span> <span style="color: #0000FF;">As</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;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' months'</span> <span style="color: #0000FF;">End</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">64</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'StartUp'</span>
            <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">128</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Idle'</span>
          <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'frequency'</span>
        , IsNull<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Every '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">freq_subday_interval</span> <span style="color: #0000FF;">As</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;">&#41;</span> <span style="color: #808080;">+</span> 
            <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">2</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' seconds'</span>
                <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">4</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' minutes'</span>
                <span style="color: #0000FF;">When</span> sched.<span style="color: #202020;">freq_subday_type</span> <span style="color: #808080;">=</span> <span style="color: #000;">8</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">' hours'</span>
            <span style="color: #0000FF;">End</span>, <span style="color: #FF0000;">'Once'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'subFrequency'</span>
        , <span style="color: #FF00FF;">Replicate</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">Len</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_start_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_start_time</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'startTime'</span>
        , <span style="color: #FF00FF;">Replicate</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">Len</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_end_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">active_end_time</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'endTime'</span>
        , <span style="color: #FF00FF;">Replicate</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'0'</span>, <span style="color: #000;">6</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">Len</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_time</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> 
            <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_time</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'nextRunTime'</span>
        , <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>jobsched.<span style="color: #202020;">next_run_date</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'nextRunDate'</span>
    <span style="color: #0000FF;">From</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysschedules</span> <span style="color: #0000FF;">As</span> sched
    Join msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobschedules</span> <span style="color: #0000FF;">As</span> jobsched
        <span style="color: #0000FF;">On</span> sched.<span style="color: #202020;">schedule_id</span> <span style="color: #808080;">=</span> jobsched.<span style="color: #202020;">schedule_id</span>
    <span style="color: #0000FF;">Where</span> sched.<span style="color: #202020;">enabled</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">/* Finally, let's look at our actual jobs and tie it all together */</span>
<span style="color: #0000FF;">Select</span> job.<span style="color: #202020;">name</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'jobName'</span>
    , sched.<span style="color: #202020;">scheduleName</span>
    , sched.<span style="color: #202020;">frequency</span>
    , sched.<span style="color: #202020;">subFrequency</span>
    , <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">startTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">startTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' - '</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">endTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">endTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> 
        <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'scheduleTime'</span> <span style="color: #008080;">-- HH:MM</span>
    , <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">1</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">5</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'/'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunDate</span>, <span style="color: #000;">7</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' '</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunTime</span>, <span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">':'</span> 
        <span style="color: #808080;">+</span> <span style="color: #FF00FF;">SubString</span><span style="color: #808080;">&#40;</span>sched.<span style="color: #202020;">nextRunTime</span>, <span style="color: #000;">3</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'nextRunDate'</span>
      <span style="color: #008080;">/* Note: the sysjobschedules table refreshes every 20 min, 
        so nextRunDate may be out of date */</span>
    , <span style="color: #FF0000;">'Execute msdb.dbo.sp_update_job @job_id = '</span><span style="color: #FF0000;">''</span> 
        <span style="color: #808080;">+</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span>job.<span style="color: #202020;">job_id</span> <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', @enabled = 0;'</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'disableScript'</span>
    , art.<span style="color: #202020;">avgRunTime</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'avgRunTime_inSec'</span> <span style="color: #008080;">-- in seconds</span>
    , <span style="color: #808080;">&#40;</span>art.<span style="color: #202020;">avgRunTime</span> <span style="color: #808080;">/</span> <span style="color: #000;">60</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'avgRunTime_inMin'</span> <span style="color: #008080;">-- convert to minutes</span>
<span style="color: #0000FF;">From</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobs</span> <span style="color: #0000FF;">As</span> job
Join myCTE <span style="color: #0000FF;">As</span> sched
    <span style="color: #0000FF;">On</span> job.<span style="color: #202020;">job_id</span> <span style="color: #808080;">=</span> sched.<span style="color: #202020;">job_id</span>
<span style="color: #0000FF;">Left</span> Join @avgRunTime <span style="color: #0000FF;">As</span> art
    <span style="color: #0000FF;">On</span> job.<span style="color: #202020;">job_id</span> <span style="color: #808080;">=</span> art.<span style="color: #202020;">job_id</span>
<span style="color: #0000FF;">Where</span> job.<span style="color: #202020;">enabled</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #008080;">-- do not display disabled jobs</span>
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> nextRunDate;</pre></div></div>

<p>If this doesn&#8217;t do exactly what you want, check out <a href="http://sqlsoft.co.uk/sqljobvis.php" target="_blank">SQLJobVis</a>, which Ken recommended.  It&#8217;s a free tool that helps visualize the job history of SQL jobs.  </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/12/sql-agent-job-script/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Metadata for Table Valued Parameters</title>
		<link>http://sqlfool.com/2010/10/metadata-for-table-valued-parameters/</link>
		<comments>http://sqlfool.com/2010/10/metadata-for-table-valued-parameters/#comments</comments>
		<pubDate>Thu, 28 Oct 2010 15:10:27 +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>

		<guid isPermaLink="false">http://sqlfool.com/?p=1390</guid>
		<description><![CDATA[Table-valued parameters (TVP) are a great feature that was new in SQL Server 2008 that allow you to insert a dataset into a table. Previously, the most common way of doing this was by passing and parsing XML. As I&#8217;ve previously posted, TVP&#8217;s perform an astounding 94% faster than singleton inserts and 75% faster than [...]]]></description>
			<content:encoded><![CDATA[<p>Table-valued parameters (TVP) are a great feature that was new in SQL Server 2008 that allow you to insert a dataset into a table.  Previously, the most common way of doing this was by passing and parsing XML.  As I&#8217;ve <a href="http://sqlfool.com/2008/11/performance-comparison-of-singleton-xml-and-tvp-inserts/" target="_blank">previously posted, </a> TVP&#8217;s perform an astounding 94% faster than singleton inserts and 75% faster than XML inserts.  But for some reason, TVP&#8217;s still aren&#8217;t widely used and understood.  In this post, I&#8217;ll walk you through how to use these and how to query the metadata for TVP&#8217;s.  </p>
<p>I&#8217;ve <a href="http://sqlfool.com/2008/11/one-to-many-inserts-with-table-valued-parameters/" target="_blank">previously posted about what TVP&#8217;s are</a> and how to use them.  But in honor of <a href="http://sears.com/zombies" target="_blank">Halloween this week</a>, I&#8217;ve updated my demo script:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Create some tables to work with */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">orders</span>
<span style="color: #808080;">&#40;</span>
      order_id      <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>   Not Null
    , orderDate     <span style="color: #0000FF;">DATE</span>                Not Null
    , customer      <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>         Not Null
&nbsp;
    <span style="color: #0000FF;">CONSTRAINT</span> PK_orders
        <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span><span style="color: #808080;">&#40;</span>order_id<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">orderDetails</span>
<span style="color: #808080;">&#40;</span>
      orderDetail_id    <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>   Not Null
    , order_id          <span style="color: #0000FF;">INT</span>                 Not Null
    , lineItem          <span style="color: #0000FF;">INT</span>                 Not Null
    , product           <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>         Not Null
&nbsp;
    <span style="color: #0000FF;">CONSTRAINT</span> PK_orderDetails
        <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span><span style="color: #808080;">&#40;</span>orderDetail_id<span style="color: #808080;">&#41;</span>
&nbsp;
    <span style="color: #0000FF;">CONSTRAINT</span> FK_orderDetails_orderID
        <span style="color: #0000FF;">FOREIGN</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#40;</span>order_id<span style="color: #808080;">&#41;</span>
        <span style="color: #0000FF;">REFERENCES</span> dbo.<span style="color: #202020;">orders</span><span style="color: #808080;">&#40;</span>order_id<span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">/* Create our new table types */</span>
<span style="color: #0000FF;">CREATE</span> TYPE dbo.<span style="color: #202020;">orderTable</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span> 
<span style="color: #808080;">&#40;</span> 
      orderDate     <span style="color: #0000FF;">DATE</span>
    , customer      <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> TYPE dbo.<span style="color: #202020;">orderDetailTable</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span> 
<span style="color: #808080;">&#40;</span> 
      lineItem      <span style="color: #0000FF;">INT</span>
    , product       <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
&nbsp;
<span style="color: #008080;">/* Create a new procedure using a table-valued parameter */</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> dbo.<span style="color: #202020;">insert_orderTVP_sp</span>
      @myOrderTable orderTable READONLY
    , @myOrderDetailTable orderDetailTable READONLY
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
    <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;
&nbsp;
    <span style="color: #0000FF;">DECLARE</span> @myOrderID <span style="color: #0000FF;">INT</span>;
&nbsp;
    <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">orders</span>
    <span style="color: #0000FF;">SELECT</span> orderDate
        , customer
    <span style="color: #0000FF;">FROM</span> @myOrderTable;
&nbsp;
    <span style="color: #0000FF;">SET</span> @myOrderID <span style="color: #808080;">=</span> <span style="color: #FF00FF;">SCOPE_IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
    <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">orderDetails</span>
    <span style="color: #0000FF;">SELECT</span> @myOrderID
        , lineItem
        , product
    <span style="color: #0000FF;">FROM</span> @myOrderDetailTable;
&nbsp;
    <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">OFF</span>;
&nbsp;
<span style="color: #0000FF;">END</span>
GO
&nbsp;
&nbsp;
<span style="color: #008080;">/* Call our new proc! */</span>
<span style="color: #0000FF;">DECLARE</span> @myTableHeaderData <span style="color: #0000FF;">AS</span> orderTable
    , @myTableDetailData <span style="color: #0000FF;">AS</span> orderDetailTable;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myTableHeaderData
<span style="color: #808080;">&#40;</span>orderDate, customer<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'Zombie'</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myTableDetailData
<span style="color: #808080;">&#40;</span>lineItem, product<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">10</span>, <span style="color: #FF0000;">'Brains'</span> <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">20</span>, <span style="color: #FF0000;">'More Brains'</span>;
&nbsp;
<span style="color: #0000FF;">EXECUTE</span> dbo.<span style="color: #202020;">insert_orderTVP_sp</span> 
      @myTableHeaderData
    , @myTableDetailData;
&nbsp;
<span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @myTableHeaderData;
<span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @myTableDetailData;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myTableHeaderData
<span style="color: #808080;">&#40;</span>orderDate, customer<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'Vampire'</span><span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @myTableDetailData
<span style="color: #808080;">&#40;</span>lineItem, product<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">10</span>, <span style="color: #FF0000;">'Blood Type O+'</span> <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">20</span>, <span style="color: #FF0000;">'Blood Type B-'</span> <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">30</span>, <span style="color: #FF0000;">'Blood Type AB+'</span> <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">40</span>, <span style="color: #FF0000;">'Blood Type A+'</span>;
&nbsp;
<span style="color: #0000FF;">EXECUTE</span> dbo.<span style="color: #202020;">insert_orderTVP_sp</span> 
      @myTableHeaderData
    , @myTableDetailData;
&nbsp;
&nbsp;
<span style="color: #008080;">/* Check our data */</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">orders</span>;
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">orderDetails</span>;</pre></div></div>

<p>Once you&#8217;ve run this, you should see the following data:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">order_id    orderDate  customer
----------- ---------- --------------------
1           2010-10-28 Zombie
2           2010-10-28 Vampire
&nbsp;
(2 row(s) affected)
&nbsp;
orderDetail_id order_id    lineItem    product
-------------- ----------- ----------- --------------------
1              1           10          Brains
2              1           20          More Brains
3              2           10          Blood Type O+
4              2           20          Blood Type B-
5              2           30          Blood Type AB+
6              2           40          Blood Type A+
&nbsp;
(6 row(s) affected)</pre></div></div>

<p>Now that we&#8217;ve successfully created a couple of table types to support our TVP&#8217;s, how do we go back and find out which objects we&#8217;ve created?  You can query the sys.types catalog view to find out.  Just search for system_type_id 243, which identifies the record as a table type.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/* Let's check out our new data types */</span>
<span style="color: #0000FF;">SELECT</span> name
    , system_type_id
    , is_table_type
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">types</span>
<span style="color: #0000FF;">WHERE</span> system_type_id <span style="color: #808080;">=</span> <span style="color: #000;">243</span>;
GO</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">name                 system_type_id is_table_type
-------------------- -------------- -------------
orderTable           243            1
orderDetailTable     243            1
&nbsp;
(2 row(s) affected)</pre></div></div>

<p>Even better yet, you can use the sys.table_types catalog view.  This gives us the same information as sys.types but also gives us the type_table_object_id, which we&#8217;ll need shortly.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> name
    , system_type_id
    , is_table_type
    , type_table_object_id
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">table_types</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">name                 system_type_id is_table_type type_table_object_id
-------------------- -------------- ------------- --------------------
orderTable           243            1             917578307
orderDetailTable     243            1             933578364
&nbsp;
(2 row(s) affected)</pre></div></div>

<p>What if you need to look up the table type definition?  You can do this using the type_table_object_id and joining to sys.columns.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> tt.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'table_type_name'</span>
    , c.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'column_name'</span>
    , t.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #FF0000;">'data_type'</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">table_types</span> <span style="color: #0000FF;">AS</span> tt
<span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">columns</span> <span style="color: #0000FF;">AS</span> c
    <span style="color: #0000FF;">ON</span> tt.<span style="color: #202020;">type_table_object_id</span> <span style="color: #808080;">=</span> c.<span style="color: #FF00FF;">object_id</span>
<span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">types</span> <span style="color: #0000FF;">As</span> t
    <span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">system_type_id</span> <span style="color: #808080;">=</span> t.<span style="color: #202020;">system_type_id</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">table_type_name      column_name     data_type
-------------------- --------------- ---------------
orderTable           orderDate       date
orderDetailTable     lineItem        int
orderTable           customer        varchar
orderDetailTable     product         varchar
&nbsp;
(4 row(s) affected)</pre></div></div>

<p>And last, but certainly not least, how do we see if any procs are currently using the table types?  SQL Server 2008 makes this easy for us with the sys.dm_sql_referencing_entities DMV.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> referencing_schema_name, referencing_entity_name, referencing_id
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_sql_referencing_entities</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'dbo.orderTable'</span>, <span style="color: #FF0000;">'TYPE'</span><span style="color: #808080;">&#41;</span>;</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">referencing_schema_name referencing_entity_name referencing_id
----------------------- ----------------------- --------------
dbo                     insert_orderTVP_sp      949578421
&nbsp;
(1 row(s) affected)</pre></div></div>

<p>If you&#8217;re wondering how to implement SQL Server TVP&#8217;s in your .NET code, well&#8230; I can&#8217;t tell you how to do it, but I can point you to a place that can.  Stephen Forte has <a href="http://www.stephenforte.net/PermaLink,guid,07dfeb00-d0b0-47dd-9761-3b4c9f160277.aspx" target="_blank">a post that explains how easy it is to do</a>.</p>
<p>So now that you have a better understanding of how to work with TVP&#8217;s, why don&#8217;t you go implement one in your environment and see how for yourself just how awesome it is?  <img src='http://sqlfool.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Oh, and Happy Halloween!</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2010/10/metadata-for-table-valued-parameters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rename Database Objects En Masse</title>
		<link>http://sqlfool.com/2010/09/rename-database-objects-en-masse/</link>
		<comments>http://sqlfool.com/2010/09/rename-database-objects-en-masse/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 15:55:22 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[T-SQL Scripts]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=1375</guid>
		<description><![CDATA[Ever need to rename all objects in a database? Yeah, okay&#8230; it doesn&#8217;t happen very often, but when it does, it can be time consuming. This recently came up as something I needed to do. When you consider all the tables involved and you add in defaults, indexes, and foreign keys, well&#8230; you can imagine [...]]]></description>
			<content:encoded><![CDATA[<p>Ever need to rename all objects in a database?  Yeah, okay&#8230; it doesn&#8217;t happen very often, but when it does, it can be time consuming.  This recently came up as something I needed to do.  When you consider all the tables involved and you add in defaults, indexes, and foreign keys, well&#8230; you can imagine how the number of objects adds up quickly.  After doing a few tedious renames, it occurred to me that I could write a script to generate the rename scripts for me.  Work smarter, not harder, right?  For anyone interested, here&#8217;s the script.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Select</span> name
    , <span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#93;</span> 
    , <span style="color: #0000FF;">Case</span> 
        <span style="color: #0000FF;">When</span> <span style="color: #808080;">&#91;</span>type<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'U'</span> <span style="color: #0000FF;">Then</span> <span style="color: #FF0000;">'Execute sp_rename N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'old_'</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: #0000FF;">When</span> <span style="color: #808080;">&#91;</span>type<span style="color: #808080;">&#93;</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'D'</span>, <span style="color: #FF0000;">'PK'</span>, <span style="color: #FF0000;">'F'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">Then</span> 
            <span style="color: #FF0000;">'Execute sp_rename N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'old_'</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'OBJECT'</span><span style="color: #FF0000;">''</span>
        <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'renameScript'</span>
    , <span style="color: #0000FF;">Case</span> <span style="color: #0000FF;">When</span> parent_object_id <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Then</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">Else</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">End</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'sortOrder'</span>
<span style="color: #0000FF;">From</span> sys.<span style="color: #202020;">objects</span>
<span style="color: #0000FF;">Where</span> <span style="color: #808080;">&#91;</span>type<span style="color: #808080;">&#93;</span> In <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'D'</span>, <span style="color: #FF0000;">'PK'</span>, <span style="color: #FF0000;">'U'</span>, <span style="color: #FF0000;">'F'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">Union</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">Select</span> i.<span style="color: #202020;">name</span>
    , o.<span style="color: #808080;">&#91;</span><span style="color: #FF00FF;">object_id</span><span style="color: #808080;">&#93;</span>
    , <span style="color: #FF0000;">'Execute sp_rename N'</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> o.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span> <span style="color: #808080;">+</span> i.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'old_'</span> <span style="color: #808080;">+</span> i.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', N'</span><span style="color: #FF0000;">'INDEX'</span><span style="color: #FF0000;">''</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'renameScript'</span>
    , <span style="color: #000;">0</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'sortOrder'</span>
<span style="color: #0000FF;">From</span> sys.<span style="color: #202020;">indexes</span> <span style="color: #0000FF;">As</span> i
<span style="color: #808080;">JOIN</span> sys.<span style="color: #202020;">objects</span> <span style="color: #0000FF;">As</span> o
    <span style="color: #0000FF;">On</span> i.<span style="color: #FF00FF;">object_id</span> <span style="color: #808080;">=</span> o.<span style="color: #FF00FF;">object_id</span>
<span style="color: #0000FF;">Where</span> i.<span style="color: #202020;">is_primary_key</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #008080;">-- exclude PKs, we take care of that above</span>
    <span style="color: #808080;">AND</span> i.<span style="color: #202020;">type</span> <span style="color: #808080;">&lt;&gt;</span> <span style="color: #000;">0</span> <span style="color: #008080;">-- exclude heaps</span>
    <span style="color: #808080;">AND</span> o.<span style="color: #202020;">type</span> Not In <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'S'</span>, <span style="color: #FF0000;">'IT'</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- exclude system &amp; internal tables</span>
<span style="color: #0000FF;">Order</span> <span style="color: #0000FF;">By</span> sortOrder;</pre></div></div>

<p>Be forewarned that I only tested this on a couple of databases, but it seemed to run without problem for tables, indexes, primary keys, defaults, and foreign keys.  The sortOrder column is there only to ensure that table renames are performed last.  Otherwise, your index renames would fail.  This will only run on SQL Server 2005 or 2008.  If you have any problems with the script, please let me know.  <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/09/rename-database-objects-en-masse/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

