I want to update a field based on another table field. The table is like this:
TABLE A
SID SMONTH STID VID VVID
1 201312 s10 v5 ?
2 201312 s10 v5 ?
1 201312 s11 v7 ?
2 201401 s11 v7 ?
1 201312 s1 v9 ?
2 201401 s1 v9 ?
1 201312 s1 v60 ?
1 201312 s1 v71 ?
In the above table A , i need to update VVID column with the VVID from the below table B.
TABLE B
VVID STID VID WEIGHT
v1 s10 v5 0.5
v2 s10 v5 7.5
v1 s11 v7 1.5
v2 s11 v7 6.5
v1 s1 v9 5
v2 s1 v9 5
v1 s1 v60 5
v1 s1 v71 5
In the above table B , VVID is generated based on three fields STID , VID and WEIGHT . But in the table A , i dont have WEIGHT field. So if i use the below code i am getting "single row query returns more than one row" error.
UPDATE A
SET VVID = (SELECT distinct VVID
FROM B
WHERE B.STID = A.STID and B.VID = A.VID )
Please give me suggestions.
Thanks Sathish
Here your inner query is returning more than one row as the additional criteria for comparing the weight is missing so for example STID = s10 and VID = v5 the inner query will return two rows with VVID = v1 and VVID = v2. The update clause requires a single value after SET.
You can modify your query to update the max or min of VVID if it is acceptable for your application
UPDATE A SET VVID = (SELECT MIN(VVID) FROM B WHERE B.STID = A.STID and B.VID = A.VID)
or
UPDATE A SET VVID = (SELECT MAX(VVID) FROM B WHERE B.STID = A.STID and B.VID = A.VID)
I think you can update with an INNER JOIN
query
UPDATE A
JOIN B
ON B.STID = A.STID
AND B.VID = A.VID
SET A.VVID = B.VVID
Please use union with constant value instead of join. For details please refer to the post http://scn.sap.com/docs/DOC-40619
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.