动态执行sql,返回一个值

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

  function dyn_sql(p_sql in varchar2) return varchar2 is
    c      integer;
    result varchar2(4000);
    l_temp number;
  begin
    c := dbms_sql.open_cursor;
    dbms_sql.parse(c, p_sql, dbms_sql.native);
    if upper(p_sql) like 'SELECT%' then
      dbms_sql.define_column(c, 1, result, 4000);
      l_temp := dbms_sql.execute_and_fetch(c);
      if l_temp > 0 then
        dbms_sql.column_value(c, 1, result);
      else
        result := null;
      end if;
    else
      l_temp := dbms_sql.execute(c);
      result := null;
    end if;
    dbms_sql.close_cursor(c);
    return result;
  end dyn_sql;