Solved

Oracle MERGE statement ON clause error

Posted on 2004-08-20
4
653 Views
Last Modified: 2008-01-09
Example code: **************************************

merge into genericitem x
using (select distinct changedocid, changedoctype from effectivity) y
on (x.itemid = y.changedocid and x.itemtypecd = y.changedoctype)
when matched then update set x.subtypecd = null
when not matched then insert (x.itemid, x.itemtypecd, x.subtypecd)
values (y.changedocid, y.changedoctype, null)

(itemid + itemtypecd) = the primaryKey for the genericitem table
(itemid + itemtypecd) = the primaryKey for the genericitem table

Problem: ******************************************

I get the Oracle error:
   ORA-00904: "X"."ITEMTYPECD": invalid identifier

generated from the clause:
    on (x.itemid = y.changedocid and x.itemtypecd = y.changedoctype)  

Question(s): ****************************************

1. Can I use multiple join operators?
2. If so, why am I getting the error?
3. I want to use this Merge statement for many tables that all require multiple join operators in the ON clause.  How can I make this work?

Thanks
MarkHensley
markjhensley@msn.com

0
Comment
Question by:MarkHensley
  • 3
4 Comments
 
LVL 12

Accepted Solution

by:
geotiger earned 500 total points
ID: 11855547

Is itemtypecd somehow related to subtypecd (composite key from subtypecd or there is integrity constraint on it)?  You can not update key that is used in ON clause. This is an undocumented feature .

Please see it more at this link:

http://asktom.oracle.com/pls/ask/f?p=4950:8:6906283133203542202::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:5318183934935,

GT

0
 

Author Comment

by:MarkHensley
ID: 11856373
subtypecd is part of a UK on the genericitem table, the UK is defined as itemid,itemtypecd,subtypecd

There are only three columns in the entire table, so if I can't update any of the three then whant should the syntax of the following be?

when matched then update set x.subtypecd = null

When I try and onit the "when matched" clause it throws an error and there are no other columns to update.
0
 
LVL 12

Expert Comment

by:geotiger
ID: 11856423
You might want to avoid MERGE INTO completely since you do not want to update it when matched. You then use the insert into as the following


Insert into genericitem x
select distinct changedocid, changedoctype, null
from effectivity
where x.itemid != changedocid and x.itemtypecd != changedoctype);

GT
0
 
LVL 12

Expert Comment

by:geotiger
ID: 11856449
I do not know whether this is going to work. You can try it since you have the tables:

merge into genericitem x
using (select distinct changedocid, changedoctype from effectivity) y
on (x.itemid = y.changedocid and x.itemtypecd = y.changedoctype)
when matched then NULL
when not matched then insert (x.itemid, x.itemtypecd, x.subtypecd)
values (y.changedocid, y.changedoctype, null)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now