Monday, February 25, 2008
Saturday, January 26, 2008
Bulk Updates in T-SQL without Cursors
Sometimes you may want to perform a mass update on a table based on the contents of another table. The first thing you may think of is using a cursor to scroll through Table A and update Table B line by line. This is extremely slow. However, there is another way.
The way to do it is to join the two tables together then perform a self-update.
The way to do it is to join the two tables together then perform a self-update.
UPDATE ProductSo what have I done there? Well the first two lines instruct the dbms to update the Product table, altering the price. The second two lines provide a join of the 2 tables we are using and an interim relation that the Update can use as it's source. At table of 250,000 records took 3 seconds to update with this method but over 30 minutes using a cursor so the benefits are huge.
SET Price = NewPrice
FROM Product
INNER JOIN UpdatedProduct ON UpdatedProduct .ProductId = Product.ProductId
Tuesday, January 8, 2008
Lesser known elements of C#: Coalescing
Unless you read through all 800 or so pages of the C# specification, and honestly I've had more fun bashing my head against a bowl of weetabix then you could be forgiven for missing out of some of the lesser known elements of the language. One of which is the coalescing operator. You may have come across the term "Coalesce" in SQL and the meaning is almost identical in C#. Let's look at an example:
So what does this means? Well if the string "SomeString" is null then the string "YourName" will be set to "Joe Public". If it's not null then the string "YourName" will be set to the value of "SomeString". So the element before the ?? operator will be used unless it's null, in which case the element after the ?? operator is used. In essence this is similar to the ISNULL feature of T-SQL or the COALESCE feature of SQL. It's not something that'll change your life for the better but it's a great party trick because, in my own experience, a lot of c# programmers haven't come across it. So go nuts, impress your friends :)
string YourName = SomeString ?? "Joe Public";
So what does this means? Well if the string "SomeString" is null then the string "YourName" will be set to "Joe Public". If it's not null then the string "YourName" will be set to the value of "SomeString". So the element before the ?? operator will be used unless it's null, in which case the element after the ?? operator is used. In essence this is similar to the ISNULL feature of T-SQL or the COALESCE feature of SQL. It's not something that'll change your life for the better but it's a great party trick because, in my own experience, a lot of c# programmers haven't come across it. So go nuts, impress your friends :)
Sunday, January 6, 2008
So it begins..
That's the title of the first level of "Cannon Fodder", a rather splendid game from the 1990's. So I'd guess I'd best introduce myself since no-one else will (and if they do then you may be hearing voices - go get that checked :) ). I'm a 32-year old developer from the British Isles specialising in .NET, C#, Java, SQL and insanely obscure TV knowledge. I say "British Isles" because my developing skills (or lack of :)) has taken me to all over the place including Jersey, Exeter, London, Birmingham and even the Isle of Man.
Why "The Accidental Coder"? Well, way back when, it was always planned that I was going to be a lawyer. I studied both Law and Politics at A-Level but a "B" grade in Physics put pay to my entry into Law at university. I decided to take Computer Science instead and the rest is history. This blog, possibly the finest blog on this page, will probably be mostly developer related but I tend to ramble so expect a wide variety of topics.
Why "The Accidental Coder"? Well, way back when, it was always planned that I was going to be a lawyer. I studied both Law and Politics at A-Level but a "B" grade in Physics put pay to my entry into Law at university. I decided to take Computer Science instead and the rest is history. This blog, possibly the finest blog on this page, will probably be mostly developer related but I tend to ramble so expect a wide variety of topics.
Subscribe to:
Posts (Atom)
