Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UPDATE and EXISTS Clause

Please help me identify what I am doing wrong in the query below :

The following code gives an error: ORA-00904 : DPT.DEPTNO : invalid identifier

UPDATE emp1 
SET ename = (SELECT dname
FROM dpt 
WHERE dpt.deptno = emp1.deptno)
WHERE EXISTS (SELECT ename
FROM emp1 
WHERE emp1.deptno = dpt.deptno);

the first half of the query works fine on its own :

UPDATE emp1 
SET ename = (SELECT dname
FROM dpt 
WHERE dpt.deptno = emp1.deptno)
like image 400
Manushree Mishra Avatar asked Jan 31 '26 02:01

Manushree Mishra


1 Answers

You have the wrong table in the second subquery:

UPDATE emp1 
SET ename = (SELECT dname FROM dpt WHERE dpt.deptno = emp1.deptno)
WHERE EXISTS (SELECT 1 FROM dpt WHERE emp1.deptno = dpt.deptno);
----------------------------^

It is curious that you are setting a column called ename to the name of what is presumably a department. But that is another matter.

like image 93
Gordon Linoff Avatar answered Feb 01 '26 17:02

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!