Reputation: 385
I want to use an if
-else
condition in an SQL query. Below I have listed my query. I am getting an error like:
Only one expression can be specified in the select list when the sub query is not introduced with EXISTS.
How can I execute this query?
if (select * from tblOfferDownloads where OfferId ='162')>0
Begin
select * from tblOfferDownloads
End
else
Begin
Select * from tbloffer
End
Upvotes: 1
Views: 314
Reputation: 24134
Try to use EXISTS or COUNT. You get error because this SQL SELECT returns more than one row and you try to compare the rowset with 0.
if EXISTS(select COUNT(*) from tblOfferDownloads where OfferId ='162')
or
if (select COUNT(*) from tblOfferDownloads where OfferId ='162')>0
Upvotes: 3
Reputation: 31239
The if statement should be like this:
if EXISTS(select NULL from tblOfferDownloads where OfferId ='162')
BEGIN
....
Or with a count:
if (select COUNT(*) from tblOfferDownloads where OfferId ='162')>0
BEGIN
....
Upvotes: 0