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 th开发者_运维问答ere a way to implement a logic like that one ?
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'))
精彩评论