Solved

Help with a Merge statement in oracle

Posted on 2011-03-08
13
348 Views
Last Modified: 2012-05-11

Hi,
 Below is the merge statement I was using to Update and insert  data into a table. But the problem is  the conditions  in the Update statement are not working. I mean rows are not being updated, but when I remove one of the condition then it works.

MERGE INTO table  a
     USING table b
        ON (b.id = a.id)
WHEN MATCHED
THEN
   UPDATE SET
      a.col1 = b.col1,
      a.col2 = b.col2,
      a.col3 = 13
           WHERE  a.col3 = 6  and
                      b.col4 ='N'
WHEN NOT MATCHED
THEN
   INSERT     (a.col1, a.col2, a.col3)
       VALUES (b.col1, b.col3, 1)
       where b.col4 = 'N'
0
Comment
Question by:new_perl_user
  • 6
  • 5
  • 2
13 Comments
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35070274
Use both conditions separately. You should get the same result!
0
 

Author Comment

by:new_perl_user
ID: 35070364
how can we use them separately, I mean using a seperate update statement. If so then it won't work for me because it has to update only when both conditions are met.
0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35070396
Try this please and let me know the result:
MERGE INTO table  a
     USING table b
        ON (b.id = a.id)
WHEN MATCHED
THEN
   UPDATE SET
      a.col1 = b.col1,
      a.col2 = b.col2,
      a.col3 = 13
           WHERE  a.col3 = 6  and
                      b.col4 ='N'
WHEN NOT MATCHED
THEN
   update set     
   a.col1 = b.col1, 
   a.col2 = b.col3,
   a.col3 = 1
   where b.col4 = 'N'

Open in new window

0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35070430
i believe i might of rushed abit into answering that one!

MERGE INTO table  a
     USING table b
        ON (b.id = a.id)
WHEN MATCHED
THEN
   UPDATE SET
      a.col1 = b.col1,
      a.col2 = b.col2,
      a.col3 = 13
           WHERE  a.col3 = 6  and
                      b.col4 ='N'
WHEN NOT MATCHED
THEN
   INSERT  into a   (a.col1, a.col2, a.col3)
       VALUES (b.col1, b.col3, 1)
       where b.col4 = 'N'
0
 

Author Comment

by:new_perl_user
ID: 35070454
Hi,
 It is saying oracle error ORA-))90% missing keyword near the second update statement.

But when not matched  it should insert because it is a new record.
0
 

Author Comment

by:new_perl_user
ID: 35070458
sorry ORA -00905
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35070481
MERGE INTO table  a
     USING table b
        ON (b.id = a.id)
WHEN MATCHED
THEN
   UPDATE SET
      a.col1 = b.col1,
      a.col2 = b.col2,
      a.col3 = 13
           WHERE  a.col3 = 6  AND
                      b.col4 ='N'
WHEN NOT MATCHED
THEN
   INSERT  VALUES  (b.col1, b.col2, b.col3)
       WHERE b.col4 = 'N'
0
 

Author Comment

by:new_perl_user
ID: 35070533
Insert is working fine, the problem is with  the update conditions

WHERE  a.col3 = 6  AND
                      b.col4 ='N'

Although there are records to be updated  it is not updating.
0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35070591
it will only update the columns where the col3 = 6 and col4 = 'N' i hope you're aware about that.
 I'm working blind here and i don't have two tables to work with right now. I'll get home soon and get back to you as soon as i can.
Hopefully you will find the answer meanwhile.
0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35070600
if possible please provide some data to work with. Thanks.
0
 
LVL 31

Expert Comment

by:awking00
ID: 35071004
See attached.
merge.txt
0
 

Author Comment

by:new_perl_user
ID: 35072342
I tried the above one and it is saying missing keyword at and condition.
0
 
LVL 31

Accepted Solution

by:
awking00 earned 500 total points
ID: 35072921
I forgot that you can't update a column used in the on clause. Try this one.
merge.txt
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

760 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

19 Experts available now in Live!

Get 1:1 Help Now