title: Function to set a sequence's currval
body: <p>I wrote this function to set my sequence's currval to maximum id+1. But I get errors like:</p>
  
  <pre><code>ORA-14552: cannot perform a DDL, commit or rollback inside a query or DML 
  </code></pre>
  
  <p>Please help me to solve that issue.</p>
  
  <pre><code>CREATE OR REPLACE Function createcurrentid (
     model_in IN varchar2,
     primarykey_in IN VARCHAR2,
     seq_name IN VARCHAR2)
    RETURN number
  IS
    cnumber number;
    c2      sys_refcursor;
    c3      sys_refcursor;
    c6      sys_refcursor;
    c7      sys_refcursor;
    c8      sys_refcursor;
    c9      sys_refcursor;
    c4 number;
    c5 number;
    type result_rec is record (
      id      number,
     currvalvalue  number
   );
   l_result_rec result_rec;
  BEGIN
   open c2 FOR 'SELECT max('||primarykey_in||') AS id FROM '||model_in;
   fetch c2 into c4;
   close c2;
   open c3 FOR 'select '||seq_name||'.currval AS currvalvalue from dual';
   fetch c3 into c5;
   close c3;
   cnumber:=c4-c5;
   open c6 FOR 'alter sequence'||seq_name||' increment BY'||cnumber;
   commit;
   open c8 FOR 'select '||seq_name||'.nextval from dual';
   commit;
   open c9 FOR 'select '||seq_name||'.CURRVAL from dual';
   commit;
   open c7 FOR 'alter sequence'||seq_name||' increment BY 1';
   commit;
   RETURN cnumber;
   END;
  </code></pre>