<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hibbins.com &#187; TSQL</title>
	<atom:link href="http://www.hibbins.com/category/softwaredevelopment/code/tsql-code-softwaredevelopment/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hibbins.com</link>
	<description>Software Development: C#, SQL Server, .NET, Analysis Services, Reporting Services, Intergration Services, OLAP, PHP, HTML, XML, CSS, Java, Javascript and anything else technical, complex and hibbins.com</description>
	<lastBuildDate>Wed, 04 Jan 2012 11:42:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>TSQL Date Only Function</title>
		<link>http://www.hibbins.com/2009/11/24/t-sql-date-only-function/</link>
		<comments>http://www.hibbins.com/2009/11/24/t-sql-date-only-function/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 07:00:38 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MSSQL2005]]></category>
		<category><![CDATA[MSSQL2008]]></category>
		<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=243</guid>
		<description><![CDATA[CREATE FUNCTION &#91;dbo&#93;.&#91;DateOnly&#93; &#40;@DATE DATETIME&#41; RETURNS DATETIME BEGIN RETURN DATEADD&#40;d, DATEDIFF&#40;d, 0, @DATE&#41;, 0&#41; END]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>DateOnly<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">DATE</span> DATETIME<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">RETURNS</span> DATETIME
<span style="color: #993333; font-weight: bold;">BEGIN</span>
    <span style="color: #993333; font-weight: bold;">RETURN</span> DATEADD<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">END</span></pre></div></div>

<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2009%2F11%2F24%2Ft-sql-date-only-function%2F';
  addthis_title  = 'TSQL+Date+Only+Function';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2009/11/24/t-sql-date-only-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TSQL Last Day Of The Month</title>
		<link>http://www.hibbins.com/2009/11/23/tsql-last-day-of-the-month/</link>
		<comments>http://www.hibbins.com/2009/11/23/tsql-last-day-of-the-month/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 11:43:45 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[MSSQL2005]]></category>
		<category><![CDATA[MSSQL2008]]></category>
		<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=230</guid>
		<description><![CDATA[CREATE FUNCTION &#91;dbo&#93;.&#91;LastDayOfTheMonth&#93; &#40;@DATE DATETIME &#41; RETURNS DATETIME BEGIN RETURN dateadd&#40;ms, -3, DATEADD&#40;mm, DATEDIFF&#40;m, 0, @DATE&#41; + 1, 0&#41;&#41; END]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>LastDayOfTheMonth<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">DATE</span> DATETIME <span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">RETURNS</span> DATETIME
<span style="color: #993333; font-weight: bold;">BEGIN</span>
    <span style="color: #993333; font-weight: bold;">RETURN</span> dateadd<span style="color: #66cc66;">&#40;</span>ms<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>mm<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>m<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">DATE</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">END</span></pre></div></div>

<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2009%2F11%2F23%2Ftsql-last-day-of-the-month%2F';
  addthis_title  = 'TSQL+Last+Day+Of+The+Month';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2009/11/23/tsql-last-day-of-the-month/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server Schema,Table,Function and Stored procedure Auditing</title>
		<link>http://www.hibbins.com/2008/12/17/sql-server-schematablefunction-and-stored-procedure-auditing/</link>
		<comments>http://www.hibbins.com/2008/12/17/sql-server-schematablefunction-and-stored-procedure-auditing/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 16:09:47 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[MSSQL2005]]></category>
		<category><![CDATA[MSSQL2008]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[Audit]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[SQL Server Schema]]></category>
		<category><![CDATA[Stored Procedure]]></category>
		<category><![CDATA[Table]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=187</guid>
		<description><![CDATA[If you like to know what&#8217;s changing on your SQL Server then the following code creates an audit table and the associated trigger for logging the changes The Audit Object Table CREATE TABLE &#91;dbo&#93;.&#91;AuditObjects&#93;&#40; &#91;EventID&#93; &#91;INT&#93; IDENTITY&#40;1,1&#41; NOT NULL ,&#91;EventData&#93; &#91;xml&#93; NULL PRIMARY KEY CLUSTERED &#40; &#91;EventID&#93; ASC &#41; WITH &#40;IGNORE_DUP_KEY = OFF&#41; ON &#91;PRIMARY&#93; [...]]]></description>
			<content:encoded><![CDATA[<p>If you like to know what&#8217;s changing on your SQL Server then the following code creates an audit table and the associated trigger for logging the changes</p>
<p>The Audit Object Table</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>AuditObjects<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>
 <span style="color: #66cc66;">&#91;</span>EventID<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">IDENTITY</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>EventData<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>xml<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">NULL</span>
<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> CLUSTERED <span style="color: #66cc66;">&#40;</span>
   <span style="color: #66cc66;">&#91;</span>EventID<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ASC</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #66cc66;">&#40;</span>IGNORE_DUP_KEY <span style="color: #66cc66;">=</span> OFF<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">PRIMARY</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>And the associated Trigger</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> <span style="color: #66cc66;">&#91;</span>TriggerAuditObjects<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DATABASE</span>
<span style="color: #993333; font-weight: bold;">FOR</span> DDL_DATABASE_LEVEL_EVENTS
<span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>AuditObjects<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>EventData<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> EVENTDATA<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">GO</span>
ENABLE <span style="color: #993333; font-weight: bold;">TRIGGER</span> <span style="color: #66cc66;">&#91;</span>TriggerAuditObjects<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">DATABASE</span></pre></div></div>

<p>You can now use xpath queries to analyse the data</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2008%2F12%2F17%2Fsql-server-schematablefunction-and-stored-procedure-auditing%2F';
  addthis_title  = 'SQL+Server+Schema%2CTable%2CFunction+and+Stored+procedure+Auditing';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2008/12/17/sql-server-schematablefunction-and-stored-procedure-auditing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Search Stored Procedures</title>
		<link>http://www.hibbins.com/2008/12/05/search-stored-procedures/</link>
		<comments>http://www.hibbins.com/2008/12/05/search-stored-procedures/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 09:21:50 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[MSSQL2005]]></category>
		<category><![CDATA[MSSQL2008]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[Find]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[Replace]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Stored Procedures]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=173</guid>
		<description><![CDATA[Ever had the need to search stored procedures to find redundant tables or text that needs replacing or re-factoring ? Here&#8217;s a script to help (Sorry for the cursor, I&#8217;m sad with myself for using it ) -- Search string DECLARE @SearchString nvarchar&#40;50&#41; SET @SearchString = 'Search String' &#160; -- Required Declares DECLARE @getdbname sysname [...]]]></description>
			<content:encoded><![CDATA[<p>Ever had the need to search stored procedures to find redundant tables or text that needs replacing or re-factoring ?</p>
<p>Here&#8217;s a script to help (Sorry for the cursor, I&#8217;m sad with myself for using it <img src='http://www.hibbins.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' />  )</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- Search string</span>
<span style="color: #993333; font-weight: bold;">DECLARE</span> @SearchString nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SET</span> @SearchString <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Search String'</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Required Declares</span>
<span style="color: #993333; font-weight: bold;">DECLARE</span> @getdbname sysname
<span style="color: #993333; font-weight: bold;">DECLARE</span> @sqlstm nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">DECLARE</span> SeachCursor cursor <span style="color: #993333; font-weight: bold;">FOR</span> 
<span style="color: #808080; font-style: italic;">-- Get All The Names into the SeachCursor</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'['</span><span style="color: #66cc66;">+</span>name<span style="color: #66cc66;">+</span><span style="color: #ff0000;">']'</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#91;</span>master<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>sysdatabases<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> name 
<span style="color: #993333; font-weight: bold;">OPEN</span> SeachCursor
<span style="color: #808080; font-style: italic;">-- Add the Search Pattern</span>
<span style="color: #993333; font-weight: bold;">SET</span> @SearchString <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'%'</span> <span style="color: #66cc66;">+</span> @SearchString <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'%'</span>
<span style="color: #808080; font-style: italic;">--Get the first Name</span>
FETCH <span style="color: #993333; font-weight: bold;">NEXT</span> <span style="color: #993333; font-weight: bold;">FROM</span> SeachCursor <span style="color: #993333; font-weight: bold;">INTO</span> @getdbname
WHILE @@FETCH_STATUS<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span>
	<span style="color: #993333; font-weight: bold;">BEGIN</span>
	<span style="color: #808080; font-style: italic;">--set the statement to define the search condition, with variables</span>
	<span style="color: #993333; font-weight: bold;">SET</span> @sqlstm <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'
	SELECT [SP].[Specific_Catalog] AS [Database_Name], [SP].[Routine_Name] AS [Stored Procedure Name],[SP].[Routine_Definition] AS [Routine_Definition]
	FROM '</span><span style="color: #66cc66;">+</span> @getdbname<span style="color: #66cc66;">+</span><span style="color: #ff0000;">'.[Information_Schema].[Routines] AS [SP]
	WHERE PatIndex('</span><span style="color: #66cc66;">+</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">+</span>@SearchString<span style="color: #66cc66;">+</span><span style="color: #ff0000;">''</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">+</span><span style="color: #ff0000;">', [Routine_Definition]) &gt; 0'</span>
	<span style="color: #808080; font-style: italic;">--Execute the Query</span>
	<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@sqlstm<span style="color: #66cc66;">&#41;</span>
	FETCH <span style="color: #993333; font-weight: bold;">NEXT</span> <span style="color: #993333; font-weight: bold;">FROM</span> SeachCursor <span style="color: #993333; font-weight: bold;">INTO</span> @getdbname
	<span style="color: #993333; font-weight: bold;">END</span>
<span style="color: #808080; font-style: italic;">--Close the Cursor and Deallocate it from memory</span>
CLOSE SeachCursor
DEALLOCATE SeachCursor</pre></div></div>

<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2008%2F12%2F05%2Fsearch-stored-procedures%2F';
  addthis_title  = 'Search+Stored+Procedures';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2008/12/05/search-stored-procedures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL 2008 Enable Changes To Tables</title>
		<link>http://www.hibbins.com/2008/10/08/sql-2008-enable-changes-to-tables/</link>
		<comments>http://www.hibbins.com/2008/10/08/sql-2008-enable-changes-to-tables/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 13:23:34 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[MSSQL2008]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[Microsoft SQL Server Management Studio]]></category>
		<category><![CDATA[not permitted]]></category>
		<category><![CDATA[re-created]]></category>
		<category><![CDATA[re-creation]]></category>
		<category><![CDATA[saving]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=168</guid>
		<description><![CDATA[If you get the error in the SQL 2008 Management Studio : &#8220;Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can&#8217;t be re-created or enabled the option Prevent saving changes that require the table to [...]]]></description>
			<content:encoded><![CDATA[<p>If you get the error in the SQL 2008 Management Studio :</p>
<p><em>&#8220;Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can&#8217;t be re-created or enabled the option Prevent saving changes that require the table to be re-created.&#8221;</em></p>
<p>You can enable changes inside the Microsoft SQL Server Management Studio by going to &#8216;Tools|Options|Designers&#8217; then unchecking the &#8216;Prevent saving changes that require table re-creation&#8217; option</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2008%2F10%2F08%2Fsql-2008-enable-changes-to-tables%2F';
  addthis_title  = 'SQL+2008+Enable+Changes+To+Tables';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2008/10/08/sql-2008-enable-changes-to-tables/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>TSQL Count Number Of Stored Procedures, Views, Tables or Functions</title>
		<link>http://www.hibbins.com/2008/09/04/tsql-count-number-of-stored-procedures-views-tables-or-functions/</link>
		<comments>http://www.hibbins.com/2008/09/04/tsql-count-number-of-stored-procedures-views-tables-or-functions/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 10:19:53 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MSSQL2005]]></category>
		<category><![CDATA[MSSQL2008]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[Count]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Stored Procedure]]></category>
		<category><![CDATA[Table]]></category>
		<category><![CDATA[View]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=155</guid>
		<description><![CDATA[You can use TSQL to Count Number Of Stored Procedures, Views, Tables or Functions in a Database by using the Database INFORMATION_SCHEMA view /* Count Number Of Tables In A Database */ SELECT COUNT&#40;*&#41; AS TABLE_COUNT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' /* Count Number Of Views In A Database */ SELECT COUNT&#40;*&#41; AS VIEW_COUNT FROM [...]]]></description>
			<content:encoded><![CDATA[<p>You can use TSQL to Count Number Of Stored Procedures, Views, Tables or Functions in a Database by using the Database INFORMATION_SCHEMA view</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Count Number Of Tables In A Database */</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> TABLE_COUNT <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLES</span>
    <span style="color: #993333; font-weight: bold;">WHERE</span> TABLE_TYPE<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'BASE TABLE'</span>
    <span style="color: #808080; font-style: italic;">/* Count Number Of Views In A Database */</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> VIEW_COUNT <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>VIEWS
    <span style="color: #808080; font-style: italic;">/* Count Number Of Stored Procedures In A Database */</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> PROCEDURE_COUNT <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>ROUTINES <span style="color: #993333; font-weight: bold;">WHERE</span> ROUTINE_TYPE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'PROCEDURE'</span>
    <span style="color: #808080; font-style: italic;">/* Count Number Of Functions In A Database */</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> FUNCTION_COUNT <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>ROUTINES <span style="color: #993333; font-weight: bold;">WHERE</span> ROUTINE_TYPE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'FUNCTION'</span></pre></div></div>

<p>The same methodology can be used to query for information :</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Select Table Information For A Database */</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLES</span> <span style="color: #993333; font-weight: bold;">WHERE</span> TABLE_TYPE<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'BASE TABLE'</span>
    <span style="color: #808080; font-style: italic;">/* Select View Information For A Database */</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>VIEWS
    <span style="color: #808080; font-style: italic;">/* Select Stored Procedure Information For A Database */</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>ROUTINES <span style="color: #993333; font-weight: bold;">WHERE</span> ROUTINE_TYPE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'PROCEDURE'</span>
    <span style="color: #808080; font-style: italic;">/* Select Function Information For A Database */</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>ROUTINES <span style="color: #993333; font-weight: bold;">WHERE</span> ROUTINE_TYPE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'FUNCTION'</span></pre></div></div>

<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2008%2F09%2F04%2Ftsql-count-number-of-stored-procedures-views-tables-or-functions%2F';
  addthis_title  = 'TSQL+Count+Number+Of+Stored+Procedures%2C+Views%2C+Tables+or+Functions';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2008/09/04/tsql-count-number-of-stored-procedures-views-tables-or-functions/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>SQL Server &#8211; TSQL DISTINCT or GROUP BY?</title>
		<link>http://www.hibbins.com/2008/09/03/sql-server-tsql-distinct-or-group-by/</link>
		<comments>http://www.hibbins.com/2008/09/03/sql-server-tsql-distinct-or-group-by/#comments</comments>
		<pubDate>Wed, 03 Sep 2008 10:47:46 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MSSQL2005]]></category>
		<category><![CDATA[MSSQL2008]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[DISTINCT]]></category>
		<category><![CDATA[GROUP BY]]></category>
		<category><![CDATA[GROUPBY]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=153</guid>
		<description><![CDATA[DISTINCT v GROUP BY A DISTINCT and GROUP BY usually generate the same query plan, so performance should be very simular or the same, if you only need to remove the dulicates then DISTINCT is easier to understand in the query, if you have a more complex query it may be worth running both and [...]]]></description>
			<content:encoded><![CDATA[<p>DISTINCT v GROUP BY</p>
<p>A DISTINCT and GROUP BY usually generate the same query plan, so performance should be very simular or the same, if you only need to remove the dulicates then DISTINCT is easier to understand in the query, if you have a more complex query it may be worth running both and studdying the execution plan to see if your specific example wins</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2008%2F09%2F03%2Fsql-server-tsql-distinct-or-group-by%2F';
  addthis_title  = 'SQL+Server+%26%238211%3B+TSQL+DISTINCT+or+GROUP+BY%3F';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2008/09/03/sql-server-tsql-distinct-or-group-by/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Take a MSSQL database offline and back online with TSQL</title>
		<link>http://www.hibbins.com/2008/08/05/take-a-mssql-database-offline-and-back-online-with-tsql/</link>
		<comments>http://www.hibbins.com/2008/08/05/take-a-mssql-database-offline-and-back-online-with-tsql/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 06:29:29 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MSSQL2005]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[Offline]]></category>
		<category><![CDATA[Online]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=119</guid>
		<description><![CDATA[Take a database offline with TSQL: &#8211; Kill All Other Users Processes And Set The Database To Single User Mode ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE &#8211; Take Offline EXEC sp_dboption &#8216;AdventureWorks&#8217;, &#8216;offline&#8217;, &#8216;TRUE&#8217; And restoring the database back to online with TSQL: &#8211; Allow Users Back In By Setting Multi User Mode [...]]]></description>
			<content:encoded><![CDATA[<p>Take a database offline with TSQL:</p>
<blockquote><p><span style="color: #008000;">&#8211; Kill All Other Users Processes And Set The Database To Single User Mode</span><br />
<span style="color: #0000ff;">ALTER DATABASE </span>AdventureWorks<br />
<span style="color: #0000ff;">SET SINGLE_USER</span><br />
<span style="color: #0000ff;">WITH ROLLBACK IMMEDIATE</span><br />
<span style="color: #008000;">&#8211; Take Offline<br />
</span><span style="color: #0000ff;">EXEC </span><span style="color: #800000;">sp_dboption </span><span style="color: #ff0000;">&#8216;AdventureWorks&#8217;</span>, <span style="color: #ff0000;">&#8216;offline&#8217;</span>, <span style="color: #ff0000;">&#8216;TRUE&#8217; </span></p></blockquote>
<p>And restoring the database back to online with TSQL:</p>
<blockquote><p><span style="color: #008000;">&#8211; Allow Users Back In By Setting Multi User Mode On</span><br />
<span style="color: #0000ff;">ALTER DATABASE</span> AdventureWorks <span style="color: #0000ff;">SET MULTI_USER</span><br />
<span style="color: #008000;">&#8211; Bring Back Online<br />
</span><span style="color: #0000ff;">EXEC </span><span style="color: #800000;">sp_dboption </span><span style="color: #ff0000;">&#8216;AdventureWorks&#8217;</span>, <span style="color: #ff0000;">&#8216;offline&#8217;</span>, <span style="color: #ff0000;">&#8216;FALSE&#8217; </span></p></blockquote>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2008%2F08%2F05%2Ftake-a-mssql-database-offline-and-back-online-with-tsql%2F';
  addthis_title  = 'Take+a+MSSQL+database+offline+and+back+online+with+TSQL';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2008/08/05/take-a-mssql-database-offline-and-back-online-with-tsql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Check if a SQL Server database exists or not (TSQL)</title>
		<link>http://www.hibbins.com/2008/08/01/check-if-a-sql-server-database-exists-or-not-tsql/</link>
		<comments>http://www.hibbins.com/2008/08/01/check-if-a-sql-server-database-exists-or-not-tsql/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 11:29:15 +0000</pubDate>
		<dc:creator>Jon Hibbins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[MSSQL2005]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[Count]]></category>
		<category><![CDATA[Exists]]></category>
		<category><![CDATA[Information]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[Schematam]]></category>
		<category><![CDATA[Usefull]]></category>

		<guid isPermaLink="false">http://www.hibbins.com/?p=114</guid>
		<description><![CDATA[Does an SQL Server database exists?]]></description>
			<content:encoded><![CDATA[<p>Here is a usefull TSQL Script for checking if a database exists :</p>
<blockquote><p><span style="color: #008000;">&#8211; Check if the Database Exists</span><br />
<span style="color: #0000ff;">DECLARE </span>@DatabaseCount <span style="color: #0000ff;">AS int</span><br />
<span style="color: #0000ff;">SELECT </span>@DatabaseCount=<span style="color: #ff00ff;">COUNT</span>(*) <span style="color: #0000ff;">FROM </span><span style="color: #000000;">master.dbo.sysdatabases </span><span style="color: #0000ff;">WHERE</span> <span style="color: #0000ff;">name</span>=<span style="color: #ff0000;">&#8216;master&#8217;</span><br />
<span style="color: #0000ff;">SELECT </span>@DatabaseCount <span style="color: #0000ff;">AS</span> [DatabaseCount]</p></blockquote>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.hibbins.com%2F2008%2F08%2F01%2Fcheck-if-a-sql-server-database-exists-or-not-tsql%2F';
  addthis_title  = 'Check+if+a+SQL+Server+database+exists+or+not+%28TSQL%29';
  addthis_pub    = 'JHibbins';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.hibbins.com/2008/08/01/check-if-a-sql-server-database-exists-or-not-tsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

