Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to update/merge a table from multiple joins

2767110Jul 2 2015 — edited Jul 7 2015

Hi,

I got a problem updating a table with a value coming from two other joined tables.  It looks like if I enter a value for an employee number, it does update it, but when I assign a field name, it gives me an error, I guess because all of my tables contain multiple records for the same employee.  The following works:

MERGE INTO table1 a

USING

     (SELECT b.employee as EMPNO, numofdays as NEWDAYS

      FROM table2 b, table3 c

      WHERE b.employee = c.employee

      AND b.sequence = c.sequence

      AND b.year = '2015') d

      ON (a.employee = d.EMPNO)

     WHEN MATCHED THEN

           UPDATE SET a.days = NEWDAYS

      WHERE school_year = '2014-2015'

      and a.employee = 123456;

The following does not work, and the only difference is that I'm assigning a variable name instead of the actual value.  I also tried it without specifying "and a.employee = EMPNO;", as I'm already linking table1 to the select on the statement "ON (a.employee = d.EMPNO)".

MERGE INTO table1 a

USING

     (SELECT b.employee as EMPNO, numofdays as NEWDAYS

      FROM table2 b, table3 c

      WHERE b.employee = c.employee

      AND b.sequence = c.sequence

      AND b.year = '2015') d

      ON (a.employee = d.EMPNO)

     WHEN MATCHED THEN

      UPDATE SET a.days = NEWDAYS

      WHERE school_year = '2014-2015'

      and a.employee = EMPNO;

I tried the UPDATE unsuccessfully as well.

How else could I accomplish this?

Thank you.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 4 2015
Added on Jul 2 2015
2 comments
912 views