I’m trying to update a table with the multiple returns of a function.
I have created a TYPE
CREATE OR REPLACE TYPE city_state AS OBJECT
(
city VARCHAR2(30),
state VARCHAR2(2)
);
/
and I have a function that returns a variable of this type.
CREATE OR REPLACE FUNCTION closestcity(lat IN NUMBER, lon IN NUMBER) RETURN city_state IS
...
I need to update a table that contains city, state, latitude and longitude columns. With the lat/lon, I should call the function and use the result to update the value of the city/state. I only want to call the function once for each row and there are only some rows that I need to update (let’s say city is NULL)
This is what I got so far
UPDATE (SELECT * FROM t t1 WHERE city IS NULL)
SET (city, state) = (
SELECT newcity.city, newcity.state FROM
( SELECT closestcity(latitude, longitude) newcity
FROM t t2
WHERE t1.latitude = t2.latitude AND
t1.longitude = t2.longitude)
);
but I get an invalid identifir error. I feel like I’m overcomplicating it, what would be the correct approach to this?
The reason you’ve got that error is because Oracle attempts to qualify
newcityreferenced in theSELECT newcity.city, newcity.stateselect statement. It cannot find the objectnewcityand thus throws the error. Also alias the inline view ast1if you really need it. As @Rene has mentioned, you can successfully replace it with the table name. To that end you may rewrite your update statement as follows:Or simply:
Update#1