Hi there,
I've tried searching for the answer to this, but haven't found what I'm looking for, so I would really appreciate any help anyone can offer.
I am trying to produce an update query to update a value in one table (Address) from another table (Pupil) which are joined (many-to-many relationship) by a third table Pupil_Address. I want to update the field Address.LEA_no with the value of Pupil.Responsible_LEA where the value Pupil_Address.Main_indicator is 'M' and the values of Pupil.Responsible_LEA and Address.LEA_No are not equal.
I know how to do this in MS Jet SQL, but I can't implement it in Oracle. I've tried two approaches.
First, I tried this:
which returns the error "ORA-00904: "PUPIL"."RESPONSIBLE_LEA": invalid identifier", which I would expect.
My second approach was:
But this produces "ORA-01427: single-row subquery returns more than one row".
I think I understand why this approach fails, because the sub-query in the set clause itself produces more than 1 row, so the db has no way of determing which of these rows' values should be used in the update. I need to corrolate this sub-query with the sub-query in the where clause of the update-set statement. But I have no idea how to do this!
If anyone could offer me any guidance I'd be very grateful.
Best wishes,
cja
I've tried searching for the answer to this, but haven't found what I'm looking for, so I would really appreciate any help anyone can offer.
I am trying to produce an update query to update a value in one table (Address) from another table (Pupil) which are joined (many-to-many relationship) by a third table Pupil_Address. I want to update the field Address.LEA_no with the value of Pupil.Responsible_LEA where the value Pupil_Address.Main_indicator is 'M' and the values of Pupil.Responsible_LEA and Address.LEA_No are not equal.
I know how to do this in MS Jet SQL, but I can't implement it in Oracle. I've tried two approaches.
First, I tried this:
Code:
update address
set address.LEA_NO = pupil.RESPONSIBLE_LEA
where address.ADDRESS_ID in
(select address.ADDRESS_ID
FROM (Address INNER JOIN pupil_address ON Address.Address_ID = Pupil_Address.Address_ID)
INNER JOIN Pupil ON Pupil_address.Pupil_ID = Pupil.Pupil_ID
where pupil_address.MAIN_ADDRESS_IND = 'M'
and pupil.RESPONSIBLE_LEA <> address.LEA_NO)
which returns the error "ORA-00904: "PUPIL"."RESPONSIBLE_LEA": invalid identifier", which I would expect.
My second approach was:
Code:
update address
set address.LEA_NO =
(select pupil.RESPONSIBLE_LEA
FROM (pupil INNER JOIN pupil_address ON pupil.PUPIL_ID = Pupil_Address.pupil_ID)
INNER JOIN address ON address.ADDRESS_ID = Pupil_address.address_id
where pupil_address.MAIN_ADDRESS_IND = 'M'
and pupil.RESPONSIBLE_LEA <> address.LEA_NO)
where address.ADDRESS_ID in
(select address.ADDRESS_ID
FROM (Address INNER JOIN pupil_address ON Address.Address_ID = Pupil_Address.Address_ID)
INNER JOIN Pupil ON Pupil_address.Pupil_ID = Pupil.Pupil_ID
where pupil_address.MAIN_ADDRESS_IND = 'M'
and pupil.RESPONSIBLE_LEA <> address.LEA_NO)
But this produces "ORA-01427: single-row subquery returns more than one row".
I think I understand why this approach fails, because the sub-query in the set clause itself produces more than 1 row, so the db has no way of determing which of these rows' values should be used in the update. I need to corrolate this sub-query with the sub-query in the where clause of the update-set statement. But I have no idea how to do this!
If anyone could offer me any guidance I'd be very grateful.
Best wishes,
cja