<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Dot Net Technologies -- MS Tech talk and more - SQL Tips</title>
    <link>http://www.dotnettechnologies.com/</link>
    <description>Creating Solutions with Microsoft's .Net Technologies</description>
    <language>en-us</language>
    <copyright>© Copyright 2010 </copyright>
    <lastBuildDate>Wed, 04 Aug 2010 04:22:15 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>daryl@rubiconcomputing.com</managingEditor>
    <webMaster>daryl@rubiconcomputing.com</webMaster>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=204ee18d-4d55-41b1-8ad6-9cbbb083cd37</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,204ee18d-4d55-41b1-8ad6-9cbbb083cd37.aspx</pingback:target>
      <dc:creator>Daryl</dc:creator>
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,204ee18d-4d55-41b1-8ad6-9cbbb083cd37.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=204ee18d-4d55-41b1-8ad6-9cbbb083cd37</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">The project I am currently working on is
a port from a horribly over-engineered Oracle system to a much more manageable SQL
Server system. One of the things we needed to do was rename a lot of fields to something
more usable. For instance, every table had a field titled "Ended" but we felt it was
easier to think of that record as "Deleted". Rather than go through every table (and
it wasn't in every table, meaning you needed to manually check each one to verify),
I opted a scripting solution.<br /><br />
Below is a T-SQL script which will generate the sp_rename scripts for all the tables
where the target field is found. A job which would have taken hours and been prone
to errors is now a simple task taking a minute.<br /><br /><pre><span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">DECLARE</span> @OldFieldName <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">VARCHAR</span>(50),
@NewFieldName <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">VARCHAR</span>(50) <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @OldFieldName
= <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'SomeOldFieldName'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">SET</span> @NewFieldName
= <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'MyNewFieldName'</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">select</span><span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'sp_RENAME
'</span> + <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHAR</span>(39)
+ <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'OFC.'</span> +
TABLE_NAME + <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'.'</span> +
@OldFieldName + <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHAR</span>(39)
+ <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">',
'</span> + <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHAR</span>(39)
+ @NewFieldName + <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHAR</span>(39)
+ <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">','</span> + <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHAR</span>(39)
+ <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'COLUMN'</span> + <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHAR</span>(39)
+ <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHAR</span>(10)
+ <span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;">'GO'</span> + <span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;">CHAR</span>(10) <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">from</span> INFORMATION_SCHEMA.COLUMNS <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">WHERE</span> COLUMN_NAME
= @OldFieldName <span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">ORDER</span><span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;">BY</span> TABLE_NAME</span></pre>The
script above will generate the T-SQL code to rename any field SomeOldFieldName to
MyNewFieldName. Run it, and then run the generated script.<br /><br />
Given this and CodeSmith, can you tell I enjoy scripting work?<br /><p></p><img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=204ee18d-4d55-41b1-8ad6-9cbbb083cd37" /></body>
      <title>Tip of the Day: T-SQL Script to rename bulk columns</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,204ee18d-4d55-41b1-8ad6-9cbbb083cd37.aspx</guid>
      <link>http://www.dotnettechnologies.com/2010/08/04/TipOfTheDayTSQLScriptToRenameBulkColumns.aspx</link>
      <pubDate>Wed, 04 Aug 2010 04:22:15 GMT</pubDate>
      <description>The project I am currently working on is a port from a horribly over-engineered Oracle system to a much more manageable SQL Server system. One of the things we needed to do was rename a lot of fields to something more usable. For instance, every table had a field titled "Ended" but we felt it was easier to think of that record as "Deleted". Rather than go through every table (and it wasn't in every table, meaning you needed to manually check each one to verify), I opted a scripting solution.&lt;br&gt;
&lt;br&gt;
Below is a T-SQL script which will generate the sp_rename scripts for all the tables
where the target field is found. A job which would have taken hours and been prone
to errors is now a simple task taking a minute.&lt;br&gt;
&lt;br&gt;
&lt;pre&gt;&lt;span style="color: Black; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;&lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;DECLARE&lt;/span&gt; @OldFieldName &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;VARCHAR&lt;/span&gt;(50),
@NewFieldName &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;VARCHAR&lt;/span&gt;(50) &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @OldFieldName
= &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'SomeOldFieldName'&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;SET&lt;/span&gt; @NewFieldName
= &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'MyNewFieldName'&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;select&lt;/span&gt; &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'sp_RENAME
'&lt;/span&gt; + &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHAR&lt;/span&gt;(39)
+ &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'OFC.'&lt;/span&gt; +
TABLE_NAME + &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'.'&lt;/span&gt; +
@OldFieldName + &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHAR&lt;/span&gt;(39)
+ &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;',
'&lt;/span&gt; + &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHAR&lt;/span&gt;(39)
+ @NewFieldName + &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHAR&lt;/span&gt;(39)
+ &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;','&lt;/span&gt; + &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHAR&lt;/span&gt;(39)
+ &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'COLUMN'&lt;/span&gt; + &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHAR&lt;/span&gt;(39)
+ &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHAR&lt;/span&gt;(10)
+ &lt;span style="color: Red; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;'GO'&lt;/span&gt; + &lt;span style="color: Fuchsia; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;CHAR&lt;/span&gt;(10) &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;from&lt;/span&gt; INFORMATION_SCHEMA.COLUMNS &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;WHERE&lt;/span&gt; COLUMN_NAME
= @OldFieldName &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;ORDER&lt;/span&gt; &lt;span style="color: Blue; background-color: transparent; font-family: Courier New; font-size: 11px;"&gt;BY&lt;/span&gt; TABLE_NAME&lt;/span&gt;&lt;/pre&gt;The
script above will generate the T-SQL code to rename any field SomeOldFieldName to
MyNewFieldName. Run it, and then run the generated script.&lt;br&gt;
&lt;br&gt;
Given this and CodeSmith, can you tell I enjoy scripting work?&lt;br&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=204ee18d-4d55-41b1-8ad6-9cbbb083cd37" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,204ee18d-4d55-41b1-8ad6-9cbbb083cd37.aspx</comments>
      <category>All Things</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=f32c9c96-4f69-4cb0-8638-85ffbcb0f9ad</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,f32c9c96-4f69-4cb0-8638-85ffbcb0f9ad.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,f32c9c96-4f69-4cb0-8638-85ffbcb0f9ad.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f32c9c96-4f69-4cb0-8638-85ffbcb0f9ad</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Recently, I posted a <a href="http://www.dotnettechnologies.com/PermaLink,guid,8df6f658-9820-485e-a7f9-1a9a970bd4ce.aspx" target="_blank">script</a> which
would identify all the procedures which did not have appropriate permissions set,
which is great for last minute deployment checks. I realized I needed to explore INFORMATION_SCHEMA
closer as there is a LOT of useful metadata about the database available. Today's
tip is about some of that useful information.
</p>
        <p>
INFORMATION_SCHEMA is a specia schema which contains several views of the database
metadata. Previous to these views, database developers were forced to join on several
sys tables. Many of my earlier scripts for example rely on this method. However, in
SQL Server 2005 system views were provided which made this task significantly easier.
Here's some of the more useful views to look at. Explore them with code like this
and see what they contain.
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> * <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> INFORMATION_SCHEMA.COLUMNS</span>
        </p>
        <p>
I have found the following most useful, although in truth I suspect over time I will
have use for all of them.
</p>
        <p>
COLUMNS --&gt; I frequently use this to find what tables a particular column name
is found in
</p>
        <p>
CONSTRAINT_COLUMN_USAGE --&gt; identify what keys (primary, foreign) are defined,
and on what fields
</p>
        <p>
ROUTINES --&gt; allows me to explore procedures and functions using this
view
</p>
        <p>
TABLES --&gt; Get information about the various tables in the database
</p>
        <p>
VIEW_COLUMN_USAGE --&gt; obtain information on the fields the view contains
</p>
        <p>
Here's a good link where you can find more information on the <a href="http://msdn.microsoft.com/en-us/library/ms186778.aspx" target="_blank">INFORMATION_SCHEMA</a>.
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=f32c9c96-4f69-4cb0-8638-85ffbcb0f9ad" />
      </body>
      <title>Tip of the Day: More useful information on INFORMATION_SCHEMA</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,f32c9c96-4f69-4cb0-8638-85ffbcb0f9ad.aspx</guid>
      <link>http://www.dotnettechnologies.com/2010/03/11/TipOfTheDayMoreUsefulInformationOnINFORMATIONSCHEMA.aspx</link>
      <pubDate>Thu, 11 Mar 2010 04:02:00 GMT</pubDate>
      <description>&lt;p&gt;
Recently, I posted a &lt;a href="http://www.dotnettechnologies.com/PermaLink,guid,8df6f658-9820-485e-a7f9-1a9a970bd4ce.aspx" target=_blank&gt;script&lt;/a&gt; which
would identify all the procedures which did not have appropriate permissions set,
which is great for last minute deployment checks. I realized I needed to explore INFORMATION_SCHEMA
closer as there is a LOT of useful metadata about the database available. Today's
tip is about some of that useful information.
&lt;/p&gt;
&lt;p&gt;
INFORMATION_SCHEMA is a specia schema which contains several views of the database
metadata. Previous to these views, database developers were forced to join on several
sys tables. Many of my earlier scripts for example rely on this method. However, in
SQL Server 2005 system views were provided which made this task significantly easier.
Here's some of the more useful views to look at. Explore them with code like this
and see what they contain.
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; * &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; INFORMATION_SCHEMA.COLUMNS&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
I have found the following most useful, although in truth I suspect over time I will
have use for all of them.
&lt;/p&gt;
&lt;p&gt;
COLUMNS --&amp;gt; I frequently use this to find what tables a particular column name
is found in
&lt;/p&gt;
&lt;p&gt;
CONSTRAINT_COLUMN_USAGE --&amp;gt; identify what keys (primary, foreign) are defined,
and on what fields
&lt;/p&gt;
&lt;p&gt;
ROUTINES --&amp;gt;&amp;nbsp;allows me to explore&amp;nbsp;procedures and functions using this
view
&lt;/p&gt;
&lt;p&gt;
TABLES --&amp;gt; Get information about the various tables in the database
&lt;/p&gt;
&lt;p&gt;
VIEW_COLUMN_USAGE --&amp;gt; obtain information on the fields the view contains
&lt;/p&gt;
&lt;p&gt;
Here's a good link where you can find more information on the &lt;a href="http://msdn.microsoft.com/en-us/library/ms186778.aspx" target=_blank&gt;INFORMATION_SCHEMA&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=f32c9c96-4f69-4cb0-8638-85ffbcb0f9ad" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,f32c9c96-4f69-4cb0-8638-85ffbcb0f9ad.aspx</comments>
      <category>All Things</category>
      <category>Design</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=8df6f658-9820-485e-a7f9-1a9a970bd4ce</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,8df6f658-9820-485e-a7f9-1a9a970bd4ce.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,8df6f658-9820-485e-a7f9-1a9a970bd4ce.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=8df6f658-9820-485e-a7f9-1a9a970bd4ce</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Creating a script to grant execute to stored procedures where none exist<br />
 <br />
It’s very common to forget to script out permissions for a stored procedure when generating
the script. There’s nothing more frustrating than deploying some code changes, only
to have them not work because you have elevated security on your development SQL Server,
but not on QA or Production. If you find one, likely you’ll find it several places.
How do you quickly locate the procedures which don’t have the desired permission set?
Better yet, how do you fix it quickly without writing a lot of script or manually
setting the permissions in SQL Server Management Studio?
</p>
        <p>
          <br />
The script below is one I pulled off the internet (<a href="http://sqlfool.com/story/46/date-posted/06-06-0939/" target="blank">http://sqlfool.com/story/46/date-posted/06-06-0939/</a>)
and modified a little. Since the environment I am in now uses multiple schemas, I
added the ability to specify schema to search for. 
</p>
        <p>
          <br />
Here’s the script:
</p>
        <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          <p>
            <br />
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DECLARE</span> @role <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">VARCHAR</span>(50) 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DECLARE</span> @targetSchema <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">VARCHAR</span>(50)<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SET</span> @targetSchema
= <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'dbo'</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SET</span> @role
= <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'MyUser'</span><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'GRANT
EXECUTE ON '</span> + SPECIFIC_SCHEMA + <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'.['</span> +
SPECIFIC_NAME + <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">']
TO '</span> + @role + <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">CHAR</span>(10)
+ <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'GO'</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> INFORMATION_SCHEMA.ROUTINES <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WITH</span> (NoLock)<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> SPECIFIC_NAME <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Not</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">In</span><br />
(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> o.name <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> sys.database_permissions
p <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WITH</span> (NoLock) 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INNER</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Join</span> sys.objects
o <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WITH</span> (NoLock) 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> p.major_id
= o.<span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">OBJECT_ID</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">INNER</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">Join</span> sys.database_principals
u <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WITH</span> (NoLock) 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> u.principal_id
= p.grantee_principal_id 
<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> u.name
= @role)<br /><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AND</span> ROUTINE_TYPE
= <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'PROCEDURE'</span><br /><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AND</span> SPECIFIC_NAME <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">not</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">like</span><span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'sp_%'</span><br /><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AND</span> SPECIFIC_SCHEMA
= @targetSchema<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ORDER</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">BY</span> SPECIFIC_NAME
</p>
          <p>
            <font face="Verdana" size="2">Just change the @targetSchema and @role variables and
run it. The results will be a list of procedures on the current database which don’t
have execute permissions assigned to the indicated @role. You can then copy those
procs you want to grant execute on into the query window and execute them.<br /></font>
          </p>
        </span>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=8df6f658-9820-485e-a7f9-1a9a970bd4ce" />
      </body>
      <title>Tip of the Day: Locating scripts without execute permissions set</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,8df6f658-9820-485e-a7f9-1a9a970bd4ce.aspx</guid>
      <link>http://www.dotnettechnologies.com/2010/03/05/TipOfTheDayLocatingScriptsWithoutExecutePermissionsSet.aspx</link>
      <pubDate>Fri, 05 Mar 2010 04:36:29 GMT</pubDate>
      <description>&lt;p&gt;
Creating a script to grant execute to stored procedures where none exist&lt;br&gt;
&amp;nbsp;&lt;br&gt;
It’s very common to forget to script out permissions for a stored procedure when generating
the script. There’s nothing more frustrating than deploying some code changes, only
to have them not work because you have elevated security on your development SQL Server,
but not on QA or Production. If you find one, likely you’ll find it several places.
How do you quickly locate the procedures which don’t have the desired permission set?
Better yet, how do you fix it quickly without writing a lot of script or manually
setting the permissions in SQL Server Management Studio?
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
The script below is one I pulled off the internet (&lt;a href="http://sqlfool.com/story/46/date-posted/06-06-0939/" target=blank&gt;http://sqlfool.com/story/46/date-posted/06-06-0939/&lt;/a&gt;)
and modified a little. Since the environment I am in now uses multiple schemas, I
added the ability to specify schema to search for. 
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
Here’s the script:
&lt;/p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; 
&lt;p&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DECLARE&lt;/span&gt; @role &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;VARCHAR&lt;/span&gt;(50) 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DECLARE&lt;/span&gt; @targetSchema &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;VARCHAR&lt;/span&gt;(50)&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SET&lt;/span&gt; @targetSchema
= &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'dbo'&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SET&lt;/span&gt; @role
= &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'MyUser'&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'GRANT
EXECUTE ON '&lt;/span&gt; + SPECIFIC_SCHEMA + &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'.['&lt;/span&gt; +
SPECIFIC_NAME + &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;']
TO '&lt;/span&gt; + @role + &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;CHAR&lt;/span&gt;(10)
+ &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'GO'&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; INFORMATION_SCHEMA.ROUTINES &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WITH&lt;/span&gt; (NoLock)&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; SPECIFIC_NAME &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Not&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;In&lt;/span&gt; 
&lt;br&gt;
(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; o.name &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; sys.database_permissions
p &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WITH&lt;/span&gt; (NoLock) 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INNER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Join&lt;/span&gt; sys.objects
o &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WITH&lt;/span&gt; (NoLock) 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; p.major_id
= o.&lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;OBJECT_ID&lt;/span&gt; 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;INNER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;Join&lt;/span&gt; sys.database_principals
u &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WITH&lt;/span&gt; (NoLock) 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; u.principal_id
= p.grantee_principal_id 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; u.name
= @role)&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AND&lt;/span&gt; ROUTINE_TYPE
= &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'PROCEDURE'&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AND&lt;/span&gt; SPECIFIC_NAME &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;not&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;like&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'sp_%'&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AND&lt;/span&gt; SPECIFIC_SCHEMA
= @targetSchema&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ORDER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;BY&lt;/span&gt; SPECIFIC_NAME
&lt;/p&gt;
&lt;p&gt;
&lt;font face=Verdana size=2&gt;Just change the @targetSchema and @role variables and run
it. The results will be a list of procedures on the current database which don’t have
execute permissions assigned to the indicated @role. You can then copy those procs
you want to grant execute on into the query window and execute them.&lt;br&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/span&gt;&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=8df6f658-9820-485e-a7f9-1a9a970bd4ce" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,8df6f658-9820-485e-a7f9-1a9a970bd4ce.aspx</comments>
      <category>All Things</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=70592587-7f7f-4d02-83b2-3c78023caa64</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,70592587-7f7f-4d02-83b2-3c78023caa64.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,70592587-7f7f-4d02-83b2-3c78023caa64.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=70592587-7f7f-4d02-83b2-3c78023caa64</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It's been a LONG time since I did any SSIS work, and I really enjoy it. I'm writing
an ETL process for importing general ledger transactions, and it's a fairly complex
task. To help notate what needs to be done as well as explain some of the complexities,
I am making sure to annotate the code in the Control Flow.
</p>
        <p>
I had forgotten how to add carriage returns in annotations, so this is more a reminder
again just in case I forget: Ctrl-Enter will do the trick!
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=70592587-7f7f-4d02-83b2-3c78023caa64" />
      </body>
      <title>Tip of the Day: SSIS -- adding carriage returns to annotations</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,70592587-7f7f-4d02-83b2-3c78023caa64.aspx</guid>
      <link>http://www.dotnettechnologies.com/2009/11/11/TipOfTheDaySSISAddingCarriageReturnsToAnnotations.aspx</link>
      <pubDate>Wed, 11 Nov 2009 17:13:49 GMT</pubDate>
      <description>&lt;p&gt;
It's been a LONG time since I did any SSIS work, and I really enjoy it. I'm writing
an ETL process for importing general ledger transactions, and it's a fairly complex
task. To help notate what needs to be done as well as explain some of the complexities,
I am making sure to annotate the code in the Control Flow.
&lt;/p&gt;
&lt;p&gt;
I had forgotten how to add carriage returns in annotations, so this is more a reminder
again just in case I forget: Ctrl-Enter will do the trick!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=70592587-7f7f-4d02-83b2-3c78023caa64" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,70592587-7f7f-4d02-83b2-3c78023caa64.aspx</comments>
      <category>All Things</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=b1654b01-66f4-4a29-9b70-7c4fc1524f94</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,b1654b01-66f4-4a29-9b70-7c4fc1524f94.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,b1654b01-66f4-4a29-9b70-7c4fc1524f94.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=b1654b01-66f4-4a29-9b70-7c4fc1524f94</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I can't take credit for this, as I took it from the article <a href="http://www.sqlservercentral.com/articles/T-SQL/67624/" target="_blank">How
To Get Table Row Counts Quickly And Painlessly on SqlServerCentral.com by Kendal
Van Dyke</a>. However, it such a good tip, I had to change it a bit, as I tend to
use this a LOT.
</p>
        <p>
First, I made the results a little more "printable" but reducing the table name column.
Next, I made it a system proc because when I go onto client sites, I am looking at
many databases. I probably don't need to create a procedure for it, but there have
been many cases where I trend things, and it would be nice to have this handy. Finally,
I added a parameter to allow me to change the sorting. By default, it sorts by table
name. I added an optional parameter to allow me to pass in an integer (could have
done it a million ways, I know, but I chose to do this solely for my preferences --
feel free to change to yours).
</p>
        <p>
          <strong>Usage</strong>:  <font size="2">sp_retrieveRowCounts   
--- sorts by table name</font></p>
        <p>
          <font size="2">            <font size="2">sp_retrieveRowCounts
1  ---sorts by row count, then table name</font></font>
        </p>
        <p>
          <font size="2">
            <font size="2">Here's the proc:</font>
          </font>
        </p>
        <font size="2">
          <font size="2">
            <p>
              <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <br />
                <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">USE</span> master<br />
GO<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">IF</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span><span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">OBJECT_ID</span>(<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'sp_retrieveRowCounts'</span>,<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'P'</span>)) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">IS</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NOT</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NULL</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">BEGIN</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DROP</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">PROC</span> sp_retrieveRowCounts<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">END</span><br />
GO<br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">CREATE</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">PROC</span> sp_retrieveRowCounts
(@OrderByCount <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> = <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span>)<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AS</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">BEGIN</span><br /><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DECLARE</span> @OrderByParm <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DECLARE</span> @SQLStatement <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">varchar</span>(700)<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DECLARE</span> @OrderByClause <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">varchar</span>(35)<br />
    <br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> @OrderByParm
= <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ISNULL</span>(@OrderByCount,0)<br /><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">IF</span> (@OrderByParm
!= 0)<br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> @OrderByClause
= <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'
ddps.row_count DESC, o.NAME ASC'</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ELSE</span><br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> @OrderByClause
= <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'
o.NAME ASC'</span><br /><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> @SQLStatement
= <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'SELECT
TableName = LEFT(o.name,50), TotalRows = ddps.row_count FROM sys.indexes AS i 
<br />
     INNER JOIN sys.objects AS o ON i.OBJECT_ID = o.OBJECT_ID<br />
     INNER JOIN sys.dm_db_partition_stats AS ddps ON i.OBJECT_ID
= ddps.OBJECT_ID<br />
     AND i.index_id = ddps.index_id 
<br />
    WHERE i.index_id &lt; 2 
<br />
     AND o.is_ms_shipped = 0 
<br />
    ORDER BY '</span> + @OrderByClause<br />
    <br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">EXEC</span>(@SQLStatement)<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">END</span></span>
            </p>
            <p>
              <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--Mark
procedure as system object</span>
                <br />
                <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">EXEC</span> sys.sp_MS_marksystemobject
sp_retrieveRowCounts<br />
GO<br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">GRANT</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">EXEC</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> sp_retrieveRowCounts <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">TO</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><br />
GO</span>
            </p>
            <p>
            </p>
          </font>
        </font> 
<img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=b1654b01-66f4-4a29-9b70-7c4fc1524f94" /></body>
      <title>Tip of the Day: Getting Table Counts</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,b1654b01-66f4-4a29-9b70-7c4fc1524f94.aspx</guid>
      <link>http://www.dotnettechnologies.com/2009/09/02/TipOfTheDayGettingTableCounts.aspx</link>
      <pubDate>Wed, 02 Sep 2009 17:34:10 GMT</pubDate>
      <description>&lt;p&gt;
I can't take credit for this, as I took it from&amp;nbsp;the&amp;nbsp;article &lt;a href="http://www.sqlservercentral.com/articles/T-SQL/67624/" target=_blank&gt;How
To Get Table Row Counts Quickly And Painlessly&amp;nbsp;on SqlServerCentral.com by Kendal
Van Dyke&lt;/a&gt;. However, it such a good tip, I had to change it a bit, as I tend to
use this a LOT.
&lt;/p&gt;
&lt;p&gt;
First, I made the results a little more "printable" but reducing the table name column.
Next, I made it a system proc because when I go onto client sites, I am looking at
many databases. I probably don't need to create a procedure for it, but there have
been many cases where I trend things, and it would be nice to have this handy. Finally,
I added a parameter to allow me to change the sorting. By default, it sorts by table
name. I added an optional parameter to allow me to pass in an integer (could have
done it a million ways, I know, but I chose to do this solely for my preferences --
feel free to change to yours).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Usage&lt;/strong&gt;:&amp;nbsp; &lt;font size=2&gt;sp_retrieveRowCounts&amp;nbsp;&amp;nbsp;&amp;nbsp;
--- sorts by table name&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;font size=2&gt;sp_retrieveRowCounts
1&amp;nbsp; ---sorts by row count, then table name&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=2&gt;&lt;font size=2&gt;Here's the proc:&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;font size=2&gt;&lt;font size=2&gt; 
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;USE&lt;/span&gt; master&lt;br&gt;
GO&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;IF&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;OBJECT_ID&lt;/span&gt;(&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'sp_retrieveRowCounts'&lt;/span&gt;,&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'P'&lt;/span&gt;)) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;IS&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NOT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NULL&lt;/span&gt; 
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DROP&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;PROC&lt;/span&gt; sp_retrieveRowCounts&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;END&lt;/span&gt;
&lt;br&gt;
GO&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;CREATE&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;PROC&lt;/span&gt; sp_retrieveRowCounts
(@OrderByCount &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; = &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;)&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AS&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DECLARE&lt;/span&gt; @OrderByParm &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DECLARE&lt;/span&gt; @SQLStatement &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;varchar&lt;/span&gt;(700)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DECLARE&lt;/span&gt; @OrderByClause &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;varchar&lt;/span&gt;(35)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; @OrderByParm
= &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ISNULL&lt;/span&gt;(@OrderByCount,0)&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;IF&lt;/span&gt; (@OrderByParm
!= 0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; @OrderByClause
= &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'
ddps.row_count DESC, o.NAME ASC'&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ELSE&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; @OrderByClause
= &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'
o.NAME ASC'&lt;/span&gt;
&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; @SQLStatement
= &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'SELECT
TableName = LEFT(o.name,50), TotalRows = ddps.row_count FROM sys.indexes AS i 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INNER JOIN sys.objects AS o ON i.OBJECT_ID = o.OBJECT_ID&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; INNER JOIN sys.dm_db_partition_stats AS ddps ON i.OBJECT_ID
= ddps.OBJECT_ID&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND i.index_id = ddps.index_id 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHERE i.index_id &amp;lt; 2 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND o.is_ms_shipped = 0 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ORDER BY '&lt;/span&gt; + @OrderByClause&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;EXEC&lt;/span&gt;(@SQLStatement)&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;END&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--Mark
procedure as system object&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;EXEC&lt;/span&gt; sys.sp_MS_marksystemobject
sp_retrieveRowCounts&lt;br&gt;
GO&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;GRANT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;EXEC&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; sp_retrieveRowCounts &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;TO&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt;
&lt;br&gt;
GO&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/font&gt;&lt;/font&gt;&amp;nbsp;&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=b1654b01-66f4-4a29-9b70-7c4fc1524f94" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,b1654b01-66f4-4a29-9b70-7c4fc1524f94.aspx</comments>
      <category>All Things</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=f20dd081-543c-48cb-8e21-cc44f6d2ea5a</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,f20dd081-543c-48cb-8e21-cc44f6d2ea5a.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,f20dd081-543c-48cb-8e21-cc44f6d2ea5a.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=f20dd081-543c-48cb-8e21-cc44f6d2ea5a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When I go onto a client site, I usually don't have the luxury of designing everything
from scratch. There are times (like on my current engagement) if I were given the
choice, I'd say scrap it, but usually it doesn't happen that way. It is what it is...
been saying that a lot lately!
</p>
        <p>
So one of the tools I find useful is the ability to search for particular field
names in the database. This is very similar to sp_findsp I use, which I have
posted in the past off the net, but I have modified it so it gets registered as a
system procedure.
</p>
        <p>
So here's sp_findfields. It will return a fairly printable representation of the results
of all fields which match the passed in string. It's registered as a system procedure
in this script, so it is accessible to all databases.
</p>
        <p>
Usage: 
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">sp_findfields <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'%Contact%'</span></span>
        </p>
        <p>
This will find all fields with the string 'Item' in it. Note you need to provide
the wildcard characters.
</p>
        <font color="#0000ff" size="2">
          <font color="#0000ff" size="2">
            <p>
              <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">USE</span> master<br />
GO<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">IF</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span><span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">OBJECT_ID</span>(<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'sp_findfields'</span>,<span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">'P'</span>)) <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">IS</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NOT</span><span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">NULL</span><br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">BEGIN</span><br />
        <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">DROP</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">PROCEDURE</span> sp_findfields<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">END</span><br />
GO<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">CREATE</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">PROCEDURE</span> sp_findfields 
<br />
(@searchText <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">varchar</span>(50))<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">AS</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">BEGIN</span><br />
    <br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">SELECT</span> SchemaName
= <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">LEFT</span>(TABLE_SCHEMA,10),<br />
        TableName = <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">LEFT</span>(TABLE_NAME,
35),<br />
        FieldName = <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">LEFT</span>(COLUMN_NAME,
40),<br />
        DataType = <span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">LEFT</span>(DATA_TYPE,
13),<br />
        DataSize = CHARACTER_MAXIMUM_LENGTH,<br />
        AlloysNulls = IS_NULLABLE<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">FROM</span> INFORMATION_SCHEMA.columns 
<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">WHERE</span> column_name <span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">like</span> @searchText<br />
    <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ORDER</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">BY</span> TableName,
FieldName<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">END</span><br />
GO<br /><br /><span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">--Mark
procedure as system object</span><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">EXEC</span> sys.sp_MS_marksystemobject
sp_findfields<br />
GO<br /><br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">GRANT</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">EXEC</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">ON</span> sp_findfields <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">TO</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><br />
GO</span>
            </p>
          </font>
        </font>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=f20dd081-543c-48cb-8e21-cc44f6d2ea5a" />
      </body>
      <title>Tip of the Day: A system procedure for locating fields in a database</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,f20dd081-543c-48cb-8e21-cc44f6d2ea5a.aspx</guid>
      <link>http://www.dotnettechnologies.com/2009/09/01/TipOfTheDayASystemProcedureForLocatingFieldsInADatabase.aspx</link>
      <pubDate>Tue, 01 Sep 2009 04:10:15 GMT</pubDate>
      <description>&lt;p&gt;
When I go onto a client site, I usually don't have the luxury of designing everything
from scratch. There are times (like on my current engagement) if I were given the
choice, I'd say scrap it, but usually it doesn't happen that way. It is what it is...
been saying that a lot lately!
&lt;/p&gt;
&lt;p&gt;
So one of the tools I find useful is the ability to search for particular&amp;nbsp;field
names&amp;nbsp;in the database. This is very similar to sp_findsp I use, which I have
posted in the past off the net, but I have modified it so it gets registered as a
system procedure.
&lt;/p&gt;
&lt;p&gt;
So here's sp_findfields. It will return a fairly printable representation of the results
of all fields which match the passed in string. It's registered as a system procedure
in this script, so it is accessible to all databases.
&lt;/p&gt;
&lt;p&gt;
Usage: 
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;sp_findfields &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'%Contact%'&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
This will find all fields with the&amp;nbsp;string 'Item' in it. Note you need to provide
the wildcard characters.
&lt;/p&gt;
&lt;font color=#0000ff size=2&gt;&lt;font color=#0000ff size=2&gt; 
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;USE&lt;/span&gt; master&lt;br&gt;
GO&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;IF&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;OBJECT_ID&lt;/span&gt;(&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'sp_findfields'&lt;/span&gt;,&lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;'P'&lt;/span&gt;)) &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;IS&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NOT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;NULL&lt;/span&gt; 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;DROP&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;PROCEDURE&lt;/span&gt; sp_findfields&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;END&lt;/span&gt;
&lt;br&gt;
GO&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;CREATE&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;PROCEDURE&lt;/span&gt; sp_findfields 
&lt;br&gt;
(@searchText &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;varchar&lt;/span&gt;(50))&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;AS&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;BEGIN&lt;/span&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;SELECT&lt;/span&gt; SchemaName
= &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;LEFT&lt;/span&gt;(TABLE_SCHEMA,10),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TableName = &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;LEFT&lt;/span&gt;(TABLE_NAME,
35),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FieldName = &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;LEFT&lt;/span&gt;(COLUMN_NAME,
40),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DataType = &lt;span style="FONT-SIZE: 11px; COLOR: fuchsia; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;LEFT&lt;/span&gt;(DATA_TYPE,
13),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DataSize = CHARACTER_MAXIMUM_LENGTH,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AlloysNulls = IS_NULLABLE&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;FROM&lt;/span&gt; INFORMATION_SCHEMA.columns 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;WHERE&lt;/span&gt; column_name &lt;span style="FONT-SIZE: 11px; COLOR: silver; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;like&lt;/span&gt; @searchText&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ORDER&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;BY&lt;/span&gt; TableName,
FieldName&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;END&lt;/span&gt;
&lt;br&gt;
GO&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: teal; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;--Mark
procedure as system object&lt;/span&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;EXEC&lt;/span&gt; sys.sp_MS_marksystemobject
sp_findfields&lt;br&gt;
GO&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;GRANT&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;EXEC&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;ON&lt;/span&gt; sp_findfields &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;TO&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt;
&lt;br&gt;
GO&lt;/span&gt;
&lt;/p&gt;
&lt;/font&gt;&lt;/font&gt;&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=f20dd081-543c-48cb-8e21-cc44f6d2ea5a" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,f20dd081-543c-48cb-8e21-cc44f6d2ea5a.aspx</comments>
      <category>All Things</category>
      <category>C#</category>
      <category>CSharp</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=29bdfb36-397e-47b3-b62d-04782a8e5e98</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,29bdfb36-397e-47b3-b62d-04782a8e5e98.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,29bdfb36-397e-47b3-b62d-04782a8e5e98.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=29bdfb36-397e-47b3-b62d-04782a8e5e98</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Still working on getting posts over at <a href="http://www.developernation.net/" target="_blank">developernation.net</a>,
and ran out of time on a series I am writing. This week looks to slow down a bit,
so hopefully by the end of the week I can finish my series on a web service for paging
and sorting data. Until then, I just ran across a situation where I needed to programmatically
determine the current database name from T-SQL. Sounds easy, right? Well,
fortunately it is, but the function to do it is documented poorly, so I figured I
would post a tip so others could find it easily.
</p>
        <p>
SELECT db_name()<br /></p>
        <p>
The documentation says you need to pass in the db id, as does the example. However,
if you read very closely, it appears you don't need to pass in the id, and it will
return the current db for the connection. I wish they would have followed the documentation
standard of using optional parameters with brackets [], and it would have saved some
time.
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=29bdfb36-397e-47b3-b62d-04782a8e5e98" />
      </body>
      <title>Tip of the Day -- Determine current database name using T-SQL</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,29bdfb36-397e-47b3-b62d-04782a8e5e98.aspx</guid>
      <link>http://www.dotnettechnologies.com/2006/05/16/TipOfTheDayDetermineCurrentDatabaseNameUsingTSQL.aspx</link>
      <pubDate>Tue, 16 May 2006 15:38:30 GMT</pubDate>
      <description>&lt;p&gt;
Still working on getting posts over at &lt;a href="http://www.developernation.net/" target=_blank&gt;developernation.net&lt;/a&gt;,
and ran out of time on a series I am writing. This week looks to slow down a bit,
so hopefully by the end of the week I can finish my series on a web service for paging
and sorting data. Until then, I just ran across a situation where I needed to programmatically
determine the current&amp;nbsp;database name&amp;nbsp;from T-SQL. Sounds easy, right? Well,
fortunately it is, but the function to do it is documented poorly, so I figured I
would post a tip so others could find it easily.
&lt;/p&gt;
&lt;p&gt;
SELECT db_name()&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
The documentation says you need to pass in the db id, as does the example. However,
if you read very closely, it appears you don't need to pass in the id, and it will
return the current db for the connection. I wish they would have followed the documentation
standard of using optional parameters with brackets [], and it would have saved some
time.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=29bdfb36-397e-47b3-b62d-04782a8e5e98" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,29bdfb36-397e-47b3-b62d-04782a8e5e98.aspx</comments>
      <category>All Things</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=e71c0a9e-e77d-4544-8698-d2c8674e7435</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,e71c0a9e-e77d-4544-8698-d2c8674e7435.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,e71c0a9e-e77d-4544-8698-d2c8674e7435.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=e71c0a9e-e77d-4544-8698-d2c8674e7435</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
So it has been hit or miss posting here, but I am trying to correct it by bringing
on some additional help. I set up a new blog, <a href="http://www.developernation.net">developernation.net</a>,
and begun getting some content on there. Likely I'll crosspost on here too (my content
anyways) but likely most new entries are going to be there first. I figured if I could
get someone else on there helping doing some content, it will have more regular posts.
This blog will continue, just a lag a few days on the content of theother site, and
contain more personal observations.
</p>
        <p>
So swing on by and pop in. I've started off with a bang, and I am in the process of
posting a web service design I created and tested which handles paging and sorting
of data on the server, which my initial tests have shown to be highly efficient. I
am walking through the entire design, and by the end of the weekend I hope to have
the series done and the code posted. If not, you can wait a few days and I will post
it all here as well. 
</p>
        <p>
Thanks!
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=e71c0a9e-e77d-4544-8698-d2c8674e7435" />
      </body>
      <title>A change of directions...</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,e71c0a9e-e77d-4544-8698-d2c8674e7435.aspx</guid>
      <link>http://www.dotnettechnologies.com/2006/04/28/AChangeOfDirections.aspx</link>
      <pubDate>Fri, 28 Apr 2006 15:38:06 GMT</pubDate>
      <description>&lt;p&gt;
So it has been hit or miss posting here, but I am trying to correct it by bringing
on some additional help. I set up a new blog, &lt;a href="http://www.developernation.net"&gt;developernation.net&lt;/a&gt;,
and begun getting some content on there. Likely I'll crosspost on here too (my content
anyways) but likely most new entries are going to be there first. I figured if I could
get someone else on there helping doing some content, it will have more regular posts.
This blog will continue, just a lag a few days on the content of theother site, and
contain more personal observations.
&lt;/p&gt;
&lt;p&gt;
So swing on by and pop in. I've started off with a bang, and I am in the process of
posting a web service design I created and tested which handles paging and sorting
of data on the server, which my initial tests have shown to be highly efficient. I
am walking through the entire design, and by the end of the weekend I hope to have
the series done and the code posted. If not, you can wait a few days and I will post
it all here as well. 
&lt;/p&gt;
&lt;p&gt;
Thanks!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=e71c0a9e-e77d-4544-8698-d2c8674e7435" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,e71c0a9e-e77d-4544-8698-d2c8674e7435.aspx</comments>
      <category>All Things</category>
      <category>ASP.Net</category>
      <category>C#</category>
      <category>CSharp</category>
      <category>Debugging</category>
      <category>Design</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
      <category>Tools</category>
      <category>VB.Net</category>
      <category>Web Services</category>
      <category>Windows</category>
      <category>WinForms</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=42aee519-178f-4c39-a2d0-0318ad74be2b</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,42aee519-178f-4c39-a2d0-0318ad74be2b.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,42aee519-178f-4c39-a2d0-0318ad74be2b.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=42aee519-178f-4c39-a2d0-0318ad74be2b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Once again I have disappeared from blogging. Let's face it, I rarely get to do it.
I also need to upgrade this DasBlog but I have been so busy I haven't been able to
do that, even though someone kindly wrote me and told me it would solve my spam comment
problem. Maybe this weekend...
</p>
        <p>
But I wanted to blog because I found a weird problem, with a weirder solution. I wrote
a Windows service in C# which periodically loads some data, based on the time since
the last load. So dates are a bit important in this process.
</p>
        <p>
First, the cryptic message: “<font color="#191970">Internal Query Processor
Error: The query processor could not produce a query plan. Contact your primary support
provider for more information</font>”.  A Google search turned up several
solutions, but most of them relied on a service patch. What's wierder is that the
error was intermittant. The procs would run fine in Query Analyzer, but not from
C#.
</p>
        <p>
Finally, I tried the unlikely. Instead of passing in my parameters as  DateTime
values, I passed them in as SqlDbType.Varchar, and in the proc made sure and converted
them to DateTime values. Lo and behold, the problem went away.....Go figure!
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=42aee519-178f-4c39-a2d0-0318ad74be2b" />
      </body>
      <title>Bizarre error, bizarre solution...</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,42aee519-178f-4c39-a2d0-0318ad74be2b.aspx</guid>
      <link>http://www.dotnettechnologies.com/2005/09/20/BizarreErrorBizarreSolution.aspx</link>
      <pubDate>Tue, 20 Sep 2005 22:50:34 GMT</pubDate>
      <description>&lt;p&gt;
Once again I have disappeared from blogging. Let's face it, I rarely get to do it.
I also need to upgrade this DasBlog but I have been so busy I haven't been able to
do that, even though someone kindly wrote me and told me it would solve my spam comment
problem. Maybe this weekend...
&lt;/p&gt;
&lt;p&gt;
But I wanted to blog because I found a weird problem, with a weirder solution. I wrote
a Windows service in C# which periodically loads some data, based on the time since
the last load. So dates are a bit important in this process.
&lt;/p&gt;
&lt;p&gt;
First, the cryptic message: &amp;#8220;&lt;font color=#191970&gt;Internal Query Processor Error:
The query processor could not produce a query plan. Contact your primary support provider
for more information&lt;/font&gt;&amp;#8221;.&amp;nbsp; A Google search turned up several solutions,
but most of them relied on a service patch. What's wierder is that the error was intermittant.&amp;nbsp;The
procs would run fine in Query Analyzer, but not from C#.
&lt;/p&gt;
&lt;p&gt;
Finally, I tried the unlikely. Instead of passing in my parameters as&amp;nbsp;&amp;nbsp;DateTime
values, I passed them in as SqlDbType.Varchar, and in the proc made sure and converted
them to DateTime values. Lo and behold, the problem went away.....Go figure!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=42aee519-178f-4c39-a2d0-0318ad74be2b" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,42aee519-178f-4c39-a2d0-0318ad74be2b.aspx</comments>
      <category>All Things</category>
      <category>ASP.Net</category>
      <category>C#</category>
      <category>CSharp</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
      <category>VB.Net</category>
      <category>Windows</category>
      <category>WinForms</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=d98820c4-cb7c-4348-8d80-b59c6d514d01</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,d98820c4-cb7c-4348-8d80-b59c6d514d01.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,d98820c4-cb7c-4348-8d80-b59c6d514d01.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=d98820c4-cb7c-4348-8d80-b59c6d514d01</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Tentatively, my current project is set to end in October. No big deal there, as I
have had absolutely no bench days in the past 10 years as an Independent Contractor.
In fact, during most of those 10 years I have been working 55-80+ hours per week and
having a great time doing it. I don't do the book thing or magazine article thing
because quite frankly, I don't have time because I am busy doing billable work which
pays a lot more. I havn't needed self-promotion thing either because almost all my
business comes from referrals from current or past customers.
</p>
        <p>
Then along come kids, and dang if you don't suddenly want to spend more time at home
and improve their quality of life (and mine). My current project wants to bring me
on full time, and we've had some preliminary discussions on it, and I am willing to
take the pay cut in my pay for one simple thing: I want to work remotely
and raise my kids in the country. Their attitude is like mine, which is that should
be fine since I have a strong work ethic and can get things done, what does it matter
where I work as long as I have a phone and internet connection. However, there are
data security concerns and management concerns which would need to be overcome. 
</p>
        <p>
I have plenty of time, since barring disaster, the contract won't be over until
October sometime. But I figure I will start self-promoting just in case someone from
a company who doesn't mind remote employees stumbles on this blog. Towards October,
I will go full bore trying to close something out, and in the meantime I plan on blogging
much more so I can display what talents I can bring to the table. 
</p>
        <p>
If any companies out there that come up on this, and want a great employee (C#/VB
developer with lots of SQL Server and architect/designer/coding and full-life cycle
development) that brings a lot of experience, a great professional attitude and
presence as well as the ability to code like the wind (and good code at that..), then
drop me a line and let's talk. 
</p>
        <p>
Here's a <a href="http://www.rubiconcomputing.com/resume/resume.html" target="_blank">resume </a>just
in case.. 
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=d98820c4-cb7c-4348-8d80-b59c6d514d01" />
      </body>
      <title>Self Promotion</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,d98820c4-cb7c-4348-8d80-b59c6d514d01.aspx</guid>
      <link>http://www.dotnettechnologies.com/2005/05/26/SelfPromotion.aspx</link>
      <pubDate>Thu, 26 May 2005 23:33:35 GMT</pubDate>
      <description>&lt;p&gt;
Tentatively, my current project is set to end in October. No big deal there, as I
have had absolutely no bench days in the past 10 years as an Independent Contractor.
In fact, during most of those 10 years I have been working 55-80+ hours per week and
having a great time doing it. I don't do the book thing or magazine article thing
because quite frankly, I don't have time because I am busy doing billable work which
pays a lot more. I havn't needed self-promotion thing either because almost all my
business comes from referrals from current or past customers.
&lt;/p&gt;
&lt;p&gt;
Then along come kids, and dang if you don't suddenly want to spend more time at home
and improve their quality of life (and mine). My current project wants to bring me
on full time, and we've had some preliminary discussions on it, and I am willing to
take&amp;nbsp;the pay&amp;nbsp;cut in my pay for one simple thing: I want to work remotely
and raise my kids in the country. Their attitude is like mine, which is that should
be fine since I have a strong work ethic and can get things done, what does it matter
where I work as long as I have a phone and internet connection. However, there are
data security concerns and management concerns which would need to be overcome. 
&lt;/p&gt;
&lt;p&gt;
I&amp;nbsp;have plenty of time, since barring disaster, the contract won't be over until
October sometime. But I figure I will start self-promoting just in case someone from
a company who doesn't mind remote employees stumbles on this blog. Towards October,
I will go full bore trying to close something out, and in the meantime I plan on blogging
much more so I can display what talents I can bring to the table. 
&lt;/p&gt;
&lt;p&gt;
If any companies out there that come up on this, and want a great employee (C#/VB
developer with lots of SQL Server and architect/designer/coding and full-life cycle
development)&amp;nbsp;that brings a lot of experience, a great professional attitude and
presence as well as the ability to code like the wind (and good code at that..), then
drop me a line and let's talk. 
&lt;/p&gt;
&lt;p&gt;
Here's a &lt;a href="http://www.rubiconcomputing.com/resume/resume.html" target=_blank&gt;resume &lt;/a&gt;just
in case.. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=d98820c4-cb7c-4348-8d80-b59c6d514d01" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,d98820c4-cb7c-4348-8d80-b59c6d514d01.aspx</comments>
      <category>All Things</category>
      <category>ASP.Net</category>
      <category>C#</category>
      <category>CSharp</category>
      <category>Design</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
      <category>Tools</category>
      <category>VB.Net</category>
      <category>Windows</category>
      <category>WinForms</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=43bf4362-22d3-40d8-8818-2f82d1b8ffd4</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,43bf4362-22d3-40d8-8818-2f82d1b8ffd4.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,43bf4362-22d3-40d8-8818-2f82d1b8ffd4.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=43bf4362-22d3-40d8-8818-2f82d1b8ffd4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Someone asked me today if I knew that you couldn't use GETDATE() in functions in SQL
Server. Well, I did, so I dug out this solution I had found a while back, and decided
to post it, since I am sure others have come across the same issue. 
</p>
        <p>
Basically, you use a view to do the work for you... here you go!
</p>
        <p>
CREATE VIEW Function_Assist_GETDATE
</p>
        <p>
/********************************************************<br />
*<br />
* A view to return one row, with one column, the current 
<br />
* date/time from the built-in function GETDATE().  This 
<br />
* view allows a UDF to bypass the restriction on access to 
<br />
* the non-deterministic getdate() function.<br />
*<br />
* Attribution: Based on a newsgroup posting in by Mikhail 
<br />
*   Berlyant in microsoft.public.sqlserver.programming 
<br />
* 
<br />
* Common Usage:<br />
DECLARE @dtVar datetime<br />
select @dtVAr = [GetDate] from Function_Assist_GETDATE<br />
**********************************************************/
</p>
        <p>
AS 
<br />
    SELECT getdate() as [GetDate]
</p>
        <p>
GO
</p>
        <p>
GRANT SELECT on Function_Assist_GETDATE to PUBLIC<br />
GO
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=43bf4362-22d3-40d8-8818-2f82d1b8ffd4" />
      </body>
      <title>Tip of the Day -- Using GetDate in functions</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,43bf4362-22d3-40d8-8818-2f82d1b8ffd4.aspx</guid>
      <link>http://www.dotnettechnologies.com/2005/03/11/TipOfTheDayUsingGetDateInFunctions.aspx</link>
      <pubDate>Fri, 11 Mar 2005 21:45:00 GMT</pubDate>
      <description>&lt;p&gt;
Someone asked me today if I knew that you couldn't use GETDATE() in functions in SQL
Server. Well, I did, so I dug out this solution I had found a while back, and decided
to post it, since I am sure others have come across the same issue. 
&lt;/p&gt;
&lt;p&gt;
Basically, you use a view to do the work for you... here you go!
&lt;/p&gt;
&lt;p&gt;
CREATE VIEW Function_Assist_GETDATE
&lt;/p&gt;
&lt;p&gt;
/********************************************************&lt;br&gt;
*&lt;br&gt;
* A view to return one row, with one column, the current 
&lt;br&gt;
* date/time from the built-in function GETDATE().&amp;nbsp; This 
&lt;br&gt;
* view allows a UDF to bypass the restriction on access to 
&lt;br&gt;
* the non-deterministic getdate() function.&lt;br&gt;
*&lt;br&gt;
* Attribution: Based on a newsgroup posting in by Mikhail 
&lt;br&gt;
*&amp;nbsp;&amp;nbsp; Berlyant in microsoft.public.sqlserver.programming 
&lt;br&gt;
* 
&lt;br&gt;
* Common Usage:&lt;br&gt;
DECLARE @dtVar datetime&lt;br&gt;
select @dtVAr = [GetDate] from Function_Assist_GETDATE&lt;br&gt;
**********************************************************/
&lt;/p&gt;
&lt;p&gt;
AS 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT getdate() as [GetDate]
&lt;/p&gt;
&lt;p&gt;
GO
&lt;/p&gt;
&lt;p&gt;
GRANT SELECT on Function_Assist_GETDATE to PUBLIC&lt;br&gt;
GO
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=43bf4362-22d3-40d8-8818-2f82d1b8ffd4" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,43bf4362-22d3-40d8-8818-2f82d1b8ffd4.aspx</comments>
      <category>All Things</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=eea0c32f-1b4f-45d6-acb4-4dde56f38ffc</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,eea0c32f-1b4f-45d6-acb4-4dde56f38ffc.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,eea0c32f-1b4f-45d6-acb4-4dde56f38ffc.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=eea0c32f-1b4f-45d6-acb4-4dde56f38ffc</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
So, you want to get the value of an identity column after an insert so you can update
the UI or use it in another procedure. You've been using @@IDENTITY for ages, and
it has worked fine. But today, you're getting a new value. What's up?
</p>
        <p>
There are 3 (or more if you want to consider queries or identity tables) methods for
getting the identity value from a table after an insert. The methods are @@IDENTITY,
SCOPE_IDENTITY() and IDENT_CURRENT(). What's the difference?
</p>
        <p>
@@IDENTITY gets the last inserted identity value. The problem is, in a high-volumn
environment or one with triggers, it may not be the value you are expecting.
</p>
        <p>
The SCOPE_IDENTITY() function gets the last identity inserted within the same scope
(e.g. inside the procedure you're running). This is useful especially when your table
has triggers which might do another insert. You'll only get the identity value of
your change, not the trigger's.
</p>
        <p>
The IDENT_CURRENT('table_name') returns the last identity value for the specified
table, regardless of scope.
</p>
        <p>
So, for safety in your routines, if you want the value YOU inserted, try the SCOPE_IDENTITY
function for safety!
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=eea0c32f-1b4f-45d6-acb4-4dde56f38ffc" />
      </body>
      <title>The difference between @@IDENTITY, SCOPE_IDENTITY(), and IDENT_CURRENT()</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,eea0c32f-1b4f-45d6-acb4-4dde56f38ffc.aspx</guid>
      <link>http://www.dotnettechnologies.com/2004/12/31/TheDifferenceBetweenIDENTITYSCOPEIDENTITYAndIDENTCURRENT.aspx</link>
      <pubDate>Fri, 31 Dec 2004 15:25:39 GMT</pubDate>
      <description>&lt;p&gt;
So, you want to get the value of an identity column after an insert so you can update
the UI or use it in another procedure. You've been using @@IDENTITY for ages, and
it has worked fine. But today, you're getting a new value. What's up?
&lt;/p&gt;
&lt;p&gt;
There are 3 (or more if you want to consider queries or identity tables) methods for
getting the identity value from a table after an insert. The methods are @@IDENTITY,
SCOPE_IDENTITY() and IDENT_CURRENT(). What's the difference?
&lt;/p&gt;
&lt;p&gt;
@@IDENTITY gets the last inserted identity value. The problem is, in a high-volumn
environment or one with triggers, it may not be the value you are expecting.
&lt;/p&gt;
&lt;p&gt;
The SCOPE_IDENTITY() function gets the last identity inserted within the same scope
(e.g. inside the procedure you're running). This is useful especially when your table
has triggers which might do another insert. You'll only get the identity value of
your change, not the trigger's.
&lt;/p&gt;
&lt;p&gt;
The IDENT_CURRENT('table_name') returns the last identity value for the specified
table, regardless of scope.
&lt;/p&gt;
&lt;p&gt;
So, for safety in your routines, if you want the value YOU inserted, try the SCOPE_IDENTITY
function for safety!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=eea0c32f-1b4f-45d6-acb4-4dde56f38ffc" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,eea0c32f-1b4f-45d6-acb4-4dde56f38ffc.aspx</comments>
      <category>All Things</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=64f0d00f-10c2-47f2-ba98-cfa86815c141</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,64f0d00f-10c2-47f2-ba98-cfa86815c141.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,64f0d00f-10c2-47f2-ba98-cfa86815c141.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=64f0d00f-10c2-47f2-ba98-cfa86815c141</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Here's a quick and dirty routine I use all the time. This simple procedure will list
all tables in your database where a particular field is found. Not earth shattering
by any means, but useful and since I find I use it all the time, I thought someone
else might find it userful and same them the brief effort to write their own!
</p>
        <p>
IF EXISTS(SELECT name FROM sysobjects WHERE name = 'sp_findField' AND type='P')<br />
BEGIN<br />
 DROP PROCEDURE sp_findField<br />
END<br />
GO
</p>
        <p>
CREATE PROCEDURE dbo.sp_findField (@fieldName varchar(100))<br />
AS<br />
BEGIN<br />
 SET NOCOUNT ON
</p>
        <p>
 SELECT so.name<br />
 FROM sysobjects so, syscolumns sc<br />
 WHERE so.id = sc.id<br />
 AND sc.name = @fieldName<br />
 ORDER BY so.name
</p>
        <p>
END<br />
GO
</p>
        <p>
GRANT EXECUTE ON sp_findField TO public<br />
GO
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=64f0d00f-10c2-47f2-ba98-cfa86815c141" />
      </body>
      <title>Finding all tables where a field exists in SQL Server</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,64f0d00f-10c2-47f2-ba98-cfa86815c141.aspx</guid>
      <link>http://www.dotnettechnologies.com/2004/12/30/FindingAllTablesWhereAFieldExistsInSQLServer.aspx</link>
      <pubDate>Thu, 30 Dec 2004 18:09:49 GMT</pubDate>
      <description>&lt;p&gt;
Here's a quick and dirty routine I use all the time. This simple procedure will list
all tables in your database where a particular field is found. Not earth shattering
by any means, but useful and since I find I use it all the time, I thought someone
else might find it userful and same them the brief effort to write their own!
&lt;/p&gt;
&lt;p&gt;
IF EXISTS(SELECT name FROM sysobjects WHERE name = 'sp_findField' AND type='P')&lt;br&gt;
BEGIN&lt;br&gt;
&amp;nbsp;DROP PROCEDURE sp_findField&lt;br&gt;
END&lt;br&gt;
GO
&lt;/p&gt;
&lt;p&gt;
CREATE PROCEDURE dbo.sp_findField (@fieldName varchar(100))&lt;br&gt;
AS&lt;br&gt;
BEGIN&lt;br&gt;
&amp;nbsp;SET NOCOUNT ON
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;SELECT so.name&lt;br&gt;
&amp;nbsp;FROM sysobjects so, syscolumns sc&lt;br&gt;
&amp;nbsp;WHERE so.id = sc.id&lt;br&gt;
&amp;nbsp;AND sc.name = @fieldName&lt;br&gt;
&amp;nbsp;ORDER BY so.name
&lt;/p&gt;
&lt;p&gt;
END&lt;br&gt;
GO
&lt;/p&gt;
&lt;p&gt;
GRANT EXECUTE ON sp_findField TO public&lt;br&gt;
GO
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=64f0d00f-10c2-47f2-ba98-cfa86815c141" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,64f0d00f-10c2-47f2-ba98-cfa86815c141.aspx</comments>
      <category>All Things</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=33c1ea8a-2aff-47e3-af42-71ad24e06a91</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,33c1ea8a-2aff-47e3-af42-71ad24e06a91.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,33c1ea8a-2aff-47e3-af42-71ad24e06a91.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=33c1ea8a-2aff-47e3-af42-71ad24e06a91</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I am a huge fan of SQL Server. Don't get me wrong, there's nothing wrong with Oracle
per se, other than it's tough to get it to run right and efficiently, and not for
the amateur. If I was considering cross-platform work, Oracle would be the choice
for me. For everything else, there's SQL Server, and when the next version comes out,
look out!
</p>
        <p>
But one “downfall” of SQL Server is that it becomes very easy to believe
you can administer a production database with only Access skills, or worse, none at
all. Some stories come to mind from my experiences.
</p>
        <p>
First, I was doing a project for a large state agency, and at the time no one but
the DBA could touch the SQL Server, or directly run script against the production
server. I needed a very simple update script run, so I wrote it out, and gave it to
the “DBA” to execute. He returns in about 5 minutes and tells me it doesn't
run, and asked if I would mind taking a look at it. I did, and I had accidentally
pasted two “set” keywords in a row. Even a Jr. Developer with a minutia
of SQL experience should have seen this and been able to fix it. So, I fix it, and
I asked him why he didn't just erase the extra “set”. is reply: I don't
know SQL. Please keep it quiet.
</p>
        <p>
Mind you, this guy was touted as a Sr. DBA, and responsible for about 16 databases
which control some very important data. Unfortunately, this wasn't the worst of it. 
</p>
        <p>
One day I was using the restroom (that's important to the story), and he comes in
and begins using the stall next to me. We begin some general chit-chat, and he's excited
he can tell me something about SQL I might not know. His epiphany? “I went to
a class, and we learned about indexes in it! I am so excited to start trying them
out!” Well, since we had never been able to directly access the database, I
had just solved some of our performance problems. I also got access to the databases
from that day forward..
</p>
        <p>
Another performance story. I was doing a project for an Internet startup, and I overheard
they were having some SQL Server performance problems. The sign-ups for their site
was killing their SQL Server with all the activity. I offered to take a look at it,
but there was a definite anti-Microsoft spin at the company, and they were going to
switch to Oracle because the owner had been told it was simply because Microsoft can't
keep up with Oracle. I was working on a VB application for them, so they assumed wrongly
that I didn't know databases very well.
</p>
        <p>
So, they hire a team of Oracle (weren't employees of the Oracle Corp, just for reference) developers,
and in 4 months and almost $300,000 later, it still wasn't up and running. The owner
was bleeding cash, and was pressing the Oracle team to find out when the database
conversion would be done and the bleeding would stop. They then informed him
it would be about another month (and $100K), but even then they'd need to keep one
of them one to maintain the database, or hire a full time Oracle DBA.
</p>
        <p>
So I re-enter the SQL Server discussion with the owner. I ask him to let me look at
the SQL Server configuration, and maybe I can buy him some time until the Oracle system
is ready to go. Immediately, I see there are no indexes on the tables, and the stored procedures
were very inefficient. By adding indexes alone, the performance gain alone went from
~98% resources used on the server (processor and memory) to less than 5% peak usage.
The Oracle team was gone the next day (sorry guys), and the SQL Server portion ran
flawlessly from that day on. But something so simple cost over $300K and wasted four
months, and probably cost the company a lot of lost business.
</p>
        <p>
My point is this: SQL Server is simple to use, but you have to educate yourself on
how to use it best, because in some ways it makes it way too easy to think you are
doing the right thing, when in fact it can have a huge impact on your performance.
Next thing you know, you think the server can't handle it and you're out $20K on a
new server which you don't need.
</p>
        <p>
Case in point: I was hired by a bank to help a team of new developer's get up to speed
in VB.Net and ASP.Net (I tried to get them to go C#, but that's another story). Their
application was simple, but every day the resources on the server(s) were getting
pegged and the entire system was unusable until the next reboot. 
</p>
        <p>
I decided looking into this would need to be a top priority. I checked the indexes,
and they all looked good, but we did some tweaking there, and the problem was still
occurring. I finally pull out the SQL Performance Analyzer, and quickly nail down
there is a serious disk I/O problem. For example, I pull out a small table of about
200 rows, and do a select *, and it turns out it take 3000 disk I/O's to get it. The
indexes were fine, and the record size was small, so there was something else going
on. 
</p>
        <p>
On a hunch, I check to see if the disk drive is fragmented. Holy crap! It looked
as if it had never been defragged, and come to find out, it hadn't. The network admin
responsible for this production machine advised me the way he builds the machines
means that they never have to be defragged. So I show him an image of the fragmentation
on the drives, and he thinks I rigged it. End of conversation with him. 
</p>
        <p>
So I fix the problem, then ask for $200 for a program to schedule the de-fragmentation
automatically. I don't get it, so I now have to manually defrag it once per week.
Cost? Well, for me to track it down, and the time spent every week over a year and
a half? Likely over $5000. Go figure.
</p>
        <p>
I tried to get the employees there to take responsibility for doing it, but it never
seemed to be a priority for them. So when I left to being a new project, I reminded
them someone needed to do it. Well, I can assume for a while someone did. But I am
back there, and yesterday there were complaints of poor performance. And guess what
I found when I checked? The drives were very fragmented. One is so full and so bad,
I think we're going to have to drop all the indexes, defragment, then rebuild them.
That's gonna cost a ton of time and money. The $200 is going to be budgeted this time.
</p>
        <p>
The tip: Make sure you keep your SQL Server data and index drives defragmented. Failure
to do so can have a huge impact on your SQL Performance, since in the end, SQL Server
relies on quick disk I/O to perform well.
</p>
        <p>
-- Daryl
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=33c1ea8a-2aff-47e3-af42-71ad24e06a91" />
      </body>
      <title>Tip of the Day: The Importance of Defragging your SQL Server drive</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,33c1ea8a-2aff-47e3-af42-71ad24e06a91.aspx</guid>
      <link>http://www.dotnettechnologies.com/2004/12/11/TipOfTheDayTheImportanceOfDefraggingYourSQLServerDrive.aspx</link>
      <pubDate>Sat, 11 Dec 2004 18:29:51 GMT</pubDate>
      <description>&lt;p&gt;
I am a huge fan of SQL Server. Don't get me wrong, there's nothing wrong with Oracle
per se, other than it's tough to get it to run right and efficiently, and not for
the amateur. If I was considering cross-platform work, Oracle would be the choice
for me. For everything else, there's SQL Server, and when the next version comes out,
look out!
&lt;/p&gt;
&lt;p&gt;
But one &amp;#8220;downfall&amp;#8221; of SQL Server is that it becomes very easy to believe
you can administer a production database with only Access skills, or worse, none at
all. Some stories come to mind from my experiences.
&lt;/p&gt;
&lt;p&gt;
First, I was doing a project for a large state agency, and at the time no one but
the DBA could touch the SQL Server, or directly run script against the production
server. I needed a very simple update script run, so I wrote it out, and gave it to
the &amp;#8220;DBA&amp;#8221; to execute. He returns in about 5 minutes and tells me it doesn't
run, and asked if I would mind taking a look at it. I did, and I had accidentally
pasted two &amp;#8220;set&amp;#8221; keywords in a row. Even a Jr. Developer with a minutia
of SQL experience should have seen this and been able to fix it. So, I fix it, and
I asked him why he didn't just erase the extra &amp;#8220;set&amp;#8221;. is reply: I don't
know SQL. Please keep it quiet.
&lt;/p&gt;
&lt;p&gt;
Mind you, this guy was touted as a Sr. DBA, and responsible for about 16 databases
which control some very important data. Unfortunately, this wasn't the worst of it. 
&lt;/p&gt;
&lt;p&gt;
One day I was using the restroom (that's important to the story), and he comes in
and begins using the stall next to me. We begin some general chit-chat, and he's excited
he can tell me something about SQL I might not know. His epiphany? &amp;#8220;I went to
a class, and we learned about indexes in it! I am so excited to start trying them
out!&amp;#8221; Well, since we had never been able to directly access the database, I
had just solved some of our performance problems. I also got access to the databases
from that day forward..
&lt;/p&gt;
&lt;p&gt;
Another performance story. I was doing a project for an Internet startup, and I overheard
they were having some SQL Server performance problems. The sign-ups for their site
was killing their SQL Server with all the activity. I offered to take a look at it,
but there was a definite anti-Microsoft spin at the company, and they were going to
switch to Oracle because the owner had been told it was simply because Microsoft can't
keep up with Oracle. I was working on a VB application for them, so they assumed wrongly
that I didn't know databases very well.
&lt;/p&gt;
&lt;p&gt;
So, they hire a team of Oracle (weren't employees of the Oracle Corp, just for reference)&amp;nbsp;developers,
and in 4 months and almost $300,000 later, it still wasn't up and running. The owner
was bleeding cash, and was pressing the Oracle team to find out when&amp;nbsp;the database
conversion&amp;nbsp;would be done and the bleeding would stop. They then informed him
it would be about another month (and $100K), but even then they'd need to keep one
of them one to maintain the database, or hire a full time Oracle DBA.
&lt;/p&gt;
&lt;p&gt;
So I re-enter the SQL Server discussion with the owner. I ask him to let me look at
the SQL Server configuration, and maybe I can buy him some time until the Oracle system
is ready to go. Immediately, I see there are no indexes on the tables, and the stored&amp;nbsp;procedures
were very inefficient. By adding indexes alone, the performance gain alone went from
~98% resources used on the server (processor and memory) to less than 5% peak usage.
The Oracle team was gone the next day (sorry guys), and the SQL Server portion ran
flawlessly from that day on. But something so simple cost over $300K and wasted four
months, and probably cost the company a lot of lost business.
&lt;/p&gt;
&lt;p&gt;
My point is this: SQL Server is simple to use, but you have to educate yourself on
how to use it best, because in some ways it makes it way too easy to think you are
doing the right thing, when in fact it can have a huge impact on your performance.
Next thing you know, you think the server can't handle it and you're out $20K on a
new server which you don't need.
&lt;/p&gt;
&lt;p&gt;
Case in point: I was hired by a bank to help a team of new developer's get up to speed
in VB.Net and ASP.Net (I tried to get them to go C#, but that's another story). Their
application was simple, but every day the resources on the server(s) were getting
pegged and the entire system was unusable until the next reboot. 
&lt;/p&gt;
&lt;p&gt;
I decided looking into this would need to be a top priority. I checked the indexes,
and they all looked good, but we did some tweaking there, and the problem was still
occurring. I finally pull out the SQL Performance Analyzer, and quickly nail down
there is a serious disk I/O problem. For example, I pull out a small table of about
200 rows, and do a select *, and it turns out it take 3000 disk I/O's to get it. The
indexes were fine, and the record size was small, so there was something else going
on. 
&lt;/p&gt;
&lt;p&gt;
On a hunch,&amp;nbsp;I check to see if the disk drive is fragmented. Holy crap! It looked
as if it had never been defragged, and come to find out, it hadn't. The network admin
responsible for this production machine advised me the way he builds the machines
means that they never have to be defragged. So I show him an image of the fragmentation
on the drives, and he thinks I rigged it. End of conversation with him. 
&lt;/p&gt;
&lt;p&gt;
So I fix the problem, then ask for $200 for a program to schedule the de-fragmentation
automatically. I don't get it, so I now have to manually defrag it once per week.
Cost? Well, for me to track it down, and the time spent every week over a year and
a half? Likely over $5000. Go figure.
&lt;/p&gt;
&lt;p&gt;
I tried to get the employees there to take responsibility for doing it, but it never
seemed to be a priority for them. So when I left to being a new project, I reminded
them someone needed to do it. Well, I can assume for a while someone did. But I am
back there, and yesterday there were complaints of poor performance. And guess what
I found when I checked? The drives were very fragmented. One is so full and so bad,
I think we're going to have to drop all the indexes, defragment, then rebuild them.
That's gonna cost a ton of time and money. The $200 is going to be budgeted this time.
&lt;/p&gt;
&lt;p&gt;
The tip: Make sure you keep your SQL Server data and index drives defragmented. Failure
to do so can have a huge impact on your SQL Performance, since in the end, SQL Server
relies on quick disk I/O to perform well.
&lt;/p&gt;
&lt;p&gt;
-- Daryl
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=33c1ea8a-2aff-47e3-af42-71ad24e06a91" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,33c1ea8a-2aff-47e3-af42-71ad24e06a91.aspx</comments>
      <category>All Things</category>
      <category>General</category>
      <category>SQL Tips</category>
      <category>Tips and Tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.dotnettechnologies.com/Trackback.aspx?guid=3d080936-1fb7-42f9-bfdc-adf6199ec9e1</trackback:ping>
      <pingback:server>http://www.dotnettechnologies.com/pingback.aspx</pingback:server>
      <pingback:target>http://www.dotnettechnologies.com/PermaLink,guid,3d080936-1fb7-42f9-bfdc-adf6199ec9e1.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.dotnettechnologies.com/CommentView,guid,3d080936-1fb7-42f9-bfdc-adf6199ec9e1.aspx</wfw:comment>
      <wfw:commentRss>http://www.dotnettechnologies.com/SyndicationService.asmx/GetEntryCommentsRss?guid=3d080936-1fb7-42f9-bfdc-adf6199ec9e1</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
On a rare occasion, I come across a circumstance where I can't create a SQL Server
2000 diagram, or the table is so huge and denormalized it doesn't really print from
the diagram tool (which I love, BTW).
</p>
        <p>
So, I ended up writing this little script which allows me to create a report which
is easily printable. Yes, I am definitely a guy, as I am visually focused. :)
</p>
        <p>
          <font color="#0066ff">DECLARE @TableName VARCHAR(50)</font>
        </p>
        <p>
          <font color="#0066ff">SET @TableName = 'Authors'</font>
        </p>
        <p>
          <font color="#0066ff">select<br />
 'Table_Name'   = Left(sysobjects.name,25),<br />
 'Column_name'   = Left(syscolumns.name,25),<br />
 'Type'     = Left(type_name(xusertype),10),<br />
 'Length'    = convert(int, length),<br />
 'Nullable'    = case when isnullable = 0 then 'No' else
'Yes' end<br />
from syscolumns, sysobjects where sysobjects.name = @TableName<br />
and sysobjects.id = syscolumns.id<br />
order by colid</font>
        </p>
        <p>
Now, I imagine by now anyone reading these tips are saying “OK, nothing Earth
shattering here..”. Give it time. Right now I am working on 2 projects, and
it's hard to justify a lot of blogging time when I could be billing $XX per hour as
many hours as I can stay awake and sane. At some point, I am going to write a series
of articles on “Control Oriented Development”, and possibly give the keys
to the city for a Questionaire engine I created which is basically limitless in how
it presents questionaires to users. If you can think of a question, it can ask it...
But that's down the road, so stick around!
</p>
        <img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=3d080936-1fb7-42f9-bfdc-adf6199ec9e1" />
      </body>
      <title>Creating a printable listing of fields in a table</title>
      <guid isPermaLink="false">http://www.dotnettechnologies.com/PermaLink,guid,3d080936-1fb7-42f9-bfdc-adf6199ec9e1.aspx</guid>
      <link>http://www.dotnettechnologies.com/2004/11/06/CreatingAPrintableListingOfFieldsInATable.aspx</link>
      <pubDate>Sat, 06 Nov 2004 16:45:22 GMT</pubDate>
      <description>&lt;p&gt;
On a rare occasion, I come across a circumstance where I can't create a SQL Server
2000 diagram, or the table is so huge and denormalized it doesn't really print from
the diagram tool (which I love, BTW).
&lt;/p&gt;
&lt;p&gt;
So, I ended up writing this little script which allows me to create a report which
is easily printable. Yes, I am definitely a guy, as I am visually focused. :)
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#0066ff&gt;DECLARE @TableName VARCHAR(50)&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#0066ff&gt;SET @TableName = 'Authors'&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#0066ff&gt;select&lt;br&gt;
&amp;nbsp;'Table_Name'&amp;nbsp;&amp;nbsp;&amp;nbsp;= Left(sysobjects.name,25),&lt;br&gt;
&amp;nbsp;'Column_name'&amp;nbsp;&amp;nbsp;&amp;nbsp;= Left(syscolumns.name,25),&lt;br&gt;
&amp;nbsp;'Type'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= Left(type_name(xusertype),10),&lt;br&gt;
&amp;nbsp;'Length'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= convert(int, length),&lt;br&gt;
&amp;nbsp;'Nullable'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= case when isnullable = 0 then 'No' else
'Yes' end&lt;br&gt;
from syscolumns, sysobjects where sysobjects.name = @TableName&lt;br&gt;
and sysobjects.id = syscolumns.id&lt;br&gt;
order by colid&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
Now, I imagine by now anyone reading these tips are saying &amp;#8220;OK, nothing Earth
shattering here..&amp;#8221;. Give it time. Right now I am working on 2 projects, and
it's hard to justify a lot of blogging time when I could be billing $XX per hour as
many hours as I can stay awake and sane. At some point, I am going to write a series
of articles on &amp;#8220;Control Oriented Development&amp;#8221;, and possibly give the keys
to the city for a Questionaire engine I created which is basically limitless in how
it presents questionaires to users. If you can think of a question, it can ask it...
But that's down the road, so stick around!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.dotnettechnologies.com/aggbug.ashx?id=3d080936-1fb7-42f9-bfdc-adf6199ec9e1" /&gt;</description>
      <comments>http://www.dotnettechnologies.com/CommentView,guid,3d080936-1fb7-42f9-bfdc-adf6199ec9e1.aspx</comments>
      <category>Tips and Tricks</category>
      <category>SQL Tips</category>
    </item>
  </channel>
</rss>