<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devpinoy.org/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Keith Rull : SQL</title><link>http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx</link><description>Tags: SQL</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>How To: Count the occurrence of a character in a string in SQL</title><link>http://devpinoy.org/blogs/keithrull/archive/2010/06/09/how-to-count-the-occurrence-of-a-character-in-a-string-in-sql.aspx</link><pubDate>Wed, 09 Jun 2010 19:03:00 GMT</pubDate><guid isPermaLink="false">99090821-4da1-4a75-98c2-a35884625ff7:36186</guid><dc:creator>keithrull</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devpinoy.org/blogs/keithrull/rsscomments.aspx?PostID=36186</wfw:commentRss><comments>http://devpinoy.org/blogs/keithrull/archive/2010/06/09/how-to-count-the-occurrence-of-a-character-in-a-string-in-sql.aspx#comments</comments><description>&lt;p&gt;Last night I was trying to cleanup the spammers from the database of 
devpinoy.org and while I was evaluating the result sets i was able to 
conclude that aside from using common spam text like &amp;#39;cheap&amp;#39;, &amp;#39;buy&amp;#39;, 
&amp;#39;free&amp;#39;, &amp;#39;deal&amp;#39;, &amp;#39;viagra&amp;#39;, &amp;#39;prozac&amp;#39; that 30% of the false emails that 
spam accounts are using multiple dots on their email address. A good 
example is a subset below from the list of offenders that I found in the
 devpinoy db.&lt;br /&gt;&lt;img src="http://www.keithrull.com/content/binary/spam_email_accounts.png" alt="" /&gt;&lt;br /&gt;&lt;br /&gt;Having
 found that fact I immediately created a sql script that will delete 
users from the database if they have more than 2 dots in their email 
address.&lt;br /&gt;&lt;br /&gt;Enough with the side note and here is some code.&lt;br /&gt;&lt;/p&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; @string2check &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;varchar&lt;/span&gt;(50)&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;DECLARE&lt;/span&gt; @character2find &lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;char&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;SET&lt;/span&gt; @string2check = &lt;span style="color:red;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&amp;#39;this is a very long string&amp;#39;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;SET&lt;/span&gt; @character2find = &lt;span style="color:red;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&amp;#39;i&amp;#39;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;PRINT&lt;/span&gt; &lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;LEN&lt;/span&gt;(@string2check) - &lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;LEN&lt;/span&gt;(&lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;REPLACE&lt;/span&gt;(@string2check, @character2find, &lt;span style="color:red;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&amp;#39;&amp;#39;&lt;/span&gt;))&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;What
 the code above is doing is that it is removing the characters that 
matched our search key and then subracts the length of that string to 
the original string to find the total occurrence of the character we are
 looking for.&lt;br /&gt;&lt;br /&gt;Now, if you want to use this as a function you can 
use this:&lt;br /&gt;&lt;/p&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;CREATE&lt;/span&gt; &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;FUNCTION&lt;/span&gt; udf_CountCharOccurence&lt;br /&gt;(&amp;nbsp;&amp;nbsp;&amp;nbsp; @string2check &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;varchar&lt;/span&gt;(500)&lt;br /&gt; ,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@character2find &lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;char&lt;/span&gt;&lt;br /&gt;)&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&lt;/span&gt;RETURNS &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;INT&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;RETURN&lt;/span&gt; (&lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;LEN&lt;/span&gt;(@string2check) - &lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;LEN&lt;/span&gt;(&lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;REPLACE&lt;/span&gt;(&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@string2check, &lt;br /&gt;                                @character2find)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;)&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;END&lt;/span&gt;&lt;br /&gt;GO&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;The code above works great but there&amp;#39;s a catch. If you 
are concerned with case sensitivity then the code above wont work. The 
way around it is to use COLLATION which is supported by the SQL function
 below:&lt;br /&gt;&lt;/p&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;CREATE&lt;/span&gt; &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;FUNCTION&lt;/span&gt; udf_CountCharOccurenceCaseSensitive&lt;br /&gt;(&amp;nbsp;&amp;nbsp;&amp;nbsp; @string2check &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;varchar&lt;/span&gt;(500)&lt;br /&gt; ,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@character2find &lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;char&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&lt;/span&gt;)RETURNS &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;INT&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;RETURN&lt;/span&gt; (&lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;LEN&lt;/span&gt;(@string2check) - &lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;LEN&lt;/span&gt;(&lt;span style="color:fuchsia;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;REPLACE&lt;/span&gt;(&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@string2check &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;COLLATE&lt;/span&gt; SQL_Latin1_General_Cp1_CS_AS, &lt;br /&gt;                                @character2find &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;COLLATE&lt;/span&gt; SQL_Latin1_General_Cp1_CS_AS, &lt;span style="color:red;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&amp;#39;&amp;#39;&lt;/span&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;)&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;END&lt;/span&gt;&lt;br /&gt;GO&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;In order to use this in your query all you need to do is&lt;/p&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;PRINT&lt;/span&gt; dbo.udf_CountCharOccurenceCaseSensitive(&lt;span style="color:red;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&amp;#39;This is a long text&amp;#39;&lt;/span&gt;,&lt;span style="color:red;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&amp;#39;i&amp;#39;&lt;/span&gt;)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Or
 if you want to put it to use to meet the criteria that I mentioned 
about dots on emails you can do it this way:&lt;/p&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;SELECT&lt;/span&gt; * &lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;FROM&lt;/span&gt; Users&lt;br /&gt;&lt;span style="color:blue;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;WHERE&lt;/span&gt; dbo.udf_CountCharOccurenceCaseSensitive(EmailAddress,&lt;span style="color:red;background-color:transparent;font-family:Courier New;font-size:11px;"&gt;&amp;#39;.&amp;#39;&lt;/span&gt;) &amp;gt; 2&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;
HTH&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devpinoy.org/aggbug.aspx?PostID=36186" width="1" height="1"&gt;</description><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx">SQL</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/Occurrence/default.aspx">Occurrence</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/User+Defined+Functions/default.aspx">User Defined Functions</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/Count/default.aspx">Count</category></item><item><title>Refreshing views with sp_refreshview</title><link>http://devpinoy.org/blogs/keithrull/archive/2008/08/19/refreshing-views-with-sp-refreshview.aspx</link><pubDate>Tue, 19 Aug 2008 16:00:00 GMT</pubDate><guid isPermaLink="false">99090821-4da1-4a75-98c2-a35884625ff7:26928</guid><dc:creator>keithrull</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devpinoy.org/blogs/keithrull/rsscomments.aspx?PostID=26928</wfw:commentRss><comments>http://devpinoy.org/blogs/keithrull/archive/2008/08/19/refreshing-views-with-sp-refreshview.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;One of the common problems that you would encounter when you are building applications that utilize views is that sometimes there are cases wherein a view gets out of date. This happens when you add a new column to a table a view is refrencing. &lt;/p&gt;
&lt;p&gt;To fix this some people would delete the view and recreate it but there is better solution called sp_refreshview. sp_refreshview updates the metadata for the specified non-schema-bound view. Persistent metadata for a view can become outdated because of changes to the underlying objects upon which the view depends&lt;sup&gt;&lt;span style="font-size:xx-small;"&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms187821.aspx"&gt;[description from msdn]&lt;/a&gt;&lt;/span&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s a remark from MSDN regarding sp_refreshview:&lt;/p&gt;
&lt;blockquote style="margin-right:0px;" dir="ltr"&gt;
&lt;p&gt;&lt;em&gt;&lt;span style="font-size:large;"&gt;&amp;quot;&lt;/span&gt;If a view is not created with schemabinding, &lt;b&gt;sp_refreshview&lt;/b&gt; should be run when changes are made to the objects underlying the view that affect the definition of the view. Otherwise, the view might produce unexpected results when it is queried.&lt;span style="font-size:large;"&gt;&amp;quot;&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The syntax is pretty straight-forward&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;EXECUTE sp_refreshview &amp;#39;&amp;lt;view name&amp;gt;&amp;#39;&lt;/p&gt;
&lt;p&gt;Once executed the views definition would be updated.&lt;/p&gt;
&lt;p&gt;What if I want to execute sp_refreshview on all the views in my database? The answer is to create a cursor that would execute sp_refreshview on each of your view. Below is a script that does exacrly what you need:&lt;/p&gt;
&lt;pre&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;--Refresh the underlying metadata of all views&lt;/span&gt;
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;DECLARE&lt;/span&gt; @viewName &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;VARCHAR&lt;/span&gt;(255)

&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;DECLARE&lt;/span&gt; listOfViews &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;CURSOR&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FOR&lt;/span&gt;
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;SELECT&lt;/span&gt; [name] 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FROM&lt;/span&gt; &lt;span style="font-size:11px;color:lawngreen;font-family:Courier New;background-color:transparent;"&gt;sysobjects&lt;/span&gt; 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;WHERE&lt;/span&gt; xtype = &lt;span style="font-size:11px;color:red;font-family:Courier New;background-color:transparent;"&gt;&amp;#39;V&amp;#39;&lt;/span&gt;


&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;OPEN&lt;/span&gt; listOfViews

    &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FETCH&lt;/span&gt; NEXT &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FROM&lt;/span&gt; listOfViews &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;into&lt;/span&gt; @viewName

    &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;WHILE&lt;/span&gt; (&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;@@FETCH_STATUS&lt;/span&gt; &amp;lt;&amp;gt; -1)
    &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;BEGIN&lt;/span&gt;

        &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FETCH&lt;/span&gt; NEXT &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FROM&lt;/span&gt; listOfViews &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;INTO&lt;/span&gt; @viewName
        &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:brown;font-family:Courier New;background-color:transparent;"&gt;sp_refreshview&lt;/span&gt; @viewName

    &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;END&lt;/span&gt;

&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;CLOSE&lt;/span&gt; listOfViews

&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;DEALLOCATE&lt;/span&gt; listOfViews&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;HTH&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devpinoy.org/aggbug.aspx?PostID=26928" width="1" height="1"&gt;</description><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx">SQL</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL+Tricks/default.aspx">SQL Tricks</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL+Helpers/default.aspx">SQL Helpers</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL+Tips/default.aspx">SQL Tips</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/sp_5F00_refreshview/default.aspx">sp_refreshview</category></item><item><title>sp_recompile is your friend</title><link>http://devpinoy.org/blogs/keithrull/archive/2008/08/19/sp-recompile-is-your-friend.aspx</link><pubDate>Tue, 19 Aug 2008 15:47:00 GMT</pubDate><guid isPermaLink="false">99090821-4da1-4a75-98c2-a35884625ff7:26927</guid><dc:creator>keithrull</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devpinoy.org/blogs/keithrull/rsscomments.aspx?PostID=26927</wfw:commentRss><comments>http://devpinoy.org/blogs/keithrull/archive/2008/08/19/sp-recompile-is-your-friend.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve been doing a&amp;nbsp; lot of code deployments lately and I&amp;#39;ve come across several occasions wherein my stored procedures wouldn&amp;#39;t run as fast as expected compared to it&amp;#39;s previously known&amp;nbsp;execution time. The problem lies on the statistics not getting updated after change has been made against an index or other object that may affect efficiency. Since stored procedures are compiled, recompiling them would update these statistics.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s a remark about sp_recompile&amp;nbsp;taken from the &lt;strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms181647.aspx"&gt;MSDN&lt;/a&gt;&lt;/strong&gt; website&lt;/p&gt;
&lt;blockquote style="margin-right:0px;" dir="ltr"&gt;
&lt;p&gt;&lt;em&gt;&lt;span style="font-size:large;"&gt;&amp;quot;&lt;/span&gt;The queries used by stored procedures and triggers are optimized only when they are compiled. As indexes or other changes that affect statistics are made to the database, compiled stored procedures and triggers may lose efficiency. By recompiling stored procedures and triggers that act on a table, you can reoptimize the queries.&lt;span style="font-size:large;font-family:Garamond;"&gt;&amp;quot;&lt;/span&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The syntax is pretty straight-forward:&lt;/p&gt;
&lt;blockquote style="margin-right:0px;" dir="ltr"&gt;
&lt;p&gt;EXEC sp_recompile &amp;#39;&amp;lt;name of your sp&amp;gt;&amp;#39;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Once executed the stored procedure would then be marked for recompilation and would then be recompiled on the next execution. Niffty huh?!&lt;/p&gt;
&lt;p&gt;But what if I want to recompile all my stored procedures? Well, fear not! You can use a cursor that would iterate on all the&amp;nbsp;stored procedure in your current database&amp;nbsp;and execute an sp_recompile against all of them. Below is the script to accomplish this task:&lt;/p&gt;
&lt;pre&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;--Recompile all stored procedures on the current database&lt;/span&gt;
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;DECLARE&lt;/span&gt; @StoredProcedureName &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;VARCHAR&lt;/span&gt;(255)

&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;DECLARE&lt;/span&gt; listOfStoredProcedure &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;CURSOR&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FOR&lt;/span&gt;
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;SELECT&lt;/span&gt; [Name] &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FROM&lt;/span&gt; &lt;span style="font-size:11px;color:lawngreen;font-family:Courier New;background-color:transparent;"&gt;sysobjects&lt;/span&gt; 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;WHERE&lt;/span&gt; XTtype = &lt;span style="font-size:11px;color:red;font-family:Courier New;background-color:transparent;"&gt;&amp;#39;P&amp;#39;&lt;/span&gt;


&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;OPEN&lt;/span&gt; listOfStoredProcedure

    &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FETCH&lt;/span&gt; NEXT &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FROM&lt;/span&gt; listOfStoredProcedure &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;into&lt;/span&gt; @StoredProcedureName

    &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;WHILE&lt;/span&gt; (&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;@@FETCH_STATUS&lt;/span&gt; &amp;lt;&amp;gt; -1)
    &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;BEGIN&lt;/span&gt;

        &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FETCH&lt;/span&gt; NEXT &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;FROM&lt;/span&gt; listOfStoredProcedure &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;INTO&lt;/span&gt; @StoredProcedureName
        &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;EXEC&lt;/span&gt; sp_recompile @StoredProcedureName

    &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;END&lt;/span&gt;

&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;CLOSE&lt;/span&gt; listOfStoredProcedure

&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;DEALLOCATE&lt;/span&gt; listOfStoredProcedure

GO&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;HTH&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;span style="color:#ff0000;"&gt;*Note: sp_recompile can also recompile triggers&lt;/span&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devpinoy.org/aggbug.aspx?PostID=26927" width="1" height="1"&gt;</description><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx">SQL</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/sp_5F00_recompile/default.aspx">sp_recompile</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL+utilities/default.aspx">SQL utilities</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL+Tricks/default.aspx">SQL Tricks</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL+Helpers/default.aspx">SQL Helpers</category></item><item><title>Some useful date processing SQL Snippets</title><link>http://devpinoy.org/blogs/keithrull/archive/2008/07/24/some-useful-date-processing-sql-snippets.aspx</link><pubDate>Thu, 24 Jul 2008 15:44:00 GMT</pubDate><guid isPermaLink="false">99090821-4da1-4a75-98c2-a35884625ff7:26473</guid><dc:creator>keithrull</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devpinoy.org/blogs/keithrull/rsscomments.aspx?PostID=26473</wfw:commentRss><comments>http://devpinoy.org/blogs/keithrull/archive/2008/07/24/some-useful-date-processing-sql-snippets.aspx#comments</comments><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s an update to &lt;strong&gt;&lt;a href="http://www.keithrull.com/2005/03/03/UsefulSQLDateProcessingSnippets.aspx"&gt;my blog entry 3 years ago&lt;/a&gt;&lt;/strong&gt; regarding the same topic:&lt;/p&gt;
&lt;pre&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;---Calculates the first day of the previous month&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;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()) - 1, 0) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [First &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the previous &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;---Calculates the first day of current month&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;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()), 0) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [First &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;---Calculates the first day of next month&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;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()) + 1, 0) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [First &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the next &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;---Calculates the last day of the previous month&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;DATEADD&lt;/span&gt;(d, -1, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()), 0)) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Last &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the previous &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;---Calculates the last day of the current month&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;DATEADD&lt;/span&gt;(d, -1, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()) + 1, 0)) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Last &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the last day of the next month&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;DATEADD&lt;/span&gt;(d, -1, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()) + 2, 0)) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Last &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the next &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the first day of the year&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;DATEADD&lt;/span&gt;(yy, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(yy,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;()), 0) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [First &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;year&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the first day of the quater&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;DATEADD&lt;/span&gt;(qq, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(qq,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;()), 0) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [First &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the quarter]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the first monday of the month&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;DATEADD&lt;/span&gt;(wk, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(wk,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;dateadd&lt;/span&gt;(dd, 6 - &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEPART&lt;/span&gt;(&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;Day&lt;/span&gt;,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;()),&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;())), 0) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [First monday &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the last day of the prior month&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;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(mm,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;()), 0) 
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Last &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the previous &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the last day of the prior year&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;DATEADD&lt;/span&gt;(yy, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(yy,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;()), 0)
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Last &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the previous &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;year&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the last day of the current year&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;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;() ) + 1, 0)
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Last &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;year&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the monday of the current week&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;DATEADD&lt;/span&gt;(wk, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(wk,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;()), 0)
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Monday &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; week]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the yesterdays date&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;DATEADD&lt;/span&gt;(dd, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(dd,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;getdate&lt;/span&gt;()), -1)
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Yesterdays date]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the todays date&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;GetDate&lt;/span&gt;()
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Todays date]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Calculates the tommorows date&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;DATEADD&lt;/span&gt;(dd, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(dd,0,&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;getdate&lt;/span&gt;()), 1)
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Tommorows date]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;---Calculates the 15th day of previous month&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;DATEADD&lt;/span&gt;(d, 14, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()) - 1 , 0))
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [15th &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; previous &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;---Calculates the 15th day of current month&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;DATEADD&lt;/span&gt;(d, 14, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()), 0))
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [15th &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;---Calculates the 15th day of next month&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;DATEADD&lt;/span&gt;(d, 14, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEADD&lt;/span&gt;(mm, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEDIFF&lt;/span&gt;(m, 0, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GETDATE&lt;/span&gt;()) + 1, 0))
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [15th &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;day&lt;/span&gt; &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; next &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Gets the name of the current month&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;DATENAME&lt;/span&gt;(&lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;())
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Name &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;month&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Gets the weekday name of the current date&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;DATENAME&lt;/span&gt;(dw, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;())
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Weekday name &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; date]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Gets the weekday name of the current date next year&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;DATENAME&lt;/span&gt;(dw, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEADD&lt;/span&gt;(yy, 1, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;()))
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Weekday name &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; date next &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;year&lt;/span&gt;]

&lt;span style="font-size:11px;color:teal;font-family:Courier New;background-color:transparent;"&gt;--Gets the weekday name of the current date last year&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;DATENAME&lt;/span&gt;(dw, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;DATEADD&lt;/span&gt;(yy, -1, &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;GetDate&lt;/span&gt;()))
&lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;AS&lt;/span&gt; [Weekday name &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;of&lt;/span&gt; the &lt;span style="font-size:11px;color:blue;font-family:Courier New;background-color:transparent;"&gt;current&lt;/span&gt; date last &lt;span style="font-size:11px;color:fuchsia;font-family:Courier New;background-color:transparent;"&gt;year&lt;/span&gt;]
&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;And here&amp;#39;s the result for the query above&lt;/p&gt;
&lt;pre&gt;&lt;span style="font-size:11px;color:black;font-family:Courier New;background-color:transparent;"&gt;First day of the previous month
-------------------------------
2008-06-01 00:00:00.000

(1 row(s) affected)

First day of the current month
------------------------------
2008-07-01 00:00:00.000

(1 row(s) affected)

First day of the next month
---------------------------
2008-08-01 00:00:00.000

(1 row(s) affected)

Last day of the previous month
------------------------------
2008-06-30 00:00:00.000

(1 row(s) affected)

Last day of the current month
-----------------------------
2008-07-31 00:00:00.000

(1 row(s) affected)

Last day of the next month
--------------------------
2008-08-31 00:00:00.000

(1 row(s) affected)

First day of the year
-----------------------
2008-01-01 00:00:00.000

(1 row(s) affected)

First day of the quarter
------------------------
2008-07-01 00:00:00.000

(1 row(s) affected)

First monday of the month
-------------------------
2008-07-07 00:00:00.000

(1 row(s) affected)

Last day of the previous month
------------------------------
2008-07-01 00:00:00.000

(1 row(s) affected)

Last day of the previous year
-----------------------------
2008-01-01 00:00:00.000

(1 row(s) affected)

Last day of the current year
----------------------------
2008-08-01 00:00:00.000

(1 row(s) affected)

Monday of the current week
--------------------------
2008-07-21 00:00:00.000

(1 row(s) affected)

Yesterdays date
-----------------------
2008-07-23 00:00:00.000

(1 row(s) affected)

Todays date
-----------------------
2008-07-24 11:40:57.557

(1 row(s) affected)

Tommorows date
-----------------------
2008-07-25 00:00:00.000

(1 row(s) affected)

15th day of previous month
--------------------------
2008-06-15 00:00:00.000

(1 row(s) affected)

15th day of current month
-------------------------
2008-07-15 00:00:00.000

(1 row(s) affected)

15th day of next month
-----------------------
2008-08-15 00:00:00.000

(1 row(s) affected)

Name of the current month
------------------------------
July

(1 row(s) affected)

Weekday name of the current date
--------------------------------
Thursday

(1 row(s) affected)

Weekday name of the current date next year
------------------------------------------
Friday

(1 row(s) affected)

Weekday name of the current date last year
------------------------------------------
Tuesday

(1 row(s) affected)&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;I&amp;#39;m hoping that I could update this regularly. Did I miss anything? Post it on the comments and lets start an archive of useful sql date scripts.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devpinoy.org/aggbug.aspx?PostID=26473" width="1" height="1"&gt;</description><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx">SQL</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/Date+Processing/default.aspx">Date Processing</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/Snippets/default.aspx">Snippets</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/Useful+SQL+Scripts/default.aspx">Useful SQL Scripts</category></item><item><title>How To: List databases in a SQL Server using T-SQL and truncate their logs</title><link>http://devpinoy.org/blogs/keithrull/archive/2008/01/22/how-to-list-databases-in-a-sql-server-using-t-sql-and-truncate-their-logs.aspx</link><pubDate>Tue, 22 Jan 2008 16:59:00 GMT</pubDate><guid isPermaLink="false">99090821-4da1-4a75-98c2-a35884625ff7:16475</guid><dc:creator>keithrull</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devpinoy.org/blogs/keithrull/rsscomments.aspx?PostID=16475</wfw:commentRss><comments>http://devpinoy.org/blogs/keithrull/archive/2008/01/22/how-to-list-databases-in-a-sql-server-using-t-sql-and-truncate-their-logs.aspx#comments</comments><description>&lt;p&gt;Finally i&amp;#39;m back after &lt;strong&gt;&lt;a class="" href="http://www.keithrull.com/2008/01/11/MyWebhostFrustration.aspx"&gt;a month of crazyness due to my webhost&lt;/a&gt;&lt;/strong&gt; not performing up to its promise of 99.9% uptime.&lt;/p&gt;
&lt;p&gt;Enough with the rant and on with the article, today i&amp;#39;m going to talk about how to list databases in a SQL Server by just using T-SQL and then showing you how to truncate the logs on those databases in different ways.&lt;/p&gt;
&lt;p&gt;First, let&amp;#39;s look at the 5 different approaches that you can use to list down databases in SQL Server by just using plain old T-SQL.&lt;/p&gt;
&lt;p&gt;The stored procedure approach ( &lt;a href="http://msdn2.microsoft.com/en-us/library/ms176070.aspx"&gt;&lt;strong&gt;sp_databases&lt;/strong&gt;&lt;/a&gt;, &lt;a href="http://msdn2.microsoft.com/en-us/library/ms178568.aspx"&gt;&lt;strong&gt;sp_helpdb&lt;/strong&gt;&lt;/a&gt;&amp;nbsp;)&amp;nbsp;:&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT:0px;"&gt;&lt;pre&gt;&lt;span style="FONT-SIZE:11px;COLOR:black;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt; &lt;pre&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;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- Lists databases that either reside in an instance of the SQL Server &lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- 2005 Database Engine or are accessible through a database gateway.&lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;EXEC&lt;/span&gt; sp_databases

&lt;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- Reports information about a specified database or all databases.&lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;EXEC&lt;/span&gt; sp_helpdb&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;The sys tables approach ( &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms178534.aspx"&gt;sys.databases&lt;/a&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms179900.aspx"&gt;sys.sysdatabases&lt;/a&gt;&lt;/strong&gt; ):&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT:0px;"&gt;&lt;pre&gt;&lt;span style="FONT-SIZE:11px;COLOR:black;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&lt;span style="FONT-SIZE:11px;COLOR:lawngreen;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&lt;pre&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;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- [SQL Server 2000/2005] Contains one row for each database in an instance of &lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- Microsoft SQL Server 2005. When SQL Server is first installed, sysdatabases &lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- contains entries for the master, model, msdb, and tempdb databases. &lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;SELECT&lt;/span&gt; NAME &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;FROM&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:lawngreen;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;sysdatabases&lt;/span&gt;

&lt;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- [SQL Server 2005] Contains one row per database in the instance of Microsoft SQL Server. &lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;SELECT&lt;/span&gt; NAME &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;FROM&lt;/span&gt; sys.&lt;span style="FONT-SIZE:11px;COLOR:lawngreen;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;sysdatabases&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;The using the undocumented stored procedure sp_MsForEachDatabase approach&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT:0px;"&gt;&lt;pre&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;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- Undocumented SQL Server stored procedure&lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;EXEC&lt;/span&gt; sp_msForEachDB &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;#39;PRINT &amp;#39;&amp;#39;?&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Simple huh? Now that we know how to list databases&amp;nbsp;let&amp;#39;s go back to the problem of truncating all of them in one query (in fact it&amp;#39;s just one line!). Some people would suggest writing cursors that would loop thru all the rows returned&amp;nbsp;by our sysdatabases query&amp;nbsp;to do this task. Their solution might be similar to the one listed below:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[SOLUTION 1]&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT:0px;"&gt;&lt;pre&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;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;DECLARE&lt;/span&gt; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DBNames &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;CURSOR&lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;FOR&lt;/span&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; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NAME 
&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; &lt;span style="FONT-SIZE:11px;COLOR:lawngreen;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;sysdatabases&lt;/span&gt;

&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;OPEN&lt;/span&gt; DBNames

&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;DECLARE&lt;/span&gt; @Name &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;varchar&lt;/span&gt;(50)

&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;FETCH&lt;/span&gt; NEXT &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;FROM&lt;/span&gt; DBNames
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;INTO&lt;/span&gt; @Name

&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;WHILE&lt;/span&gt; (&lt;span style="FONT-SIZE:11px;COLOR:fuchsia;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;@@FETCH_STATUS&lt;/span&gt; &amp;lt;&amp;gt; -1)
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;BEGIN&lt;/span&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;DBCC&lt;/span&gt; SHRINKDATABASE( @Name , 0)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;FETCH&lt;/span&gt; NEXT &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;FROM&lt;/span&gt; DBNames
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;INTO&lt;/span&gt; @Name

&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;END&lt;/span&gt;

&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;CLOSE&lt;/span&gt; DBNames
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;DEALLOCATE&lt;/span&gt; DBNames&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Others would suggest a more primitive approach which is building a simple query first that would list the names of the database appended with the DBCC SHRINKDATABASE command:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[SOLUTION 2]&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT:0px;"&gt;&lt;pre&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;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;SELECT&lt;/span&gt; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;#39;DBCC SHRINKDATABASE(&amp;#39;&lt;/span&gt; + NAME  + &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;#39;, 0)&amp;#39;&lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;FROM&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:lawngreen;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;sysdatabases&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Then they would change the output option of the query into &amp;quot;Results to Text&amp;quot;, copy the result to a new query window and execute the query from there. Pretty primitive. Alot of steps. Same results.&lt;/p&gt;
&lt;p&gt;My suggested solution is using the sp_MSForEachDatabase&amp;nbsp;procedure in conjunction with the DBCC SHRINKDATABASE function. This would result into a 1 line query. Less code with less steps to do that creates the same results.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[MY RECOMMENDED SOLUTION]&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT:0px;"&gt;&lt;pre&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;span style="FONT-SIZE:11px;COLOR:teal;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;-- truncates all the logs of all database in the server&lt;/span&gt;
&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;EXEC&lt;/span&gt; sp_msForEachDB &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;#39;DBCC SHRINKDATABASE( &amp;#39;&amp;#39;?&amp;#39;&amp;#39;, 0)&amp;#39;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Simple and straight to the point.&lt;/p&gt;
&lt;p&gt;I hope you learned something from our article today. You can also do this programmatically by using C# and VB.NET. I wrote two articles about that topic &lt;strong&gt;&lt;a href="http://www.keithrull.com/2006/06/07/ProgrammaticallyListAllDatabasesInASQLServer.aspx"&gt;here&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="http://www.keithrull.com/2006/06/09/ProgrammaticallyListAllSQLServersAndTheirDatabasesUsingSMO.aspx"&gt;here&lt;/a&gt;&lt;/strong&gt;. Interested in &lt;strong&gt;&lt;a href="http://www.keithrull.com/2007/09/07/HowToTruncateMultipleTablesInSQLServerAndTheMagicOfSpMSforeachtable.aspx"&gt;truncating tables using sp_MsForEachTable&lt;/a&gt;&lt;/strong&gt;?&amp;nbsp;You can checkout this post.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devpinoy.org/aggbug.aspx?PostID=16475" width="1" height="1"&gt;</description><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx">SQL</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/sp_5F00_MSforeachtable/default.aspx">sp_MSforeachtable</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/Truncate+Table/default.aspx">Truncate Table</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/DBCC/default.aspx">DBCC</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/sysdatabases/default.aspx">sysdatabases</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/sys.sysdatabases/default.aspx">sys.sysdatabases</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/Truncate+Database+Logs/default.aspx">Truncate Database Logs</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SHRINKDATABASE/default.aspx">SHRINKDATABASE</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/sp_5F00_msForEachDB/default.aspx">sp_msForEachDB</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/sp_5F00_databaases/default.aspx">sp_databaases</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/sp_5F00_helpdb/default.aspx">sp_helpdb</category></item><item><title>How to: Get a list of fixed drives and their free space inside SQL Server</title><link>http://devpinoy.org/blogs/keithrull/archive/2007/09/07/how-to-get-a-list-of-fixed-drives-and-their-free-space-inside-sql-server.aspx</link><pubDate>Fri, 07 Sep 2007 15:30:00 GMT</pubDate><guid isPermaLink="false">99090821-4da1-4a75-98c2-a35884625ff7:13609</guid><dc:creator>keithrull</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devpinoy.org/blogs/keithrull/rsscomments.aspx?PostID=13609</wfw:commentRss><comments>http://devpinoy.org/blogs/keithrull/archive/2007/09/07/how-to-get-a-list-of-fixed-drives-and-their-free-space-inside-sql-server.aspx#comments</comments><description>&lt;p&gt;I didn&amp;#39;t know that I could do this inside SQL Server&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;EXEC&lt;/span&gt; master..&lt;span style="FONT-SIZE:11px;COLOR:brown;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;xp_fixeddrives&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Executing the procedure onmy server&amp;nbsp;gave me this result set:&lt;/p&gt;
&lt;p&gt;drive&amp;nbsp;&amp;nbsp;MB free&lt;br /&gt;C&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5897&lt;br /&gt;E&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;33334&lt;/p&gt;
&lt;p&gt;Man, I think it&amp;#39;s about time I upgrade and brush up&amp;nbsp;my SQL skills. I don&amp;#39;t know why you would do it inside SQL Server. But then again it&amp;#39;s pretty cool to know that something like this exist. You never know, you might need it someday.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devpinoy.org/aggbug.aspx?PostID=13609" width="1" height="1"&gt;</description><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx">SQL</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/xp_5F00_fixeddrives/default.aspx">xp_fixeddrives</category></item><item><title>How To: List All Available .NET Data Providers </title><link>http://devpinoy.org/blogs/keithrull/archive/2006/08/27/How-To_3A00_-List-All-Available-.NET-Data-Providers-.aspx</link><pubDate>Sun, 27 Aug 2006 18:51:00 GMT</pubDate><guid isPermaLink="false">99090821-4da1-4a75-98c2-a35884625ff7:4552</guid><dc:creator>keithrull</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devpinoy.org/blogs/keithrull/rsscomments.aspx?PostID=4552</wfw:commentRss><comments>http://devpinoy.org/blogs/keithrull/archive/2006/08/27/How-To_3A00_-List-All-Available-.NET-Data-Providers-.aspx#comments</comments><description>&lt;p&gt;.NET 2.0 introduces to us the class known as DBProviderFactories which enables us to access available providers on a machine. the DBProviderFactories class has method called GetFactoryClasses() that returns a DataTable that contains&amp;nbsp;any class that is registered on the GAC that implements the DBProviderFactory class.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[ C# Version&amp;nbsp;]&lt;/strong&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:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;using&lt;/span&gt; System.Data;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;using&lt;/span&gt; System.Data.Common;&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;namespace&lt;/span&gt; KeithRull.ListAllAvailableDataProviders&lt;br /&gt;{&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;class&lt;/span&gt; Program&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;static&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DataTable providersTable &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;=&lt;/span&gt; DbProviderFactories.GetFactoryClasses();&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:green;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//loop thru each provider in the providers data table&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&lt;/span&gt; (DataRow providerRow &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;in&lt;/span&gt; providersTable.Rows)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:green;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//loop thru each column in the providers table&lt;/span&gt;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;foreach&lt;/span&gt; (DataColumn providerColumn &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;in&lt;/span&gt; providersTable.Columns)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:green;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//print the information&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(providerColumn.ColumnName &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;+&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:#666666;FONT-FAMILY:Courier New;BACKGROUND-COLOR:#e4e4e4;"&gt;&amp;quot;: &amp;quot;&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;+&lt;/span&gt; providerRow[providerColumn].ToString());&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:green;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//create a extra line feed for data separation&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&lt;span style="FONT-SIZE:11px;COLOR:#666666;FONT-FAMILY:Courier New;BACKGROUND-COLOR:#e4e4e4;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:green;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//show the information to the screen&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.ReadLine();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[&amp;nbsp;VB.NET Version&amp;nbsp;]&lt;/strong&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:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Imports&lt;/span&gt; System&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Imports&lt;/span&gt; System.Data&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Imports&lt;/span&gt; System.Data.Common&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Namespace&lt;/span&gt; KeithRull.ListAllAvailableDataProviders&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Class&lt;/span&gt; Program&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Shared&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Sub&lt;/span&gt; Main(&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;ByVal&lt;/span&gt; args() &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;As&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;String&lt;/span&gt;)&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/span&gt; providersTable &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;As&lt;/span&gt; DataTable &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;=&lt;/span&gt; DbProviderFactories.GetFactoryClasses()&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/span&gt; providerRow &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;As&lt;/span&gt; DataRow&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Each&lt;/span&gt; providerRow &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;In&lt;/span&gt; providersTable.Rows&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dim&lt;/span&gt; providerColumn &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;As&lt;/span&gt; DataColumn&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Each&lt;/span&gt; providerColumn &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;In&lt;/span&gt; providersTable.Columns&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(providerColumn.ColumnName &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;+&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:#666666;FONT-FAMILY:Courier New;BACKGROUND-COLOR:#e4e4e4;"&gt;&amp;quot;: &amp;quot;&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:red;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;+&lt;/span&gt; providerRow(providerColumn).ToString())&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&lt;span style="FONT-SIZE:11px;COLOR:#666666;FONT-FAMILY:Courier New;BACKGROUND-COLOR:#e4e4e4;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;)&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Next&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.ReadLine()&lt;br /&gt;&lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&lt;/span&gt; &lt;span style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Sub&lt;/span&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 style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Class&lt;/span&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 style="FONT-SIZE:11px;COLOR:blue;FONT-FAMILY:Courier New;BACKGROUND-COLOR:transparent;"&gt;Namespace&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devpinoy.org/aggbug.aspx?PostID=4552" width="1" height="1"&gt;</description><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/.NET+How+To_2700_s/default.aspx">.NET How To's</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx">SQL</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/DataTable/default.aspx">DataTable</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/.NET+Data+Providers/default.aspx">.NET Data Providers</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/GetFactoryClasses_28002900_/default.aspx">GetFactoryClasses()</category><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/DBProviderFactories/default.aspx">DBProviderFactories</category></item><item><title>HowTo: List Database Objects in SQL Server using sysobjects, syscolumns and systypes</title><link>http://devpinoy.org/blogs/keithrull/archive/2006/06/17/HowTo_3A00_-List-Database-Objects-in-SQL-Server-using-sysobjects_2C00_-syscolumns-and-systypes.aspx</link><pubDate>Sat, 17 Jun 2006 17:52:00 GMT</pubDate><guid isPermaLink="false">99090821-4da1-4a75-98c2-a35884625ff7:3471</guid><dc:creator>keithrull</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devpinoy.org/blogs/keithrull/rsscomments.aspx?PostID=3471</wfw:commentRss><comments>http://devpinoy.org/blogs/keithrull/archive/2006/06/17/HowTo_3A00_-List-Database-Objects-in-SQL-Server-using-sysobjects_2C00_-syscolumns-and-systypes.aspx#comments</comments><description>&lt;P&gt;I have been working more and more with data recently and its a painstaking task. Alot of my time includes analysis of trades and financial information to create and calculate error free result sets.&lt;/P&gt;
&lt;P&gt;This also includes data cleanup..&lt;/P&gt;
&lt;P&gt;My task today was to create positional and transactional data for investments. Since the company that i am working for right now is thinking of consolidating the data into one system instead of two(theres two system that basically handles the same thing).This involves digging into one of the biggest database that i have seen and finding out to properly extract data without any idea on how they structured their database (the 3rd party vendor is selling us services to do this but the company declined since it would cost alot of money.)&lt;/P&gt;
&lt;P&gt;So here i am digging to every column, every table, every function&amp;nbsp;and every view. I realized that it would be pretty hard to manually go to every database to discover what columns it contain(since there was no db diagram provided by the vendor). &lt;/P&gt;
&lt;P&gt;The programmer inside me thought of using SQL SMO/DMO since this would just require me tho modify one of my previous demo regarding this subject.&lt;/P&gt;
&lt;P&gt;but... i decided not too since this would be just a one time thing and can be accomplished using sql scripts and just pasting the resultset to Excel so that i could have a suitable documentation for the database structure.&lt;/P&gt;
&lt;P&gt;One thing to remember is that&amp;nbsp;&lt;STRONG&gt;sysobjects&lt;/STRONG&gt;, &lt;STRONG&gt;syscolumns&lt;/STRONG&gt; and &lt;STRONG&gt;systypes&lt;/STRONG&gt; are your friends. This are the database objects that we would be using for this demo.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Sysobjects&lt;/STRONG&gt; contains information about each object in the database. this includes the object name, the user id of the user who created this object and many other useful information. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;EM&gt;select * from sysobjects;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;The most useful column in this table is the &lt;STRONG&gt;xtype&lt;/STRONG&gt; column. This column signifies what type of object is in the returned rowset. specifying &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;select * from sysobjects where xtype = 'u'&lt;/EM&gt; ;&lt;/P&gt;
&lt;P&gt;will return the list of tables on the current database. Here's a list of possible values for xtype:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;C&amp;nbsp;: CHECK constraint 
&lt;LI&gt;D&amp;nbsp;: Default or DEFAULT constraint 
&lt;LI&gt;F&amp;nbsp;: FOREIGN KEY constraint 
&lt;LI&gt;L&amp;nbsp;: Log 
&lt;LI&gt;P&amp;nbsp;: Stored procedure 
&lt;LI&gt;PK :&amp;nbsp;PRIMARY KEY constraint (type is K) 
&lt;LI&gt;RF&amp;nbsp;: Replication filter stored procedure 
&lt;LI&gt;S&amp;nbsp;: System tables 
&lt;LI&gt;TR&amp;nbsp;: Triggers 
&lt;LI&gt;U&amp;nbsp;: User table 
&lt;LI&gt;UQ&amp;nbsp;: UNIQUE constraint (type is K) 
&lt;LI&gt;V&amp;nbsp;: Views 
&lt;LI&gt;X&amp;nbsp;: Extended stored procedure&lt;/LI&gt;
&lt;LI&gt;TF : Functions&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;You can use &lt;STRONG&gt;syscolumns&lt;/STRONG&gt;&amp;nbsp; to retrieve columns on the database. doing&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;EM&gt;select * from syscolumns;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;would return a result containing column information which you can use to determine the scale, data type, precision and etc. The &lt;STRONG&gt;xtype&lt;/STRONG&gt; on the &lt;STRONG&gt;syscolumns&lt;/STRONG&gt; table acts differently from the sysobjects. the xtype column here represents the datatatype of that column. running this script:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;EM&gt;select * from syscolumns where xtype = 167;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;will return all columns that has a datatype of varchar. heres a list of possible values for this xtype column:&lt;/P&gt;
&lt;P&gt;127&amp;nbsp;: bigint&lt;BR&gt;173&amp;nbsp;:&amp;nbsp;&amp;nbsp;binary&lt;BR&gt;104&amp;nbsp;:&amp;nbsp;&amp;nbsp;bit&lt;BR&gt;175&amp;nbsp;:&amp;nbsp;&amp;nbsp;char&lt;BR&gt;61&amp;nbsp;:&amp;nbsp;&amp;nbsp;datetime&lt;BR&gt;106&amp;nbsp;:&amp;nbsp;&amp;nbsp;decimal&lt;BR&gt;62&amp;nbsp;:&amp;nbsp;&amp;nbsp;float&lt;BR&gt;34&amp;nbsp;:&amp;nbsp;&amp;nbsp;image&lt;BR&gt;56&amp;nbsp;:&amp;nbsp;&amp;nbsp;int&lt;BR&gt;60&amp;nbsp;:&amp;nbsp;&amp;nbsp;money&lt;BR&gt;239&amp;nbsp;:&amp;nbsp;&amp;nbsp;nchar&lt;BR&gt;99&amp;nbsp;:&amp;nbsp;&amp;nbsp;ntext&lt;BR&gt;108&amp;nbsp;:&amp;nbsp;&amp;nbsp;numeric&lt;BR&gt;231&amp;nbsp;:&amp;nbsp;&amp;nbsp;nvarchar&lt;BR&gt;59&amp;nbsp;:&amp;nbsp;&amp;nbsp;real&lt;BR&gt;58&amp;nbsp;:&amp;nbsp;&amp;nbsp;smalldatetime&lt;BR&gt;52&amp;nbsp;:&amp;nbsp;&amp;nbsp;smallint&lt;BR&gt;122&amp;nbsp;:&amp;nbsp;&amp;nbsp;smallmoney&lt;BR&gt;98&amp;nbsp;:&amp;nbsp;&amp;nbsp;sql_variant&lt;BR&gt;231&amp;nbsp;:&amp;nbsp;&amp;nbsp;sysname&lt;BR&gt;35&amp;nbsp;:&amp;nbsp;&amp;nbsp;text&lt;BR&gt;189&amp;nbsp;:&amp;nbsp;&amp;nbsp;timestamp&lt;BR&gt;48&amp;nbsp;:&amp;nbsp;&amp;nbsp;tinyint&lt;BR&gt;36&amp;nbsp;:&amp;nbsp;&amp;nbsp;uniqueidentifier&lt;BR&gt;165&amp;nbsp;:&amp;nbsp;&amp;nbsp;varbinary&lt;BR&gt;167&amp;nbsp;:&amp;nbsp;&amp;nbsp;varchar&lt;/P&gt;
&lt;P&gt;I got this xtypes by running : &lt;EM&gt;select xtype, name from systypes;&lt;/EM&gt; which basically contains a list of available sql datatypes.&lt;/P&gt;
&lt;P&gt;Putting it all together: So my idea behind this article was to create a list of available tables, and views with their underlying columns. The idea is that i can just join the sysobjects to the syscolumns to get the the tables columns and the link it to systypes to get the column datatype. The complete scripts are listed below as reference to how i was able to accomplish this task.&lt;/P&gt;
&lt;P&gt;[code language="T-SQL"]&lt;/P&gt;
&lt;P&gt;-----------------------------------------&lt;BR&gt;-- List only the tables in this database&lt;BR&gt;-----------------------------------------&lt;BR&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;o.name AS [Table Name], &lt;BR&gt;&amp;nbsp;o.type, &lt;BR&gt;&amp;nbsp;c.name AS [Col Name], &lt;BR&gt;&amp;nbsp;s.name AS [Col Type], &lt;BR&gt;&amp;nbsp;c.prec, &lt;BR&gt;&amp;nbsp;c.scale, &lt;BR&gt;&amp;nbsp;c.isnullable&lt;BR&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;dbo.sysobjects AS o &lt;BR&gt;INNER JOIN&lt;BR&gt;&amp;nbsp;dbo.syscolumns AS c &lt;BR&gt;&amp;nbsp;&amp;nbsp;ON &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;c.id = o.id &lt;BR&gt;INNER JOIN&lt;BR&gt;&amp;nbsp;dbo.systypes AS s &lt;BR&gt;&amp;nbsp;&amp;nbsp;ON &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;c.xtype = s.xtype&lt;BR&gt;WHERE&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;o.type = 'U'&lt;BR&gt;)&lt;/P&gt;
&lt;P&gt;-----------------------------------------&lt;BR&gt;-- List only the views in this database&lt;BR&gt;-----------------------------------------&lt;BR&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;o.name AS [View Name], &lt;BR&gt;&amp;nbsp;o.type, &lt;BR&gt;&amp;nbsp;c.name AS [Col Name], &lt;BR&gt;&amp;nbsp;s.name AS [Col Type], &lt;BR&gt;&amp;nbsp;c.prec, &lt;BR&gt;&amp;nbsp;c.scale, &lt;BR&gt;&amp;nbsp;c.isnullable&lt;BR&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;dbo.sysobjects AS o &lt;BR&gt;INNER JOIN&lt;BR&gt;&amp;nbsp;dbo.syscolumns AS c &lt;BR&gt;&amp;nbsp;&amp;nbsp;ON &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;c.id = o.id &lt;BR&gt;INNER JOIN&lt;BR&gt;&amp;nbsp;dbo.systypes AS s &lt;BR&gt;&amp;nbsp;&amp;nbsp;ON &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;c.xtype = s.xtype&lt;BR&gt;WHERE&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;o.type = 'V'&lt;BR&gt;)&lt;/P&gt;
&lt;P&gt;-----------------------------------------&lt;BR&gt;-- List only the functions in this database&lt;BR&gt;-----------------------------------------&lt;BR&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;o.name AS [Funtion Name], &lt;BR&gt;&amp;nbsp;o.type, &lt;BR&gt;&amp;nbsp;c.name AS [Col Name], &lt;BR&gt;&amp;nbsp;s.name AS [Col Type], &lt;BR&gt;&amp;nbsp;c.prec, &lt;BR&gt;&amp;nbsp;c.scale, &lt;BR&gt;&amp;nbsp;c.isnullable&lt;BR&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;dbo.sysobjects AS o &lt;BR&gt;INNER JOIN&lt;BR&gt;&amp;nbsp;dbo.syscolumns AS c &lt;BR&gt;&amp;nbsp;&amp;nbsp;ON &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;c.id = o.id &lt;BR&gt;INNER JOIN&lt;BR&gt;&amp;nbsp;dbo.systypes AS s &lt;BR&gt;&amp;nbsp;&amp;nbsp;ON &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;c.xtype = s.xtype&lt;BR&gt;WHERE&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;o.type = 'TF'&lt;BR&gt;)&lt;/P&gt;
&lt;P&gt;[/code]&lt;/P&gt;
&lt;P&gt;hmm.. now i need to go back to work :) have fun!&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devpinoy.org/aggbug.aspx?PostID=3471" width="1" height="1"&gt;</description><category domain="http://devpinoy.org/blogs/keithrull/archive/tags/SQL/default.aspx">SQL</category></item></channel></rss>