使用附加列复制表

Car*_*s00 0 oracle plsql stored-procedures

如何编写一个PL/SQL程序:

  • 按名称将表给出的表复制到另一个给定的表中(不存在)
  • 向第二个表添加一个ts填充了当前时间戳/日期时间的新列

APC*_*APC 5

您需要使用动态SQL来实现此目的.

create or replace procedure clone_table 
    ( p_copy_table in varchar2 
      , p_new_table in varchar2
      , p_inc_data in varchar2 := 'Y' )
is
    stmt varchar2(32767);
begin
    stmt := 'create table '|| p_new_table
                 || ' as select t.*, systimestamp as ts '
                 || ' from ' || p_copy_table || ' t';
    if p_inc_data != 'Y' then
        -- use a empty resultset to create an empty table
         stmt := stmt || ' where 1=0';
    end if;
    execute immediate stmt;
end;
Run Code Online (Sandbox Code Playgroud)