我有一个包含以下值的字符串:
COND_STRING = '(TRUE AND FALSE AND TRUE) OR FALSE'
如果在SQL Server存储过程中为True或False(1或0),如何评估结果?
答案 0 :(得分:2)
我只能想到这种方式。使用Dynamic SQL
declare @str varchar(100) = '(TRUE AND FALSE AND TRUE) OR FALSE'
select @str = replace(replace(replace(replace(@str, 'TRUE', '1'), 'FALSE', 0), 'AND', '&'), 'OR', '|')
select @str = 'SELECT' + @str
print @str
exec (@str)
答案 1 :(得分:1)
declare @new_cond = replace(replace(@cond_string, 'TRUE', '(1=1)'), 'FALSE', '(1=0)');
declare @sql = 'select @result = (case when @cond then 1 else 0 end)';
set @sql = replace(@sql, '@cond', @new_cond);
declare @result int;
exec sp_executesql @sql, N'@result int output', @result=@result output;