user3643560
user3643560

Reputation: 385

If-else condition using SQL query not executing?

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

Answers (2)

valex
valex

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

Arion
Arion

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

Related Questions