0

Is it possible to use IF ELSE inside WHERE clause something like this...

WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
IF(@transactionType='1')
AND qTable.statusId like 'SIGNED'
ELSE
AND rTable.statusId like 'ACTIVE'
shree.pat18
  • 20,943
  • 3
  • 37
  • 58
Reynan
  • 241
  • 2
  • 8
  • 23
  • possible duplicate of [SQL: IF clause within WHERE clause](http://stackoverflow.com/questions/87821/sql-if-clause-within-where-clause) – Alberto Solano Apr 11 '14 at 08:03

3 Answers3

2

You cannot use the IF ELSE statement within the WHERE clause. Use CASE instead:

WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND CASE WHEN @transactionType = '1' THEN qTable.statusId like 'SIGNED' 
    ELSE rTable.statusId like 'ACTIVE' END
Alberto Solano
  • 7,752
  • 3
  • 35
  • 59
1
WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND 
  ((@transactionType='1' AND qTable.statusId like 'SIGNED')
OR 
  (@transactionType <> '1' AND like 'ACTIVE') )
Milen
  • 8,322
  • 7
  • 40
  • 55
0

Is this what you need?

WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND ((transactionType='1' and qTable.statusId like 'SIGNED') or (transactionType <> '1' and rTable.statusId like 'ACTIVE'))
Esko
  • 3,838
  • 2
  • 18
  • 36