Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: "Merge into" wierdness - solved

Re: "Merge into" wierdness - solved

From: david wendelken <davewendelken_at_earthlink.net>
Date: Fri, 10 Dec 2004 13:01:22 -0800 (PST)
Message-ID: <23031074.1102712482113.JavaMail.root@misspiggy.psp.pas.earthlink.net>


Well, I've made some progress on this...

The source table had duplicate records in it based on the key structure mentioned in the ON clause. Once those duplicate records were removed, the statement worked.

Didn't help that the error message was completely wrong! Hope this helps someone from losing a day's work...

-----Original Message-----
From: david wendelken <davewendelken_at_earthlink.net> Sent: Dec 10, 2004 12:25 PM
To: Oracle-L_at_freelists.org
Subject: "Merge into" wierdness

I'm doing a merge into command where I query a table that holds multiple scenario items in it.

The objective is to transfer the selected scenario items from the "source scenario" to the "destination scenario". Some of those records might have been transferred before, in which case the non-key values are to be updated from the source. If they haven't been transferred before, they get inserted.

The statement worked like a charm.

Then I needed to add another column to the table, and that column needed to be added to the ON clause. I added it and the statement fails - the error message says the column doesn't exist in the table.

"Oracle Error Code = 904

ORA-00904: "L_TO"."UNITTYPEID": invalid identifier "

If I remove the column name from the ON clause, the statement runs.  

I'm flat-out certain I didn't mis-spell the column name or its prefix. (I cut-and-pasted the column name from the source select statement and the prefix from other destination columns, plus I've had another set of eyes double-check.) (You would make be very happy if I'm wrong...)

The source select statement in the merge command can clearly find the column, so it's not like I'm pointing to the wrong version of the table or something...

I've got 7 phrases in the ON clause, if that makes a difference.

Any clues? I don't have access to meta-link where I'm at.

Oracle9i Enterprise Edition Release 9.2.0.5.0 - Production With the Partitioning, OLAP and Oracle Data Mining options JServer Release 9.2.0.5.0 - Production

Here's the table and statement, somewhat simplified. Hopefully it will remain readable.

DESCR laydown

Name                            Null?    Type
------------------------------- -------- ----
LAYDOWNID               NOT NULL NUMBER(10)
ANALYSISVERSIONID  NOT NULL NUMBER(10)
SCENARIOID              NOT NULL NUMBER(10)
PAIID                         NOT NULL NUMBER(10)
ORGID                       NOT NULL NUMBER(10)
STATUSCODEID          NOT NULL NUMBER(10)
ACTIVITYID                NOT NULL NUMBER(10)
QTY                                           NUMBER(6)
ACTIVITYREMARK                      VARCHAR2(100)
ORGIDFROM                               NUMBER(10)
UNITTYPEID               NOT NULL NUMBER(10)

MERGE INTO laydown l_to
USING
  (SELECT l_src_start.*,l_over_under.qty_over_under    FROM laydown l_src_start

           ,(SELECT l_ou_temp.activityid, l_ou_temp.paiid
                        ,l_ou_temp.orgid, l_ou_temp.unittypeid
                        ,SUM(l_ou_temp.qty) qty_over_under 
            FROM ( SELECT l_ou.activityid ,l_ou.paiid 

,l_ou.orgidfrom orgid ,l_ou.unittypeid
,l_ou.qty * -1 qty
FROM laydown l_ou WHERE l_ou.analysisversionid = 1 AND l_ou.scenarioid = 3 AND l_ou.orgidfrom IS NOT NULL UNION ALL SELECT l_ou.activityid ,l_ou.paiid
,l_ou.orgid ,l_ou.unittypeid
,l_ou.qty
FROM laydown l_ou WHERE l_ou.analysisversionid = 1 AND l_ou.laydownid IN (247) ) l_ou_temp GROUP BY 1,2,3,4 ) l_over_under

   WHERE l_src_start.laydownid IN (247)
   AND l_src_start.analysisversionid = 1 
   AND l_src_start.activityid = l_over_under.activityid (+) 
   AND l_src_start.paiid = l_over_under.paiid (+) 
   AND l_src_start.orgid = l_over_under.orgid (+) 
   AND l_src_start.unittypeid = l_over_under.unittypeid (+)
  ) l_src 
ON (       l_src.activityid = l_to.activityid 
      AND l_src.paiid = l_to.paiid 
      AND l_to.orgid = 16 
      AND l_to.scenarioid = 3 
      AND l_to.analysisversionid = 1 
      AND l_to.orgidfrom = l_src.orgid 
      /* barfs on the next line */
     AND l_to.unittypeid = l_src.unittypeid 
    )
WHEN MATCHED THEN
UPDATE
SET
   qty = DECODE(SIGN(l_src.qty_over_under),-1,qty,NVL(qty,0) + NVL(l_src.qty_over_under,0))   ,activityremark = l_src.activityremark   ,unittypeid = l_src.unittypeid
WHEN NOT MATCHED THEN
INSERT
  (laydownid,analysisversionid,scenarioid ,paiid   ,orgid,activityid ,qty
  ,activityremark ,orgidfrom
  )
VALUES
  (laydown_seq.NEXTVAL,1,3 ,l_src.paiid
  ,16,l_src.activityid ,DECODE(SIGN(l_src.qty_over_under),-1,0,l_src.qty_over_under)   ,l_src.activityremark ,l_src.orgid
  )
/
--
http://www.freelists.org/webpage/oracle-l

--
http://www.freelists.org/webpage/oracle-l
Received on Fri Dec 10 2004 - 15:01:41 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US