oracle 判断条件问题

我在用plsql写的存储过程中 ,遇到if的判断条件是个子字符串,如:
v_tiaojian varchar2(20);
v_tiaojian :='1240<1400 and 1240>1000';
if v_tiaojian then
....
在执行时会报变量类型错误 请问如何将字符串变为if的判断条件。

declare
v_tiaojian varchar2(100);
begin
v_tiaojian :='1240<1400 and 1240>1000';
if  v_tiaojian is not null then
  dbms_output.put_line('成功');
end if;
end;

 

类似这样

if判断的是true或者false

---------补充----------

貌似这样也行

declare
V_Tiaojian Varchar2(100);
b boolean;
Begin
V_Tiaojian :='1240<1400';
execute immediate ('select count(*) from dual where '||v_Tiaojian) into b;
if  b then  
  dbms_output.put_line('成功');
End If;
end;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-26

1、创建函数CheckCondition

create or replace function CheckCondition(vCondition in varchar2) return Integer is
  iCurID  Integer; 
  iResult Integer;
  vSql varchar2(2000);
  iC Integer;
begin
  if vCondition is null then
    Return 1;     -- 条件成立
  end if;
 vSql := 'select Count(1) as C from dual where ' || vCondition;
  iCurID:= Dbms_Sql.Open_Cursor;
  Dbms_Sql.Parse(iCurID, vSql, Dbms_Sql.V7); 
  Dbms_Sql.Define_Column(iCurID, 1, iC);   
  iResult:= Dbms_Sql.Execute(iCurID);
  if Dbms_Sql.Fetch_Rows(iCurID) > 0 then
    Dbms_Sql.Column_Value(iCurID, 1, iC);
  else
    ic:= -1;  
  end if;  
  Dbms_Sql.Close_Cursor(iCurID);
  Return iC;
exception
   when others then
    dbms_output.put_line(sqlerrm);
     Return -2;
end;
/
2、改写你的代码
v_tiaojian varchar2(20);
v_tiaojian :='1240<1400 and 1240>1000';
i_CheckCond Integer;
i_CheckCond:= CheckCondition(v_tiaojian);
if i_CheckCond = 1 then    --  条件成立
...
else if i_CheckCond = 0 then -- 条件不成立
...
else   -- 条件语句有问题
...
end if;

 希望对你有所帮助。

追问

谢谢你费心的解答!

第2个回答  2013-11-26
IF要判断的表达式或变量应该是个逻辑值 ,你现在给他的是个字符,你应该把变量v_tiaojian声明成boolean,
v_tiaojian Boolean ;
v_tiaojian := 1240<1400 and 1240>1000;
if (v_tiaojian) then
...
这样应该就可以了