2014-05-16 57 views
1

我想在SQL查詢中使用if - else條件。下面我列出了我的查詢。我遇到如下錯誤:使用SQL查詢不執行的If-else條件?

當子查詢不是通過EXISTS引入時,只能在選擇列表中指定一個表達式。

如何執行此查詢?

if (select * from tblOfferDownloads where OfferId ='162')>0 
Begin 
select * from tblOfferDownloads 
End 
else 
Begin 
Select * from tbloffer 
End 
+0

您正在使用哪個數據庫與比較? – Guneli

回答

0

if語句應該是這樣的:

if EXISTS(select NULL from tblOfferDownloads where OfferId ='162') 
BEGIN 
.... 

或者用計數:

if (select COUNT(*) from tblOfferDownloads where OfferId ='162')>0 
BEGIN 
.... 
3

嘗試使用EXISTS或COUNT。你得到錯誤,因爲這個SQL SELECT返回多行,您嘗試行集0

if EXISTS(select COUNT(*) from tblOfferDownloads where OfferId ='162') 

if (select COUNT(*) from tblOfferDownloads where OfferId ='162')>0