Analytics


Google

Friday, August 31, 2012

Executing dynamic code in PL/SQL

This is a sample of how to execute a dynamic code to create a partition:

  PROCEDURE cre_part_tab
    (ptable IN VARCHAR2,
     ptag   IN VARCHAR2,
     plim   IN VARCHAR2)
  IS

  /*********************************************************
   *  Procedure to facilitate creating of new Partitions
   *  Author  Strovek
   *  Date    Aug 15, 2001
   *********************************************************/

    v_Cursor         INTEGER;
    v_creString      VARCHAR2(100);
  BEGIN
    v_Cursor := DBMS_SQL.OPEN_CURSOR;
    v_creString := 'ALTER TABLE ' || ptable || ' ADD PARTITION '
      || ptable || '_' || ptag || ' VALUES LESS THAN (''' || plim || ''')';
    DBMS_SQL.PARSE(v_Cursor, v_creString,DBMS_SQL.V7);
  EXCEPTION
    WHEN OTHERS THEN
      IF DBMS_SQL.IS_OPEN(v_Cursor) then
        DBMS_SQL.CLOSE_CURSOR(v_Cursor);
      End IF;
  END cre_part_stage;

Note that it is necessary to close the cursor should an error happen (to avoid leaving open cursors.  

pTable is the table name.
pTag is the tag for the partition.   Can be a running number.
pLim is the limit for the partition, that depends on how you partition your table, if by date then you need to supply a date.

No comments: