tags:

views:

53

answers:

3

Is there a way to include a blank row at the top of a sql query, eg if it is meant for a dropdown list? (MS Sql Server 2005 or 2008)

Select * 
  FROM datStatus 
ORDER BY statusName

Where I want something like

  -1  (please choose one)
  1   Beginning
  2   Middle
  3   Ending
  4   Canceled

From a table that is ordinarily just the above, but without the top row?

+4  A: 

Hi,

I have found that it is better to do this in the presentation layer of your application, as you might have different requirements based on the context. In general I try to keep my data service layer free of these sorts of implementation specific rules. So in your case I would usually just add a new item by index in the first position of the list, after i had loaded it with data from my service layer.

Enjoy!

Doug
+1, even though this isn't what I needed. In this case I am using an asp.net sqldatasource in the asp presentation layer, and I need something that already has the correct values since I'm binding the data source to a dropdownlist inside a radgrid.
Kimball Robinson
+2  A: 

How about unioning the first row together with the rest of the query?

Select -1,'(please choose one)'
union all
select * FROM datStatus ORDER BY statusName
jskaggz
Although, I generally agree with Doug that it should be handled in the ui layer.
jskaggz
+3  A: 

I feel it's nicer to do it outside SQL, but if you insist...

SELECT -1, '(please choose one)'
UNION
SELECT * FROM datStatus
ORDER BY statusName
Amadan
I was having trouble figuring out how to do it properly in the UI layer, but finally figured it out. Thanks! I'll accept this since it recommends not doing it in SQL but still gives the answer.
Kimball Robinson
If you said what framework you are using for your dropdown list (HTML? .Net?) it would be easier to give you alternative... :)
Amadan
.NET dropdownlists, but I already found a solution in another answer on stack overflow. Thanks!
Kimball Robinson