"Merge into" wierdness

  • From: david wendelken <davewendelken@xxxxxxxxxxxxx>
  • To: Oracle-L@xxxxxxxxxxxxx
  • Date: Fri, 10 Dec 2004 12:25:54 -0800 (PST)

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 
  )
/


--
//www.freelists.org/webpage/oracle-l

Other related posts: