Home » SQL & PL/SQL » SQL & PL/SQL » Evaluating Logical Expression
Evaluating Logical Expression [message #36316] Fri, 16 November 2001 01:23 Go to next message
Pravesh Sharma
Messages: 1
Registered: November 2001
Junior Member
My query was related to parsing of logical pl/sql expressions.
I will explain you with a example... (pl/sql code)

declare
myexpression varchar2(100);
begin
myexpression := '10 > 20 and 5 >100';

how can I know whether the myexpression is evaluating to true or false.
The expression will be change on runtime so even I could not write

boolean b := myexpression;

Pls guide.

I can write a parser for that but it seems to be quite clumsy.
Thanks in advance

----------------------------------------------------------------------
Re: Evaluating Logical Expression [message #36333 is a reply to message #36316] Fri, 16 November 2001 09:15 Go to previous message
Hans
Messages: 42
Registered: September 2000
Member
connect test/test
 
create or replace function str2boolean(expr_in in varchar2)
return boolean is
   v_cursor        number;
   v_numrows       integer;
   blk_str         varchar2(2000);
   v_result        varchar2(10);
begin
   blk_str := 
      'begin' || chr(10) ||
      '   if (' || expr_in || ') = true then' || chr(10) ||
      '      :result := ''true'';' || chr(10) ||
      '   elsif (' || expr_in || ') = false then' || chr(10) ||
      '      :result := ''false'';' || chr(10) ||
      '   elsif (' || expr_in || ') is null then' || chr(10) ||
      '      :result := null;' || chr(10) ||
      '   end if;' || chr(10) || 
      'end;';
 
   v_cursor := dbms_sql.open_cursor;
   
   dbms_sql.parse(v_cursor, blk_str, dbms_sql.v7);
   
   dbms_sql.bind_variable(v_cursor, ':result', v_result, 10);
   v_numrows := dbms_sql.execute(v_cursor);
   dbms_sql.variable_value(v_cursor, ':result', v_result);
  
   dbms_sql.close_cursor(v_cursor);
   
   if v_result = 'true' then  
      return ( true );
   elsif v_result = 'false' then
      return ( false );
   else
      return ( null );
   end if;
   
exception
  when others then
    dbms_sql.close_cursor(v_cursor);
    raise;
end;
/
show errors
 
 
set serveroutput on
declare
   expr  varchar2(100);
begin
   expr := '10 > 20 and 5 >100';
   
   if str2boolean( expr ) = true then
      dbms_output.put_line('result is true');
   end if;
   if str2boolean( expr ) = false then
      dbms_output.put_line('result is false');
   end if;
   if str2boolean( expr ) is null then
      dbms_output.put_line('result is null');
   end if;  
end;
/
result is false


----------------------------------------------------------------------
Previous Topic: size of long column?
Next Topic: Conversion to Date Format
Goto Forum:
  


Current Time: Fri Aug 14 08:19:29 CDT 2020