下面实例用sys.dbms_sql执行sql例,可以选择SELECT 返回值也可选择FUNCTION调用返回值,具体运用要视实际情况做修改。
function GetsqlResult(
data_sql_ in varchar2) return varchar2is
sDatasql varchar2(2000);
sReturn varchar2(200);
cid_ number;
rows_ number;
stmt_ varchar2(2000);
begin
if instr(sDatasql,'[SELECT]') > 0 then --调用SELECT 返回
sDatasql := replace(sDatasql,'[SELECT]','');cid_ := dbms_sql.open_cursor;
dbms_sql.parse(cid_,sDatasql,dbms_sql.native);
sys.dbms_sql.define_column(cid_,1,sReturn,200);
rows_ := sys.dbms_sql.execute(cid_);
if (dbms_sql.fetch_rows(cid_) > 0) then
sys.dbms_sql.column_value(cid_,sReturn);
end if;
sys.dbms_sql.close_cursor(cid_);
elsif instr(sDatasql,'[FUN]') > 0 then --调用function返回值 sDatasql := replace(sDatasql,'[FUN]',''); if instr(upper(sDatasql),'BEGIN ') <= 0 and instr(sDatasql,';') <= 0 then sDatasql := 'BEGIN '||sDatasql|| '; END;'; elsif instr(upper(sDatasql),'BEGIN ') <= 0 then sDatasql := 'BEGIN '||sDatasql|| ' END;'; end if; execute immediate sDatasql using out sReturn; else sReturn := substrb(data_sql_,100); end if; return sReturn; exception when others then if dbms_sql.is_open(cid_) then dbms_sql.close_cursor(cid_); end if; return 'ERROR'; end GetsqlResult;