使用tsql将第一行下拉列表清空

时间:2014-07-28 14:29:38

标签: tsql sql-server-2012 ssms

我有一个使用表驱动设计的遗留应用程序。我已经建立了一个自定义下拉列表,其中包含1950年到当年+ 1的年份。

我的问题是,当我完成它时,我得到了我的空标签'',然后是从1950年开始的年份,我需要从空标签''开始,然后是当前年份加上一个和desc。

以下显示了我的位置。我添加了Order by,效果很好,但现在我的空标签''是列表中的最后一项。我仍然希望它成为第一。

DECLARE @begin int = 1950
DECLARE @end int = YEAR(GETDATE()) + 1;

with foo (Year) as
(
select @begin as num
UNION ALL 
SELECT Year+1 from foo
where Year <@end


)
SELECT '' AS Value, '' AS Label UNION
select  CAST(Year AS VARCHAR(4)) AS Value, CAST(Year AS VARCHAR(4)) Label from foo 
where Year between @begin and @end

Order by Value desc

1 个答案:

答案 0 :(得分:1)

简单地再添加一列进行排序,如下所示:

DECLARE @begin int = 1950
DECLARE @end int = YEAR(GETDATE()) + 1;

with foo (Year) as
(
    select @begin as num
    UNION ALL 
    SELECT Year+1 from foo
    where Year <@end
), 
DataSource AS
(
SELECT 0 AS [RowID]
      ,'' AS Value
      ,'' AS Label 
UNION
SELECT 1 AS [RowID]
      ,CAST(YEAR AS VARCHAR(4)) AS Value
      ,CAST(YEAR AS VARCHAR(4)) AS Label 
FROM foo 
WHERE YEAR BETWEEN @begin AND @end
)
SELECT Value
      ,Label
FROM DataSource
ORDER BY RowID ASC
        ,Value DESC