How to rename multiple stored procedures in Oracle

Viewed 108

I'm using Oracle 12C, now I'm in a following trouble:

I have multiple stored procedures like:

schema.TEST1, schema.TEST2, schema.TEST3....

Now, I want to rename all of them to schema.TEST01, schema.TEST02, schema.TEST03...or any name I want which was formatted before, this is for backup.

In Oracle, I can't rename a stored procedure using a ALTER statement rename like SQL. How can I do this with one click?

Thanks!

3 Answers

For backup? That's rather poorly chosen backup system.

  • what if database dies because of disk failure? You'll lose everything (including your "backup" procedures)
  • how many "backups" do you plan to keep? For example, one of my schemas contains 643 procedures/functions/packages. With two backups, I'm already close to 2K objects. If you perform backup regularly (e.g. daily), in a matter of only a month, I'd be close to 20K objects. I really wouldn't want to do that

Therefore, why wouldn't you consider something else? For example,

  • version control system (such as Git)
  • perform Data Pump Export as a "logical" backup
    • let DBA take care about RMAN backup
  • if you want to do it manually, some GUI tools (such as TOAD) let you select all and create script - that option stores source code as files on your hard disk drive, and then you can backup those files somewhere else (burn them on a DVD, copy to USB memory stick, another hard disk drive, somewhere within your network ...)

Finally, to answer your question: how to do what you asked for in one click? As far as I can tell, you can't. You'd first have to write a procedure which would do the job, but then you're back to my second objection to your approach. How will that procedure know that proc1 is "original", while proc01 is a backup version? Why wouldn't someone name their procedures proc05 initially? That's a valid name.

Make changes according to your schema and naming convention. But it is nonsense, you do not need to backup in that way. But I took it as a challenge and would like to present you the below code

Use CLOB if source text is large enough.

DECLARE
type names_table is table of VARCHAR2(50);
  names names_table;
TYPE source_txt_table is TABLE OF VARCHAR2(32767);
  source_txt source_txt_table;
  header VARCHAR2(32767);
  final_sourc_txt VARCHAR2(32767);
BEGIN

  SELECT OBJECT_NAME bulk COLLECT into names from user_procedures WHERE object_type = 'PROCEDURE' AND OBJECT_NAME IN ('DO_SOMETHING_1','DO_SOMETHING_2');

  FOR i in 1..names.LAST
  LOOP

    SELECT text bulk COLLECT into source_txt
             FROM all_source
             WHERE name = names(i)
             ORDER BY line;

      source_txt(1) := 'CREATE OR REPLACE '||source_txt(1);

     header := REGEXP_REPLACE(upper(source_txt(1)), names(i), 'HR.'||names(i)||'_bck');  --make changes according to new naming convention

     source_txt(1) := header;

      FOR j in 1..source_txt.LAST
      LOOP

       final_sourc_txt := final_sourc_txt||source_txt(j);

      END LOOP;
      EXECUTE IMMEDIATE final_sourc_txt;
      dbms_output.put_line('Success: '|| names(i));
      final_sourc_txt := NULL;
      header := NULL;
      source_txt := NULL;
  END LOOP;

END;

You can also try using DBMS_METADATA PACKAGE to export DDLs of the schema object. I have written an example, you can use it after modifying it according to your needs.

CREATE DIRECTORY EXTERNAL AS '/external/';

DECLARE
  h PLS_INTEGER;
  th PLS_INTEGER;
  fh utl_file.file_type;
  ddls CLOB;
  SYSD VARCHAR2(50);
BEGIN
  h := dbms_metadata.open('PROCEDURE');

  DBMS_METADATA.set_filter(h, 'SCHEMA','HR');
  th := DBMS_METADATA.ADD_TRANSFORM (h, 'DDL');
  DBMS_METADATA.SET_COUNT(h, 50);

  ddls := dbms_metadata.fetch_clob(h);

  SELECT TO_CHAR(SYSDATE, 'YYYYMMDDHHMISS') INTO SYSD FROM dual;

  fh := utl_file.fopen('EXTERNAL', 'SCHEMA_BCK_'||SYSD||'.bck', 'w');

    utl_file.put(fh, ddls);
    UTL_FILE.FCLOSE(fh);

  DBMS_METADATA.CLOSE(h);
END;

It is far safer against database failures and you will not unnecessarily populate your database schema with backup objects.

Related