0

I have to search for data across three columns. For certain users I search column 1, for other users I search column 1 and 3, and finally for other I look at column 2.

I know I could use a case or if statement and say if userId = 1 then run a particular SQL statememnt. But the statement is pretty big, and I don't want to repeat the whole thing every time. Is it possible to use a CASE or IF statement (or something else) to change the conditions in the WHERE part of the statement?

for example, if the company name is x check for value in column 1 or 3, otherwise check in column 2.

I've a very small SQL Fiddle here, but I've no idea where to begin.

mal
  • 3,022
  • 5
  • 32
  • 62

1 Answers1

0

You can always add the logic you need in the where statement directly using OR clauses:

[...] WHERE ( company_name = 'x' and (column_1 = @p or column_3 = @p) )
            OR ( company_name <> 'x' and column_2 = @p )
xtoik
  • 676
  • 5
  • 11