Thursday, 28 August 2008

SQL Server Management Studio Madness - why do I have to have scripts as strings?

What do you think the following option in the SQL Server Management Studio does?

SSMS Option

Now one side of my brain says that I'll get a some preamble on my script looking a little bit like

IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[SomeTable]'))

Well you'd be right! Well in the main.

Without the option:

/****** Object:  Table [dbo].[SomeTable]    Script Date: 08/28/2008 09:03:29 ******/
CREATE TABLE [dbo].[SomeTable](
[Column1] [nchar](10) NULL,
[Column2] [nchar](10) NULL

And with the option:

/****** Object:  Table [dbo].[SomeTable]    Script Date: 08/28/2008 09:03:49 ******/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SomeTable]') AND type in (N'U'))
CREATE TABLE [dbo].[SomeTable](
[Column1] [nchar](10) NULL,
[Column2] [nchar](10) NULL

But for stored procedures?

Without the option:

/****** Object:  StoredProcedure [dbo].[DoIt]    Script Date: 08/28/2008 09:10:53 ******/
create procedure [dbo].[DoIt]
select *
from SomeTable
where Column1 = 'blah'

With the option:

/****** Object:  StoredProcedure [dbo].[DoIt]    Script Date: 08/28/2008 09:10:35 ******/
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DoIt]') AND type in (N'P', N'PC'))
EXEC dbo.sp_executesql @statement = N'create procedure [dbo].[DoIt]
select *
from SomeTable
where Column1 = '


What I want to know is why does the 'IF NOT EXISITS' check require me to generate a script as a string invocation of sp_executesql?

Tuesday, 26 August 2008

RDP over two monitors

I'd not even thought about doing this until today but at work we have dual monitors and we can work via remote desktop but this usually doesn't expand correctly over my home laptop and spare lcd setup. Today I did some hunting around and came up with the /span option on MSTSC (the rdp client).  Cannot do full screen but it expand just about perfectly!


TFS Command line (un)locking a file

I locked a file to prevent other users being able to checkin as I was doing radical surgery on it. This is something I hardly ever have to do with TFS but I was justified in this case. Since I use the command line I found that the easiest to issue a

tf lock /lock:checkin somefile.xml

However when unlocking it I discovered no unlock command. Rather you have to

tf lock /lock:none somefile.xml

This is (IMO) a real programmer choosing an API that is sensible for them (lock a file with no lock) but opaque to the new user. As I write lots of little tools/scripts etc, it's made me think about API discoverablility/design for the future.

Thursday, 21 August 2008

Playing with Extension Methods

I've had a issue with option Xml such as in our configuration stores. Basically I wanted to grab data from an XAttribute (see System.Xml.Linq) but wanted to avoid having to constantly check for null nodes. To ease the pain I wrote the following:


public static class Extensions {
/// <summary>
/// The extension method allows you to grab data from an XAttribute of if its null, supply a default
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="node">The node.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns></returns>
public static T ValueOrDefault<T>(this XAttribute node, T defaultValue) where T : IConvertible {
if (node == null) {
return defaultValue;
return (T)Convert.ChangeType(node.Value, typeof(T));

now I can write

var results = from element in doc.Descendants("SomeTag").Where(e => e != null)
select new {
Prop1 = element.Attribute("attribute1").ValueOrDefault(String.Empty),
Prop2 = element.Attribute("attribute1").ValueOrDefault(String.Empty),

instead of

var results = from element in doc.Descendants("SomeTag").Where(e => e != null)
select new {
Prop1 = element.Attribute("attribute1") != null ? element.Attribute("attribute1").Value : String.Empty,
Prop2 = element.Attribute("attribute2") != null ? element.Attribute("attribute2").Value : String.Empty,

Or some multi line version of it. Its was just as easy to create one for XElement.

Wednesday, 13 August 2008

How Did I Get Started In Software Development?

I just been tagged by my friend Mike Hadlow to join with this meme. The question is a very popular topic with us nerds, and for me especially considering my passion for technology.

How old were you when you first started  in programming?

This is a favourite story of mine. I was 11 years old (1978) and in Mr Beardsley's maths class. At the back of the room was a partition, behind which only the 6th formers (16 year and older) kids were allowed. In the fateful particular lesson a series of beeps and other noises were heard. Asking Mr Beardsley about them was the probably the most life changing thing (a part from having kids) ever to happen to me - "That's the 6th formers playing with the computers. Would you like to find out more about that?" Well we started youngsters computer club that week and the rest is history.

What was your first programming language

Sorry I'd like to say machine code but no it was BASIC (PET ISTR then TRS80 then Acorn).

What was the first real program that you wrote

The first program I entered into a computer was from my (then) best friend Mandeep:

10 print "********************* Nuclear Alert ******************************"
20 goto 10

However the first program I actually wrote myself was called fatty, and it was a game to teach my younger brother maths. It had a some simple maths questions like 'what is 15 + 4'. If you selected the right answer a fat man would move along the screen all way across, but if you got it wrong then the man would randomly fall down the screen on his journey across the screen :-) It was called fatty because the man sort of looked, well, fat!

What languages have you used since you started programming

Ok, apart from BASIC and some simple machine code on my home computer I've used Pascal, Modula2, C, C++, Assembler (many varieties), PL/1, PLS, SQL, and of course C# and VB, not to mention PERL, and various shell languages and now iron python. But without a doubt the most interesting language I've ever written in was micro code at university. Here we had to do such things like loading buses with addresses, issuing memory refresh and read instructions, unloading buses etc. Together these  instruction primitives would end up forming a single machine code instruction like load register with contents of memory address.

What was your first professional programming gig

I was hired by IBM for a year during my degree to to testing of network apps, but after graduation I started work at NatWest bank in the UK as a systems analyst but left that after 9 months to return to programming at small company in Surrey, UK. Here I learned C then C++ and became firmly entrenched in the MS world with the first visual studio. I have to say working here was was another life changing experience - I learned the difference between hacking code and engineering software systems (which I'll expand in another post).

What's the most fun you've every had programming

For me its all about the paradigms and abstractions,  and how they affect cognition. It sounds lofty but really all I mean is that I think civilisation is really just the application of one technology after another that leads us from one way of thinking to another. Software is a great medium as we can create new models, use and break them, learn from them and then start over much more easily than say working with, say, hardware.  I have fun with programming nearly every day. But I get the most fun from getting into a tough debug session where you dig deep, learn some, try again , dig deeper and so on, every time building new models and breaking through abstractions to try and understand what was intended and what was achieved.

Who am I calling out?

Peter Goodman and Simon Shearston....