I've written a lot of SQL. Mostly Microsoft's T-SQL variant. I was a DBA in a high pressure environment looking after over 80 business critical server instances.

It's a language that I'm very confident in, and one that I think is very well suited to the job. However there's one major flaw with how it works that annoys me every time I use it:

SQL has two methods for getting data into a table, depending on whether you're adding or changing data.

--Add
    insert into tablename ( idfield, field1, field2, ... )
    values ( 7, 'value one', 'another value', ... )

--Change
    update tablename 
    set field1 = 'new value',
        field2 = 'different value',
        ...
    where idfield = 7

There are two problems here, the first is that you need to know whether you're adding or changing before you write the SQL. The problem is that you often don't. You can get around this with an additional bit of logic:

--check whether to insert or update
if not exists(
    select * from tablename where idfield = 7 ) 
    insert into tablename ( idfield, field1, field2, ... )
    values ( 7, 'value one', 'another value', ... )
else
    update tablename 
    set field1 = 'new value',
        field2 = 'different value',
        ...
    where idfield = 7

Hardly ideal, it does two I/O operations where only one might be required for starters, but that isn't my issue with it. The real problem is the insert statement - it depends on the order of fields. For two or three fields this isn't a problem, but for more than that it quickly gets to be a royal pain.

Oracle has had, for a while now, a solution for the first problem that nicely fixes the second:

    upsert tablename 
    set field1 = 'new value',
        field2 = 'different value',
        ...
    where idfield = 7

This upsert statement does both update and insert in Oracle, but it also has the huge advantage that you can have one statement that lists fields next to the values you're setting for them. Order doesn't matter.

Now in SQL 2008 Microsoft have 'fixed' this (only they haven't) with the merge statement:

merge into tablename 
where  idfield = 7
when matched then
    update
    set field1 = 'new value',
        field2 = 'different value',
        ...
when not matched then
    insert ( idfield, field1, field2, ... )
    values ( 7, 'value one', 'another value', ... )

The two I/O operations where one was needed problem has been fixed, but the language issue of lots of inserted fields is still there.

Upsert isn't in the SQL:92 standard, and merge is new to the SQL:2003 standard. It's great that Microsoft have caught up with a five year old standard, but I still prefer the Oracle way of doing it.

It's just about the only thing Oracle does better than Microsoft.

0

Add a comment

And I'll tell you why...

The allegory constantly used by governments and the media is on of a household, and the recession is like the breadwinners losing their jobs. Obviously anyone sensible would cut back, yeah?

But, countries are nothing like households, and that allegory just doesn't work.

I'm not arguing that they shouldn't cut back at all, but I think a healthy government should be running some degree of deficit during a recession.

Here are my assumptions:

1. There will ALWAYS be boom and bust

We can't stop the cycle of boom and bust, it's been going for hundreds of years and will continue for hundreds more. Maybe one day, but no financial theory to date has come close to a way around them.

With that assumption we should plan for them: busts will happen.

The UK will vote on May 5th on whether to switch to the Alternative Vote system (AV) rather than the current First Past The Post (FPTP). I'm a supporter of AV, but FPTP has huge support from both the major parties and from Rupert Murdoch, which means that it both doesn't have a chance and must be the right way on principal.

Fact is anything legal that Murdoch's against I'm for on principal.

There is a problem in UK banking that's stalling our recovery and it goes something like this:

Banks have two main components: high street lending & saving, and investment banking. The investment banking side took far too many risks in dodgy sub prime investments and lost all the money. However if the high street lending & saving side of a big bank fails it does huge damage to the economy, so the government steps in to fill the gap.

British Airways have the best paid flight staff in the world. Their pay is sky high (sorry) – cabin crew can earn up to £56k a year! To put it in context that's more than junior and mid-level doctors – I really don’t see how any cabin crew job can be worth more than even the most junior doctor's role.

BA also run one of the biggest teams per flight – they have 14 on each long-haul plane.

The problem is that flights on BA are expensive.

Recently at TechEd Berlin I attended an optimising Javascript presentation that I described as having "lost focus".

I think this one (from Google) is far more the sort of level and detail that I expected:

This presentation is excellent - in fact I think every developer who ever does any Javascript should view it.

I think the current situation with bankers' bonuses shows a complete failure to understand risk and how markets actually work.

There's a TV show in the UK called Property Ladder. Every week through the property boom it followed someone buying and renovating a house with a view to selling it for profit. They found some proper idiots on that show.

There's a great deal of fuss at the moment about RBS's investment division's planned bonuses. They've made some money (great!) but want to pay over a billion in bonuses, which is over a quarter of the profit.

When .Net originally launched it came with first rate support for Simple Object Access Protocol (SOAP) and at the time I was seriously impressed. Creating a SOAP client-server connection was amazingly easy - little more than adding a .asmx file and decorating your methods with the [WebMethod] attribute, and then point your client project at it and Visual Studio does the rest.

What Visual Studio actually does in this case is create a large auto-generated code file from the WSDL.

Sightseeing

By this point we were starting to feel the tech-fatigue. We took the opportunity to see a little of Berlin, before heading back to hit the labs and the convention stalls

DEV301r Microsoft Visual Studio Tips and Tricks

Scott Cate Nice session to finish on - most of these I knew already but a couple were new. They're all on Scott's blog.

Finally all the stands that had prize draws held their raffles.
Label Cloud
Blog Archive
About Me
About Me
Blogroll
Blogroll
Loading