如何判断动态表达式是否成立
有一表达式字段,值为‘(@ContractSum > = 100000) OR (@Is_importance = true) OR (@Is_JoinForce = 1)’,现需根据页面输入的数据判断此表达式是否成立,判断前已将页面相应数据值写入表,用以下语句取出数据值后替代@ContractSum等变量:
declare @resu bit
declare @varname varchar(50)
declare @varvalue varchar(1000)
declare @v cursor
set @v = cursor for select VarName,VarValue from WF_RT_ProcessData where ProcessRTID = @rt_process_id
open @v
fetch next from @v into @varname,@varvalue
while(@@fetch_status = 0)
begin
--寻找变量,将变量值代入表达式
if CHARINDEX( '@ '+rtrim(@varname),@expression) > 0 --找到变量名,替换变量值
select @expression = REPLACE(@expression, '@ '+rtrim(@varname), ' ' ' '+rtrim(@varvalue)+ ' ' ' ');
fetch next from @v into @varname,@varvalue
end
现在的问题是不知道如何判断@expression的真假,试过以下办法,但不成功
set @sql = 'if( '+ @expression + ') set @resu = 1 else set @resu = 0 '
exec(@sql)
------解决方案--------------------declare @expression varchar(100),@resu bit,@sql nvarchar(200)
set @expression= '(1000 > = 100000) OR (1 = 1) '
set @sql = 'if( '+ @expression + ') set @resu = 1 else set @resu = 0 '
--print @sql
exec sp_executesql @sql,N '@resu bit output ',@resu output
print @resu
------解决方案--------------------从动态SQL中获取值应该按如下处理:
--如何将exec执行结果放入变量中?
declare @num int, @sql nvarchar(4000)
set @sql= 'select @a=count(*) from tableName '
exec sp_executesql @sql,N '@a int output ',@num o