使用 sysdate 更改 table 名称

Change table name with sysdate

我想通过附加 SYSDATE 来更改 table 名称。例如,我想将 table EXAMPLE_TABLE 更改为 EXAMPLE_TABLE_05_01_2015,但我想从 SYSDATE.

获取日期

我准备了以下内容,但没有用:

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

我怎样才能让它发挥作用?

这是错误:

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;

使用execute immediate.

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

也就是说,我有预感你最好使用分区表。

SQL*Plus 中,您可以使用 variable substitution.

另一种方式 :

SQL> CREATE TABLE t(ID NUMBER)
  2  /

Table created.

SQL>
SQL> COLUMN new_tablename NEW_VALUE new_tablename
SQL> SELECT 't_' || to_char(sysdate, 'dd_mm_yyyy') AS new_tablename from dual
  2  /

NEW_TABLENAM
------------
t_05_01_2015

SQL>
SQL> RENAME t TO &new_tablename
  2  /
old   1: RENAME t TO &new_tablename
new   1: RENAME t TO t_05_01_2015

Table renamed.

SQL>
SQL> select * from t_05_01_2015;

no rows selected

SQL>

所以,现在 table T 重命名T_05_01_2015