【问题标题】:check if new table exists in oracle sql检查oracle sql中是否存在新表
【发布时间】:2021-10-27 08:52:29
【问题描述】:

我要动态检查,

  • 如果存在新表,那么我想从新表中检索数据并将这些值插入到另一个表中。
  • 如果新表不存在,那么我想打印不存在新表。

表是AAA_AAA,

emp_id emp_name emp_salary
120 abc 50000
121 bcd 45000

如果存在新表,那么我必须从上表中选择值并将这些值插入到其他表中。

如果新表不存在,那么我想打印不存在新表。

我已经写了下面的查询,

select object_name from user_objects , case when select object_name from user_objects where created >= (sysdate-12/24) then select * from AAA_AAA into BBB_BBB;

BBB_BBB 是表名,如果存在新创建的表,我必须在其中插入 AAA_AAA 表中的值。

但我没有得到想要的输出。

【问题讨论】:

  • 这有点糟糕的设计。除了代码之外,您如何在逻辑上确定存在“新”表?这个“新”表将被视为“新”多久?将来还会有更多、更新的“新”表吗?这张新桌子有什么商业用途?从您对其用途的描述来看,听起来它只是将数据加载到最终目的地的入口点。那么这个“新”表的数据是从哪里来的呢?我强烈怀疑有更好的方法。
  • 会有更多的表。每 30 分钟将创建新表
  • “每 30 分钟将创建新表” 剩下的问题呢?这听起来越来越像是整个设计需要重新思考。
  • 您不应该“每 30 分钟”创建新表。甚至不是每个月。听起来您每 30 分钟就有一个新数据文件,您正试图将其加载到“旧”表中。为此,您应该使用单个外部表,根据需要替换数据文件,或使用 sqlldr。

标签: oracle oracle11g oracle10g oracle-sqldeveloper


【解决方案1】:

方法有很多,一种如下:

select table_name from user_tables where table_name='MYTABLE';

如果上述方法有效,则使用 PHPC# 等后弯语言进行插入,计算表号。

if(result.Count() > 0)
{
   //Do something
}   

【讨论】:

    【解决方案2】:

    您将需要动态 SQL,因为 - 如果表不存在 - 代码将失败。

    示例表 (aaa),而 bbb 不存在:

    SQL> set serveroutput on
    SQL>
    SQL> select * from aaa;
    
        EMP_ID EMP EMP_SALARY
    ---------- --- ----------
           120 abc      50000
           121 bcd      45000
    
    SQL> select * from bbb;
    select * from bbb
                  *
    ERROR at line 1:
    ORA-00942: table or view does not exist
    

    完成这项工作的代码:

    SQL> declare
      2    l_cnt number;
      3  begin
      4    select count(*)
      5      into l_cnt
      6      from user_tables
      7      where table_name = 'BBB';
      8
      9    if l_cnt = 0 then
     10       dbms_output.put_line('Table BBB does not exist');
     11    else
     12      execute immediate 'insert into bbb (emp_id, emp_name, emp_salary)' ||
     13        ' select emp_id, emp_name, emp_salary from aaa';
     14      dbms_output.put_line('Table AAA copied to table BBB');
     15    end if;
     16  end;
     17  /
    Table BBB does not exist                      --> exactly, it doesn't exist
    
    PL/SQL procedure successfully completed.
    

    让我们创建一个空目标bbb 表并重复该代码:

    SQL> create table bbb as select * From aaa where 1 = 2;
    
    Table created.
    
    SQL> select * from bbb;
    
    no rows selected
    
    SQL> declare
      2    l_cnt number;
      3  begin
      4    select count(*)
      5      into l_cnt
      6      from user_tables
      7      where table_name = 'BBB';
      8
      9    if l_cnt = 0 then
     10       dbms_output.put_line('Table BBB does not exist');
     11    else
     12      execute immediate 'insert into bbb (emp_id, emp_name, emp_salary)' ||
     13        ' select emp_id, emp_name, emp_salary from aaa';
     14      dbms_output.put_line('Table AAA copied to table BBB');
     15    end if;
     16  end;
     17  /
    Table AAA copied to table BBB                        --> target table now exists
    
    PL/SQL procedure successfully completed.
    
    SQL> select * from bbb;
    
        EMP_ID EMP EMP_SALARY
    ---------- --- ----------
           120 abc      50000                           --> here's its contents
           121 bcd      45000
    
    SQL>
    

    【讨论】:

      猜你喜欢
      • 2011-05-22
      • 1970-01-01
      • 2010-10-02
      • 2011-07-21
      • 2012-01-08
      • 2010-09-15
      • 2019-11-25
      • 1970-01-01
      相关资源
      最近更新 更多