<?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>Codelab Blog &#187; T-SQL</title>
	<atom:link href="http://blog.codelab.co.nz/tag/t-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codelab.co.nz</link>
	<description>Technical Articles and News from Codelab Ltd</description>
	<lastBuildDate>Tue, 17 Jan 2012 13:10:43 +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>Clean your SQL Server database!</title>
		<link>http://blog.codelab.co.nz/2009/12/23/clean-your-sql-server-database/</link>
		<comments>http://blog.codelab.co.nz/2009/12/23/clean-your-sql-server-database/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 20:05:29 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=164</guid>
		<description><![CDATA[Here are some lines of code that can help clean your database from test data so you can start over with a fresh canvas. /* Disable constraints and triggers (if any) */ exec sp_MSforeachtable &#8216;ALTER TABLE ? NOCHECK CONSTRAINT ALL&#8217; exec sp_MSforeachtable &#8216;ALTER TABLE ? DISABLE TRIGGER ALL&#8217; /*Perform delete operation on all table for [...]]]></description>
			<content:encoded><![CDATA[<p>Here are some lines of code that can help clean your database from test data so you can start over with a fresh canvas.</p>
<p>/* Disable constraints and triggers (if any) */</p>
<p>exec sp_MSforeachtable &#8216;ALTER TABLE ? NOCHECK CONSTRAINT ALL&#8217;<br />
exec sp_MSforeachtable &#8216;ALTER TABLE ? DISABLE TRIGGER ALL&#8217;</p>
<p>/*Perform delete operation on all table for cleanup, or you can put specific delete &lt;tablename&gt; commands if you want to only do a subset of deletions</p>
<p>*/<br />
exec sp_MSforeachtable &#8216;DELETE ?&#8217;</p>
<p>/*Enable Constraints &amp; Triggers again*/<br />
exec sp_MSforeachtable &#8216;ALTER TABLE ? CHECK CONSTRAINT ALL&#8217;<br />
exec sp_MSforeachtable &#8216;ALTER TABLE ? ENABLE TRIGGER ALL&#8217;</p>
<p>/*Reset Identity on tables with identity column*/<br />
exec sp_MSforeachtable &#8216;IF OBJECTPROPERTY(OBJECT_ID(&#8221;?&#8221;), &#8221;TableHasIdentity&#8221;) = 1 BEGIN DBCC CHECKIDENT (&#8221;?&#8221;,RESEED,0) END&#8217;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2009/12/23/clean-your-sql-server-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Selecting a row position within a SELECT query</title>
		<link>http://blog.codelab.co.nz/2009/03/11/selecting-a-row-position-within-a-select-query/</link>
		<comments>http://blog.codelab.co.nz/2009/03/11/selecting-a-row-position-within-a-select-query/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 09:16:11 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=91</guid>
		<description><![CDATA[I want to get the third row in a result set from the following: ID   Name 1   A 2  B 3  C 4 D You can acheive this by using the ROW_NUMBER() rank function using SQL Server 2005/2008.   The following code will return the 3rd record. create table #temp ( ID int, [Name] nvarchar(50) ) [...]]]></description>
			<content:encoded><![CDATA[<p>I want to get the third row in a result set from the following:</p>
<p>ID   Name</p>
<p>1   A</p>
<p>2  B</p>
<p>3  C</p>
<p>4 D</p>
<p>You can acheive this by using the ROW_NUMBER() rank function using SQL Server 2005/2008.   The following code will return the 3rd record.</p>
<p>create table #temp<br />
(<br />
ID int,<br />
[Name] nvarchar(50)<br />
)</p>
<p>insert into #temp values (1,&#8217;A')<br />
insert into #temp values (2,&#8217;B')<br />
insert into #temp values (3,&#8217;C')<br />
insert into #temp values (4,&#8217;D')<br />
insert into #temp values (5,&#8217;E')</p>
<p>select [Name] from #temp where ID in (select ID from<br />
(select ID, ROW_NUMBER() OVER (order by ID asc) as [Row] from #temp) as subquery<br />
where [Row] = 3)</p>
<p>drop table #temp</p>
<p>Result:</p>
<p>Name = C</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2009/03/11/selecting-a-row-position-within-a-select-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server Express 2008 Maintenance Script</title>
		<link>http://blog.codelab.co.nz/2008/11/14/sql-server-express-2008-maintenance-script/</link>
		<comments>http://blog.codelab.co.nz/2008/11/14/sql-server-express-2008-maintenance-script/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 02:01:05 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://blog.codelab.co.nz/?p=45</guid>
		<description><![CDATA[The issue I have with SQL Server Express Edition 2008 is that the SQL Agent is disabled and according to Microsoft, this has been disabled on purpose.   So, how do we schedule jobs to do such tasks as Rebuilding Indexes, Full-text Catalogs etc?   I use the Task Scheduler on Windows 2008 Server that executes a [...]]]></description>
			<content:encoded><![CDATA[<p>The issue I have with SQL Server Express Edition 2008 is that the SQL Agent is disabled and according to Microsoft, this has been disabled on purpose.   So, how do we schedule jobs to do such tasks as Rebuilding Indexes, Full-text Catalogs etc?   I use the Task Scheduler on Windows 2008 Server that executes a SQL script using sqlcmd -i &lt;database script&gt;</p>
<p>Here is the sample code that I use for Rebuilding indexes and catalogs:</p>
<blockquote><p>declare @databasename varchar(max)<br />
declare @cat varchar(max)</p>
<p>DECLARE database_cursor CURSOR FOR<br />
select name from master.sys.databases<br />
where database_id &gt; 4 &#8211;exclude system databases</p>
<p>OPEN database_cursor</p>
<p>FETCH NEXT FROM database_cursor<br />
INTO @databasename</p>
<p>WHILE @@FETCH_STATUS = 0<br />
BEGIN</p>
<p>BEGIN TRY<br />
&#8211;Below line rebuilds indexes<br />
exec (&#8216;USE &#8216; + @databasename + &#8216; EXEC sp_MSforeachtable @command1 = &#8220;ALTER INDEX ALL ON ? REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON,<br />
STATISTICS_NORECOMPUTE = ON);&#8221;&#8216;)</p>
<p>exec (&#8216;USE &#8216; + @databasename + &#8216; declare @cat varchar(max) select @cat = (select top 1 name from &#8216; + @databasename + &#8216;.sys.fulltext_catalogs) if @cat is not null begin exec (&#8221;ALTER FULLTEXT CATALOG &#8221; + @cat + &#8221; REBUILD WITH    ACCENT_SENSITIVITY=OFF;&#8221;) end&#8217;)</p>
<p>END TRY<br />
BEGIN CATCH<br />
DECLARE @ErrorMessage NVARCHAR(4000),<br />
@ErrorSeverity INT,<br />
@ErrorState INT</p>
<p>SET @ErrorMessage = &#8216; databasename: &#8216; + @databasename + &#8216; &#8216; + ERROR_MESSAGE();<br />
SET @ErrorSeverity = ERROR_SEVERITY();<br />
SET @ErrorState = ERROR_STATE();</p>
<p>&#8211;print @ErrorMessage<br />
RAISERROR(@ErrorMessage,@ErrorSeverity,@ErrorState);</p>
<p>END CATCH</p>
<p>FETCH NEXT FROM database_cursor<br />
INTO @databasename<br />
END</p>
<p>CLOSE database_cursor<br />
DEALLOCATE database_cursor<span id="more-45"></span></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.codelab.co.nz/2008/11/14/sql-server-express-2008-maintenance-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

