1

我想注入一个条件值。

Select 'Select a Value' as CarID, 
       'Select a Value' as CarName
UNION
Select Distinct CarID, 
       CarName 
from Cars c
where ..some condition...

所以在这里我只想在 select 语句返回的计数 > 1 时注入“选择一个值”,因为这将是下拉列表的数据集。

所以换句话说,如果该选择返回 0 或 1 结果,我不想包含这个联合。

有没有办法倒退或检查在返回列表的开头注入或不注入的计数?

4

2 回答 2

3
with C as 
(
  select CarID, CarName
  from Cars
  --where ..some condition...
)
select CarID, CarName
from
  (
    select CarID, CarName, 1 as Sort
    from C
    union all
    select 'Select a Value', 'Select a Value',  0
    where exists(select * from C)
  ) T
order by Sort

SQL小提琴

于 2012-05-03T05:35:27.180 回答
0

在 SQL Server 中,

SELECT * FROM (
    Select 'Select a Value' as CarID, 
           'Select a Value' as CarName
    UNION
    Select CarID, CarName 
    from Cars c
    -- WHERE...
) SUB
WHERE @@ROWCOUNT>1
于 2012-05-03T04:30:37.963 回答