📅  最后修改于: 2023-12-03 15:00:51.180000             🧑  作者: Mango
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.
old_schema_name
and new_schema_name
with the appropriate values.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;
/
Use Frename Oracle - SQL script to save time and effort when renaming your Oracle database schema objects. Happy coding!