I have this SQL statement to update column aisle from table itemloc_tmp
from aisle from table itemloc_tbl
in SQLPLUS, and it keeps returning the error like in the topic.
Update: This is sample data from itemloc_tmp
ITEM_ID SID SEC AIS
------------- --- --- ---
0007AAAAAAAAA AA3 12
0007BBBBBBBBB BB2 13
0007CCCCCCCCC CC8 11
This is sample from itemloc_tbl
ITEM_ID SID SEC AIS
------------- --- --- ---
0007AAAAAAAAA
0007BBBBBBBBB
0007CCCCCCCCC
This is the my sql statement
UPDATE ct.itemloc_tbl t1 SET
t1.aisle = (SELECT t2.aisle FROM ct.itemloc_tmp t2 WHERE t2.item_id = t1.item_id)
WHERE t1.item_id IN (SELECT t2.item_id FROM ct.itemloc_tmp t2 WHERE t2.item_id = t1.item_id)
So, I want to update aisle
column from itemloc_tmp
to itemloc_tbl
, but the statement above does not work. Would anyone point out where I was wrong?
You can limit the rows being updated. Here are two ways. The first uses rownum = 1
:
UPDATE ct.itemloc_tbl t1 SET
SET t1.aisle = (SELECT t2.aisle FROM ct.itemloc_tmp t2 WHERE t2.item_id = t1.item_id AND rownum = 1)
WHERE EXISTS (SELECT 1 FROM ct.itemloc_tmp t2 WHERE t2.item_id = t1.item_id);
The second uses an aggregation function:
UPDATE ct.itemloc_tbl t1 SET
SET t1.aisle = (SELECT MAX(t2.aisle) FROM ct.itemloc_tmp t2 WHERE t2.item_id = t1.item_id)
WHERE EXISTS (SELECT 1 FROM ct.itemloc_tmp t2 WHERE t2.item_id = t1.item_id);
Either of these fix the proximal cause of the error, which is the subquery returning more than one row. However, you need to decide what you really want for the assignment. If an "arbitrary" value is fine, then these work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.