<?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>Docile Tree &#187; database</title>
	<atom:link href="http://www.dociletree.co.za/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dociletree.co.za</link>
	<description>Aubrey Kilian explains, rants and raves</description>
	<lastBuildDate>Thu, 08 Jul 2010 08:50:40 +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>MSSQL and ON UPDATE</title>
		<link>http://www.dociletree.co.za/mssql-and-on-update/</link>
		<comments>http://www.dociletree.co.za/mssql-and-on-update/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 09:17:15 +0000</pubDate>
		<dc:creator>aubrey</dc:creator>
				<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.dociletree.co.za/?p=170</guid>
		<description><![CDATA[I had to add a column to a database table on an MSSQL server today. It had to have the most recent update/insert time that the row was inserted/updated. MySQL has a very easy way of doing this with a TIMESTAMP column type, with a DEFAULT value of CURRENT_TIMESTAMP, and then also an additional ON &#8230; </p><p><a class="more-link block-button" href="http://www.dociletree.co.za/mssql-and-on-update/">Continue reading &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>I had to add a column to a database table on an MSSQL server today.  It had to have the most recent update/insert time that the row was inserted/updated.  MySQL has a very easy way of doing this with a TIMESTAMP column type, with a DEFAULT value of CURRENT_TIMESTAMP, and then also an additional ON UPDATE CURRENT_TIMESTAMP clause in the column definition to auto-update the value when the row is updated.<br />
So I thought that if MySQL could do it, so should MSSQL be able too, right?  Wrong.  I&#8217;m not expert in MSSQL, I know MySQL fairly well, so my first surprise was to find out that the <a href="http://msdn.microsoft.com/en-us/library/aa260631(SQL.80).aspx">TIMESTAMP column type in MSSQL</a> is *so* not the same as in MySQL, and in fact is rather completely useless in this example.</p>
<p>The solution turned out to be external to the table definition, by using a trigger.<br />
So here&#8217;s how you update a column in your table with the current time whenever that row is update.  I&#8217;m assuming you know your way around MSSQL and it&#8217;s multitudes of tools available, so I won&#8217;t be giving you screenshots.</p>
<p>First, add a column to your table as either a DATETIME or SMALLDATETIME, make the default value be: <code>(GETDATE())</code><br />
The default value takes care of setting the value of the column to the current date/time when a new record is inserted.</p>
<p>Then, add a trigger onto your table, that looks similar to this:</p>
<blockquote><p>CREATE TRIGGER trg_updateLastUpdated ON [dbo].[your_table_name]<br />
FOR UPDATE<br />
AS<br />
if not update(last_updated)<br />
UPDATE your_table_name<br />
SET last_updated = GETDATE()<br />
FROM your_table_name<br />
INNER JOIN Inserted<br />
ON your_table_name.unique_id = Inserted.unique_id</p></blockquote>
<p>And that&#8217;s that.  The trigger makes sure to check that it&#8217;s not the last_updated column&#8217;s UPDATE that triggers the trigger, as without it, it will cause some nasty recursion to happen.<br />
Your INNER JOIN can be on multiple columns too, if you have more than one column that is required to uniquely identify the single row that you&#8217;re updating.</p>
<p>Yes, you could probably update the last_updated column&#8217;s value from within whatever code you&#8217;ve got, that is an alternative, depending on exactly what you use that column for.<br />
Anyway, this solution worked for me.  Did it work for you?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dociletree.co.za/mssql-and-on-update/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

