使用db_link插入会获得无效的标识符错误

时间:2017-11-14 11:02:25

标签: oracle plsql ora-00904

我正在构建一个查询,该查询在多个数据库中查找特定记录,并将它们插入到可以访问所有其余数据库的主数据库中。

但即使选择正常,插入也无法使用甚至不存在的同义词给我一个无效的标识符。

查询:

            INSERT INTO AUDIT_TABLE 

            Select Distinct tr.hmy, trim(tr.uref), tr.stotalamount, tr.upostdate, tr.sdateoccurred
            ,trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) 
            ,trim(v.ucode) 
            ,trim(v.ulastname) 
            ,trim(p.scode) 
            ,trim(p.saddr1) 
            ,trim(b.scode) 
            ,trim(b.sdesc) 
            ,l.icloseday 
            , case when l.icloseday <> 31 then trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) + (l.icloseday - 1 ) else last_day(trunc(tr.sdateoccurred )) end
            , case when l.icloseday <> 31 then 
            case when trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) + l.icloseday <= trunc(tr.sdateoccurred) then add_months((tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1),1)
              else trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) end
              else trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) end 
            ,trim(u.uname)
            ,'' 
            from trans@db_link tr
            join detail@db_link d on d.hchkorchg = tr.hmy
            join vendor@db_link v on tr.haccrualacct = v.hmyperson
            join property@db_link p on p.hmy = d.hprop
                 and p.itype = 3
            join bank@db_link b on b.hmy = tr.hperson
            join pmuser@db_link u on u.hmy = tr.husercreatedby
            join lockout@db_link l on l.hprop = p.hmy
            where 
            1=1 
            and tr.itype = 2 
            and tr.manualcheck <> 0 
            and tr.sdatecreated between to_date(trunc(sysdate, 'YEAR')) and to_date(add_months(trunc(sysdate, 'YEAR'), 12)-1)
            and case when l.icloseday <> 31 then 
              case when trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) + l.icloseday <= trunc(tr.sdateoccurred) then add_months((tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1),1)
                else trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) end
                else trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) end <> trunc(tr.UPOSTDATE)

错误:

            ORA-00904: "A2"."HUSERCREATEDBY": invalid identifier
            ORA-02063: preceding line from db_link

最奇怪的部分:所有这些数据库都具有相同的模式,但当我循环遍历所有数据库时,只有部分数据库因此错误而失败。

更新11/14/2017:

进一步了解它,似乎如果我消除了以下两个条件中的任何一个INSERT没有问题,但我一直在看它们并且不知道为什么引擎会因为这两个错误而出错简单的SELECT没有问题。

and tr.sdatecreated between to_date(trunc(sysdate, 'YEAR')) and to_date(add_months(trunc(sysdate, 'YEAR'), 12)-1)

and case when l.icloseday <> 31 then 
                             case when trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) + l.icloseday <= trunc(tr.sdateoccurred) then add_months((tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1),1)
                             else trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) end
                           else trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) end <> trunc(tr.UPOSTDATE)

1 个答案:

答案 0 :(得分:0)

仍然不确定为什么会出现这个问题,但如果有人遇到这个问题,我通过让db_link将远程信息插入远程表然后我将该信息插入主数据库中的本地表来解决它

begin
    sql_update:='DROP TABLE AUDIT_TABLE';
    execute immediate 'begin dbms_utility.exec_ddl_statement@'||v_database_name||'(:sql_update);  end;' using sql_update;

    EXCEPTION
        WHEN OTHERS THEN
          IF SQLCODE = -942 THEN
            NULL; -- suppresses ORA-00942 exception
          ELSE
             RAISE;
          END IF;
END;

sql_update := 'create table AUDIT_TABLE (table structure)';
execute immediate 'begin dbms_utility.exec_ddl_statement@'||v_database_name||'(:sql_update);  end;' using sql_update;
commit;

sql_update := q'[
              INSERT INTO AUDIT_TABLE@#DB_NAME# 
              Select {query here}
                ]';
execute immediate replace('begin ' || sql_update || ';  end;','#DB_NAME#', v_database_name);
commit;

sql_update := q'[
              INSERT INTO AUDIT_TABLE select * from AUDIT_TABLE@#DB_NAME#
                ]';
execute immediate replace('begin ' || sql_update || ';  end;','#DB_NAME#', v_database_name);
commit;