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 を超えるカウントを返す場合にのみ、「Select a value」を挿入します。これは、ドロップダウン リストのデータ セットになるためです。

つまり、select が 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 サーバーでは、

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 に答える