2013-08-21 57 views
0

我需要在內部編寫case語句,條件是類似的。如何在SQL服務器中的where語句中編寫大小寫

"If color equel to 'RED' then return all the flowers including red" 
else "return all flowers which are not RED" 

我需要包括where子句在此聲明,我在那裏聲明看起來是這樣的

Select Plant, leafs, places 
from dbo.tblplant, dbo.tblflower,dbo.tblplaces 
where dbo.tblplant = ID and 
dbo.tblplaces = PLACE and 
dbo.tblflower = REDCOLOR. 

相反redcolor我需要修改以這樣一種方式,如果它的紅色是查詢應該返回所有的花否則它應該排除紅色並返回休息。

+0

'WHERE(@Color ='Red')OR(@Color <>'Red'AND Flower.Color <>'Red')'? –

+0

「顏色」/「它」來自哪裏?它是傳遞給此查詢的變量嗎? –

+0

是的,它作爲參數傳遞給SP – Naruto

回答

4
where @colorParameter = 'RED' 
     or (@colorParameter <> 'RED' and tblplant.color <> 'RED') 
+0

不需要使用case語句嗎? – Naruto

+0

@LLL - 這是一個令人驚訝的常見錯誤,那些對SQL很陌生的人認爲他們需要'CASE',事實上他們只需要應用簡單的布爾邏輯。 –

+0

是的你是對的,謝謝你的啓發 – Naruto