Archive for February, 2009
MSSQL and ON UPDATE
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.
So I thought that if MySQL could do it, so should MSSQL be able too, right? Wrong. I’m not expert in MSSQL, I know MySQL fairly well, so my first surprise was to find out that the TIMESTAMP column type in MSSQL is *so* not the same as in MySQL, and in fact is rather completely useless in this example.
The solution turned out to be external to the table definition, by using a trigger.
So here’s how you update a column in your table with the current time whenever that row is update. I’m assuming you know your way around MSSQL and it’s multitudes of tools available, so I won’t be giving you screenshots.
First, add a column to your table as either a DATETIME or SMALLDATETIME, make the default value be: (GETDATE())
The default value takes care of setting the value of the column to the current date/time when a new record is inserted.
Then, add a trigger onto your table, that looks similar to this:
CREATE TRIGGER trg_updateLastUpdated ON [dbo].[your_table_name]
FOR UPDATE
AS
if not update(last_updated)
UPDATE your_table_name
SET last_updated = GETDATE()
FROM your_table_name
INNER JOIN Inserted
ON your_table_name.unique_id = Inserted.unique_id
And that’s that. The trigger makes sure to check that it’s not the last_updated column’s UPDATE that triggers the trigger, as without it, it will cause some nasty recursion to happen.
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’re updating.
Yes, you could probably update the last_updated column’s value from within whatever code you’ve got, that is an alternative, depending on exactly what you use that column for.
Anyway, this solution worked for me. Did it work for you?
Nerdmag.co.za launches
Sheena (aka SheBee of www.shebee.co.za), decided to start an online magazine, and via Twitter, and practically every other medium she had at her disposal, called for contributors. And so launched NerdMag. 9am this morning the site was unveiled, and if you look carefully, you might see a familiar name.
Yes, mine. I opted to reply to the call for contributors only ‘cos I find that I like writing, almost as much as I like gaming, and I’d like to write about games more than just reviews, you know? So SheBee gave the go ahead, gave me some ideas, and I started. My first article became so big that I had to abandon it, verbal diarrhoea is not the best way to start for a first article on a new online mag. So, off you go, go enjoy the new kid on the block that is NerdMag, and don’t forget to read, and comment on all the cool content there, especially my article.
And to Sheena, good luck with the venture, you’re doing something lots of people dream about but never actually execute, so that makes you one step ahead of a lot of people. All the best. And thanks for the opportunity to smear my content on your project.
It seems like for a limited period only, the fantastic 
