views:

36

answers:

1

What I want to do is something like this

DECLARE @operator nvarchar;
SET @operator = 'AND'

SELECT * FROM MyTable
WHERE first_column = "1" @operator second_columnt = "2"

is there a way to implement a logic like that one ?

+8  A: 

You can do that using dynamic sql:

declare @query varchar(max)
set @query = 'select * from MyTable where first_column = ''1'' ' +
    @operator + ' second_column = ''2'''
exec (@query)

Sometimes, where statement logic is sufficient, like:

select  *
from    MyTable
where   (operator = 'AND' and first_column = '1' and second_column = '2')
        or (operator = 'OR' and (first_column = '1' or second_column = '2'))
Andomar