Pregunta

I need to append SYSDATE while creating a table in Oracle. Please let me know if there is a direct way of doing it (Using only SQL) or I have to use PL/SQL for the same.

E.g. If table name is ABC, I need to create table as ABC_20130416.

Let me know if more info is needed.

¿Fue útil?

Solución

If SQL*Plus will always be available, you can do this using variables, something like (untested):

SQL>col yyyymmdd for a8 new_value v
SQL>select to_char(sysdate, 'YYYYMMDD') as yyyymmdd from dual;
SQL>create table ABC_&v (col1 integer);

On the other hand, if you want to able to do it anywhere you will need PL/SQL and dynamic SQL:

declare
  l_tablename varchar2(30) := 'ABC_' || to_char(sysdate, 'YYYYMMDD')
begin
  execute immediate 'create table ' || l_tablename || ' (col1 integer)';
end;
/

Or just create the table normally first and then rename:

create table xyz (
  ... many columns ...
);

declare
  l_tablename varchar2(30) := 'ABC_' || to_char(sysdate, 'YYYYMMDD')
begin
  execute immediate 'rename xyz to ' || l_tablename;
end;
/

Otros consejos

With PL/SQL only:

begin
  execute immediate 'create table foo_' || your_timestamp_here ;
end;

PL/SQL CODE:

declare
  tabname varchar(32);
begin
  tabname := 'abc'|| TO_CHAR(SYSDATE, 'YYYYMMDD');
  execute immediate 'create table '|| tabname ||' ( f1 number, f2 number, f3 number)';
end;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top