"you update value, but you joined on value, so the update would do nothing"
Iam not sure why you say that. May be i was not explaining properly.
CREATE TABLE T(VALUE INTEGER);
CREATE TABLE S(FLD CHAR(1), OLD_VALUE INTEGER, NEW_VALUE INTEGER);
INSERT INTO T VALUES(2);
INSERT INTO T VALUES(3);
INSERT INTO S VALUES('A',1,5);
INSERT INTO S VALUES('A',2,6);
INSERT INTO S VALUES('A',3,7);
COMMIT;
UPDATE T
SET VALUE = (SELECT NEW_VALUE FROM S WHERE VALUE = OLD_VALUE AND FLD = 'A');
SELECT * FROM T;
VALUE
----------
5
6
7
ROLLBACK;
ALTER TABLE S ADD CONSTRAINT UNI UNIQUE(OLD_VALUE);
UPDATE (SELECT VALUE,NEW_VALUE FROM S,T WHERE VALUE = OLD_VALUE AND FLD = 'A')
SET VALUE = NEW_VALUE;
SELECT * FROM T;
VALUE
----------
5
6
7
ROLLBACK;
MERGE INTO T
USING (SELECT * FROM S WHERE FLD = 'A') S
ON (T.VALUE = S.OLD_VALUE)
WHEN MATCHED THEN
UPDATE SET VALUE = OLD_VALUE;
UPDATE allows me to join on VALUE but MERGE dont i get the error.
ERROR at line 3:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "T"."VALUE"
Why is it so..
And again problem with JOIN UPDATE comes when i want to insert
INSERT INTO S VALUES('B',1,5);
I cant do this as i have a unique constraint on OLD_VALUE. And if i remove it then JOIN UPDATE does not work.
So all i have is is only this form of update.
UPDATE T
SET VALUE = (SELECT NEW_VALUE FROM S WHERE VALUE = OLD_VALUE AND FLD = 'A')
WHERE EXISTS(SELECT NEW_VALUE FROM S WHERE VALUE = OLD_VALUE AND FLD = 'A';
I think i have to live with the additional expence of checking for the existance of the data in the source
I also cant use the hint (undocumented means ***danger***)