简体   繁体   中英

Oracle SQL | Single-row Subquery Update returns more than one row?

this question has been asked a lot it seems, but I can't find any dealing with updates and the ones dealing with other stuff haven't helped. I feel like I'm missing something obvious but I can't put my finger on it.

I have the following query to update my table with IDs from another table to enable matching to a spreadsheet:

update TABLE3 set ITM_CD2 = 
(select pi.ITM_CD2
   from schema1.PI_TABLE pi,
        TABLE3 tb3
  where pi.OTHER_ITM_CD = tb3.OTHER_ITM_CD)

I can't actually go through with the update because I keep getting "needs a single-row subquery" issues.

EDIT: I should have mentioned that the pi table is from a separate schema.

EDIT 2: For more detail; this is an example of what I'm trying to obtain:

TABLE 3 currently has this data, for instance:

NAME ----- PRODUCT ----- ITM_CD1 ----- ITM_CD2
X          Y             11            NULL
A          B             12            NULL
C          Y             11            NULL

I'm trying to attach data from this item table so I can get the 2nd itm_cd which will allow me to compare it to a table that has ITM_CD2 but NOT ITM_CD1. The NULLs in TABLE 3 would be replaced with the matching ITM_CD2.

The table I'm trying to take the ITM_CD2 from would look like this:

PRODUCT ----- ITM_CD1 ----- ITM_CD2
A             10            90
Y             11            98
B             12            87

Perhaps you want the main TABLE3 in the sub-query:

update TABLE3 set ITM_CD2 = 
(select pi.ITM_CD2
   from PI_TABLE pi
  where pi.OTHER_ITM_CD = TABLE3.OTHER_ITM_CD)

I like using a merge rather than update for this kind of scenario. It makes the connection between the table you are reading from and the table you are writing to more clear:

MERGE INTO table3 t3
USING      pi_table pi
ON         (pi.other_itm_cd = t3.other_itm_cd)
WHEN MATCHED THEN
   UPDATE SET t3.itm_cd2 = pi.itm_cd2 

Try this query

 UPDATE ( SELECT t1.Item_CD2,pi.ITM_CD2
          FROM table3 t1
          join pi_table pi on pi.OTHER_ITM_CD = t1.OTHER_ITM_CD
         )
 SET t1.Item_CD2=pi.ITM_CD2

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM