📜  frename oracle - SQL (1)

📅  最后修改于: 2023-12-03 15:00:51.180000             🧑  作者: Mango

Frename Oracle - SQL

Are you tired of manually renaming your Oracle database schema objects one by one?

Frename Oracle - SQL script is here to rescue you! With just a few lines of code, you can easily rename your schema objects in one go.

Features
  • Easy to use: Just run the script with the old schema name and the new schema name.
  • Supports multiple types of schema objects: Tables, views, sequences, procedures, functions, triggers, and more.
  • Generates a SQL script for backup purposes: In case something goes wrong, you can easily revert to the old schema name.
How to Use
  1. Paste the code below into a SQL worksheet in your Oracle database.
  2. Replace old_schema_name and new_schema_name with the appropriate values.
  3. Execute the script.
set serveroutput on;
DECLARE
  old_schema VARCHAR2(30) := 'old_schema_name';
  new_schema VARCHAR2(30) := 'new_schema_name';
BEGIN
  FOR t IN (SELECT object_type, object_name
            FROM   user_objects
            WHERE  object_type IN ('TABLE', 'VIEW', 'SEQUENCE', 'PROCEDURE', 'FUNCTION', 'TRIGGER')
            AND    object_name NOT LIKE 'BIN$%'
            AND    object_name NOT LIKE 'SYS_%'
            AND    object_name NOT LIKE 'CREATE_%'
            AND    object_name NOT LIKE 'DROP_%'
            AND    object_name NOT LIKE 'RENAME_%')
  LOOP
    IF t.object_type = 'TABLE' OR t.object_type = 'VIEW' THEN
      EXECUTE IMMEDIATE 'RENAME '||old_schema||'.'||t.object_name||' TO '||new_schema||'.'||t.object_name;
    ELSIF t.object_type = 'SEQUENCE' THEN
      EXECUTE IMMEDIATE 'RENAME '||old_schema||'.'||t.object_name||' TO '||new_schema||'.'||t.object_name;
    ELSE
      EXECUTE IMMEDIATE 'ALTER '||t.object_type||' '||old_schema||'.'||t.object_name||' RENAME TO '||new_schema||'.'||t.object_name;
    END IF;
    dbms_output.put_line(t.object_type||': '||t.object_name||' renamed to '||new_schema||'.'||t.object_name);
  END LOOP;

  dbms_output.put_line('SQL Script for Backup:');
  FOR s IN (SELECT object_type, object_name
            FROM   user_objects
            WHERE  object_type IN ('TABLE', 'VIEW', 'SEQUENCE', 'PROCEDURE', 'FUNCTION', 'TRIGGER')
            AND    object_name NOT LIKE 'BIN$%'
            AND    object_name NOT LIKE 'SYS_%'
            AND    object_name NOT LIKE 'CREATE_%'
            AND    object_name NOT LIKE 'DROP_%'
            AND    object_name NOT LIKE 'RENAME_%')
  LOOP
    IF s.object_type = 'TABLE' OR s.object_type = 'VIEW' THEN
      dbms_output.put_line('ALTER '||new_schema||'.'||s.object_name||' RENAME TO '||old_schema||'.'||s.object_name||';');
    ELSIF s.object_type = 'SEQUENCE' THEN
      dbms_output.put_line('ALTER '||new_schema||'.'||s.object_name||' RENAME TO '||old_schema||'.'||s.object_name||';');
    ELSE
      dbms_output.put_line('ALTER '||s.object_type||' '||new_schema||'.'||s.object_name||' RENAME TO '||old_schema||'.'||s.object_name||';');
    END IF;
  END LOOP;
END;
/
Notes
  • This script does not change the password for the schema. You will need to do that manually.
  • Be careful when using this script in a production environment. Always make sure you have proper backups before running it.
  • This script only works for objects owned by the current user. If you need to rename objects owned by another user, you will need to modify the script accordingly.

Use Frename Oracle - SQL script to save time and effort when renaming your Oracle database schema objects. Happy coding!