MERGE [message #641978] |
Fri, 28 August 2015 10:31 |
John Watson
Messages: 8931 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
I'm stuck with a simple MERGE statement. The idea is that for every employee whose salary is less than the average salary of his department, I want to update his salary to the average. I can do it like this,
UPDATE emp e
SET sal = (SELECT Avg(sal)
FROM emp f
WHERE f.deptno = e.deptno)
WHERE sal < (SELECT Avg(sal)
FROM emp f
WHERE f.deptno = e.deptno);
but this attempt at using MERGE
merge INTO emp
USING (SELECT deptno,Avg(sal) deptavg
FROM emp
GROUP BY deptno) dept
ON (emp.deptno=dept.deptno)
WHEN matched THEN
UPDATE SET emp.sal = dept.deptavg WHERE emp.sal < dept.deptavg;
errors out:
orclz>
orclz> merge INTO emp
2 USING (SELECT deptno,Avg(sal) deptavg
3 FROM emp
4 GROUP BY deptno) dept
5 ON (emp.deptno=dept.deptno)
6 WHEN matched THEN
7 UPDATE SET emp.sal = dept.deptavg WHERE emp.sal < dept.deptavg;
UPDATE SET emp.sal = dept.deptavg WHERE emp.sal < dept.deptavg
*
ERROR at line 7:
ORA-00904: "EMP"."SAL": invalid identifier
orclz>
What am I doing wrong? Surely something idiotic?
|
|
|
|
Re: MERGE [message #641988 is a reply to message #641983] |
Fri, 28 August 2015 13:59 |
John Watson
Messages: 8931 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
Thankyou for doing the test. I've re-run utlsampl.sql and now it runs fine in my 12.1.0.2. I must have done something bizarre to my EMP table.
|
|
|