Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change table name with sysdate

Tags:

sql

oracle

I want to change a table name by appending SYSDATE to it. For example, I want to change table EXAMPLE_TABLE to EXAMPLE_TABLE_05_01_2015, but I want to get the date from SYSDATE.

I prepared the following but it is not working:

ALTER TABLE "MYDB"."EXAMPLE_TABLE" rename to (SELECT 'EXAMPLE_TABLE' || TO_CHAR(SYSDATE, '_dd_MM_yyyy') FROM DUAL);

How can I make it work?

Here is the error:

SQL Error: ORA-14047: ALTER TABLE|INDEX RENAME may not be combined with other operations
14047. 00000 -  "ALTER TABLE|INDEX RENAME may not be combined with other operations"
*Cause:    ALTER TABLE or ALTER INDEX statement attempted to combine
           a RENAME operation with some other operation which is illegal
*Action:   Ensure that RENAME operation is the sole operation specified in
           ALTER TABLE or ALTER INDEX statement;
like image 873
guneykayim Avatar asked Jan 05 '15 10:01

guneykayim


1 Answers

Use execute immediate.

begin
   execute immediate 
   'alter table mydb.example_table rename to ' ||
   'example_table_' || to_char(sysdate, 'dd_mm_yyyy');
end;
/

That said, I have the hunch that you'd be better off using partitioned tables.

like image 190
René Nyffenegger Avatar answered Sep 30 '22 07:09

René Nyffenegger