Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Create a view to populate a combo

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-08-22 : 07:00:13
Stephanie writes "I need to create a view that will be used to populate a HTML select control that will act as a filter on a HTML table. There is a field in the database where I need to find all of the distinct entries. These will be used as HTML options in the HTML select. I need to have the distinct entries be sorted ASC. In addition to this I need to have an option to let the user select "(None)" of the distinct entries. This option should be at the top of the list. The database has entries that are NULL, 'NULL'.

The result should be a view that contains (for example):

(None)
4000
5000
8000
9000
.
.
.

The following will almost get what I need, but I realize that ORDER BY cannot be used in a view or subquery. Plus, this code results in an extra '(None)'.

SELECT
CASE ISNULL(NULLIF(T.CODE,''),'NULL')
WHEN 'NULL' THEN '(None)'
ELSE T.CODE
END
FROM TABLE T
GROUP BY T.CODE
ORDER BY T.CODE


Results:

(None)
(None)
4000
5000
8000
9000
.
.
."

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-08-22 : 18:23:19
Because you are populating an HTML SELECT control, I would suggest that you use a Stored Procedure rather than issuing a direct SELECT against a View. Your Stored Procedure could then include the ORDER BY for you. It could also do a DISTINCT on the results of the above SELECT statement to get rid of the duplicate (None) entries. In fact, a DISTINCT is probably a better route than your GROUP BY.

Or, you might consider making the addition of a None entry as part of the web code and not the SQL code, and in your SQL inlude a WHERE T.CODE IS NOT NULL clause.

---------------------------
EmeraldCityDomains.com
Go to Top of Page
   

- Advertisement -