【问题标题】:Insert all records from selected tables knowing records value插入选定表中的所有记录,知道记录值
【发布时间】:2016-08-05 17:41:20
【问题描述】:

您好,我想知道如何将选定表中的记录插入到特定的表中,并使用相同的列模型。这是一种只从一个表中插入的方法

insert  into  shema2.MAILING_PERSON_LOG_TAB
select  *  from  shema1.mailing_person_log_201008  t
where  t.object_type  =  'active'

然后我可以选择表名。有人能帮我一把吗,如何使用 select 或 procedure 从所有表中进行操作

select t.TABLE_NAME FROM all_tables t WHERE t.table_name LIKE 'MAILING_PERSON_LOG%'

【问题讨论】:

  • 如您所见,添加数据时添加表格是个坏主意。这不是数据库应该如何工作的。应该只有一个表mailing_person_log 带有一列代表月份(或日期),因此您只需向该表中添加行以获取新数据。那么这样的问题如何编写查询来选择超过一个月的数据根本就不存在。因此我的建议是:改变你的数据模型。

标签: sql oracle select


【解决方案1】:

将插入语句构建为命令字符串并动态执行它,例如(用于说明的代码,根据您的要求进行调整):

declare
  l_to_tablename varchar2(100);
begin
  -- loop over tables to copy
  for l_rec in (select t.TABLE_NAME
                  FROM all_tables t
                 WHERE t.table_name LIKE 'MAILING_PERSON_LOG%') loop

    -- determine to_tablename from somewhere
    select to_tablename
      into l_to_tablename
      from tablemapping t
     where t.table_name = l_rec.table_name;

    execute immediate 'insert  into  shema2.' || l_to_tablename ||
                      ' select  *  from  shema1.' || l_rec.table_name ||
                      ' t where  t.object_type  =  ''active''';

  end loop;
end;

注意:如果表包含长字段,这将不起作用。

【讨论】:

  • Developer getting error in line: from tablemapping t 我想知道我应该在哪里声明 dest。表 shema2.MAILING_PERSON_LOG_TAB
  • 我假设您将源表和目标表的名称存储在一个名为 tablemapping 的表中。正如我在上面所写的:根据您的需要调整此代码。
猜你喜欢
  • 2013-02-20
  • 1970-01-01
  • 2015-08-02
  • 2018-11-04
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多