Yesterday I was writing a SQL Query that would return data to be displayed in a drop-down list. Rather than add a row first that contained your typical “Select One” and then bind the data, I wanted the data returned from my SQL Query to contain that row first.
I’ve done this in the past and I can’t remember how I did it. This time I decided to use a UNION operator.
For example, let’s say I have a SQL database with a table “Table1” and in Table1 there are two columns, “A” and “B”. “A” holds some id number that starts at zero, and “B” has some label that I want to display in the drop-down list. Normally my SQL query would be just:
SELECT A, B FROM Table1
However, because I wanted to inject a row at the top I did this instead:
SELECT -1 AS A, 'Select One' AS B UNION SELECT A, B FROM Table1
I’m sure there are other ways of doing this. Please comment and let me know what they are. Maybe one of them is the way I did it a few years ago.