plsql - How to use variables in Oracle PL/SQL Function -
i'm sorry upfront because question seems easy. have function:
create or replace function costs_mk (viewname in varchar2 , where_clause in varchar2) return varchar2 v_costs varchar2 (500); begin select listagg(costs, ';' ) within group (order costs) v_costs (select distinct (costs) viewname where_clause); return v_costs; end costs_mk;
however error-message:
error(13,30): pl/sql: ora-00920: invalid relational operator
i can't compile it. if use exact values viewname , where_clause desired result.
what doing wrong?
/edit: line 13
from viewname
/edit #2: guys. helped me lot. didn't thought dynamic sql in first step, refresher ;).
i suggest add exception block
along execute immediate
have created procedure
can similary create function
create or replace procedure costs_pk(viewname in varchar2 , where_clause in varchar2 ) v_costs varchar2 (500); sql_stmnt varchar2(2000); begin sql_stmnt := 'select listagg(cost, '';'' ) within group (order cost) (select distinct (cost) ' || viewname || ' ' || where_clause || ' ) '; --sql_stmnt := 'select listagg(cost, '';'' ) within group (order cost) (select distinct (cost) cost_tab cost >=123 ) '; execute immediate sql_stmnt v_costs ; dbms_output.put_line ('query works -- ' || v_costs); exception when others dbms_output.put_line ('input :' || viewname || ' , ' || where_clause ); dbms_output.put_line (sql_stmnt ); dbms_output.put_line ('error message : ' || sqlcode || ' ' || sqlerrm ); end; begin costs_pk('cost_tab','cost >= 123'); end;
note:
code has been tested
output:
query works -- 123;456
Comments
Post a Comment