title: Insert using db_link gets invalid identifier error
body: <p>I'm building a query that locates specific records across multiple databases and inserts them in the master DB that has access to all the rest. </p>
  
  <p>But even if the select works well, the insert fails giving me an invalid identifier using a synonym that doesn't even exist.</p>
  
  <p><strong>Query:</strong></p>
  
  <pre><code>            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 &lt;&gt; 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 &lt;&gt; 31 then 
              case when trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) + l.icloseday &lt;= 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 &lt;&gt; 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 &lt;&gt; 31 then 
                case when trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) + l.icloseday &lt;= 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 &lt;&gt; trunc(tr.UPOSTDATE)
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>            ORA-00904: "A2"."HUSERCREATEDBY": invalid identifier
              ORA-02063: preceding line from db_link
  </code></pre>
  
  <p>The weirdest part: all these database have the same schema but only some of them fail with this error when i loop through all.</p>
  
  <p><strong>Update 11/14/2017:</strong></p>
  
  <p>Looking more into it, it seems that if i eliminate any one of the two following conditions the INSERT has no issue, but i keep looking at them and don't know why the engine would error out because of these two when just the simple SELECT has no issue.</p>
  
  <pre><code>and tr.sdatecreated between to_date(trunc(sysdate, 'YEAR')) and to_date(add_months(trunc(sysdate, 'YEAR'), 12)-1)
  </code></pre>
  
  <p><strong>or</strong></p>
  
  <pre><code>and case when l.icloseday &lt;&gt; 31 then 
                               case when trunc(tr.sdateoccurred ) - (to_number(to_char(tr.sdateoccurred ,'DD')) - 1) + l.icloseday &lt;= 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 &lt;&gt; trunc(tr.UPOSTDATE)
  </code></pre>