Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Re: PLSQL function
In article <9u29ad$h0g$1_at_unbe.sarenet.es>, "Jon" says...
>
>Hi,
>
>I like to know if it is posible to create a new function so that I could
>pass to it some numeric variables and on the other hand a string variable
>that contains a formula doing some calculations with the numeric variables I
>have also passed to the function. The contect of the string variable could
>look like this "+ cost - vat + net_change". So the question is if it could
>be posible, using PLSQL, to evalute the content of the variable with the
>formula and give me back the result of apliying the formula to the numeric
>values.
>
>Thanks for you help
>
>
Yes, you would use dbms_sql to do this. Do OVER DO this, it obviously is not as fast as static operations.
ops$tkyte_at_ORA815.US.ORACLE.COM> create or replace package eval 2 as
3 function do_it 4 ( p_function in varchar2, 5 p_var1 in varchar2 default null, p_val1 in varchar2 default null, 6 p_var2 in varchar2 default null, p_val2 in varchar2 default null, 7 p_var3 in varchar2 default null, p_val3 in varchar2 default null, 8 p_var4 in varchar2 default null, p_val4 in varchar2 default null) 9 return number;
Package created.
ops$tkyte_at_ORA815.US.ORACLE.COM>
ops$tkyte_at_ORA815.US.ORACLE.COM> create or replace package body eval
2 as
3
4 g_theCursor integer default dbms_sql.open_cursor; 5 g_theStmt long default 'x';
10 if ( p_name is not null ) 11 then 12 dbms_sql.bind_variable( g_theCursor, p_name, p_val ); 13 end if;
18 ( p_function in varchar2, 19 p_var1 in varchar2 default null, p_val1 in varchar2 default null, 20 p_var2 in varchar2 default null, p_val2 in varchar2 default null, 21 p_var3 in varchar2 default null, p_val3 in varchar2 default null, 22 p_var4 in varchar2 default null, p_val4 in varchar2 default null)23 return number
25 l_rowsprocessed number default 0; 26 l_retval number; 27 begin 28 if ( g_theStmt <> p_function ) 29 then 30 dbms_sql.parse(g_theCursor, 31 'begin :ret_val := ' || p_function || '; end;', 32 dbms_sql.native ); 33 g_theStmt := p_function; 34 end if; 35 36 dbms_sql.bind_variable( g_theCursor, ':ret_val', 0 ); 37 bind( p_var1, p_val1 ); 38 bind( p_var2, p_val2 ); 39 bind( p_var3, p_val3 ); 40 bind( p_var4, p_val4 ); 41 42 l_rowsprocessed := dbms_sql.execute(g_theCursor); 43 dbms_sql.variable_value( g_theCursor, ':ret_val', l_retval ); 44 45 return l_retval;
Package body created.
ops$tkyte_at_ORA815.US.ORACLE.COM>
ops$tkyte_at_ORA815.US.ORACLE.COM> begin
2 dbms_output.put_line
3 ( eval.do_it( '+ :cost - :vat + :net_change', 4 'cost', 55, 5 'vat', 10, 6 'net_change', 1000 )7 );
9 ( eval.do_it( '+ :cost - :vat + :net_change', 10 'cost', 12, 11 'vat', 13, 12 'net_change', 2000 )13 );
PL/SQL procedure successfully completed.
ops$tkyte_at_ORA815.US.ORACLE.COM>
-- Thomas Kyte (tkyte@us.oracle.com) http://asktom.oracle.com/ Expert one on one Oracle, programming techniques and solutions for Oracle. http://www.amazon.com/exec/obidos/ASIN/1861004826/ Opinions are mine and do not necessarily reflect those of Oracle CorpReceived on Wed Nov 28 2001 - 08:11:03 CST