2012-05-03 15 views
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結果,我不想包含此聯合。

有沒有辦法有點倒退或檢查計數注入或不注入返回列表的開始?

回答

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 
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 Fiddle