将 sysdate 附加到 Oracle 中的表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16031050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Append sysdate to a table name in Oracle
提问by Sid
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.
我需要在 Oracle 中创建表时附加 SYSDATE。请让我知道是否有直接的方法(仅使用 SQL)或者我必须使用 PL/SQL。
E.g. If table name is ABC, I need to create table as ABC_20130416.
例如,如果表名是 ABC,我需要创建表为 ABC_20130416。
Let me know if more info is needed.
如果需要更多信息,请告诉我。
回答by Colin 't Hart
If SQL*Plus will always be available, you can do this using variables, something like (untested):
如果 SQL*Plus 始终可用,您可以使用变量来执行此操作,例如(未经测试):
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:
另一方面,如果你想在任何地方都能做到,你将需要 PL/SQL 和动态 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;
/
回答by Michael-O
With PL/SQL only:
仅使用 PL/SQL:
begin
execute immediate 'create table foo_' || your_timestamp_here ;
end;
回答by M.J.Ahmadi
PL/SQL CODE:
PL/SQL 代码:
declare
tabname varchar(32);
begin
tabname := 'abc'|| TO_CHAR(SYSDATE, 'YYYYMMDD');
execute immediate 'create table '|| tabname ||' ( f1 number, f2 number, f3 number)';
end;