我无法将选择查询转换为更新语句。
SELECT (SELECT DECODE(building_1, 'No', '0', building_1) +
COALESCE(building_2, '0') + COALESCE(Building_3, '0') +
COALESCE(Building_4, '0') + COALESCE(Building_5, '0') +
COALESCE(Building_6, '0') + COALESCE(Building_7, '0')
FROM tb2
WHERE marsha_CODE = SUBSTR(OMT.PROPERTY_NAME, 1, 5)) ROOMS
FROM tb1 ALR, tb3 OMT
WHERE 1 = 1
and work_Order_id = OMT.Request_id
AND ADDRESS = OMT.PROPERTY_ADDRESS
我需要在select语句的下面部分是update语句中的设置值
( SELECT DECODE(building_1, 'No', '0', building_1) +
COALESCE(building_2, '0') + COALESCE(Building_3, '0') +
COALESCE(Building_4, '0') + COALESCE(Building_5, '0') +
COALESCE(Building_6, '0') + COALESCE(Building_7, '0')
FROM tb2
WHERE marsha_CODE = SUBSTR(OMT.PROPERTY_NAME, 1, 5) )
以及update语句where条件的where子句
WHERE 1=1
AND work_Order_id = OMT.Request_id
AND ADDRESS = OMT.PROPERTY_ADDRESS
以下是我尝试过的内容:
UPDATE tb1
SET room_number =
(SELECT DECODE(building_1, 'No', '0', building_1)
+ COALESCE (building_2, '0')
+ COALESCE (Building_3, '0')
+ COALESCE (Building_4, '0')
+ COALESCE (Building_5, '0')
+ COALESCE (Building_6, '0')
+ COALESCE (Building_7, '0')
FROM tb2,
tb3 OMT
WHERE marsha_CODE = SUBSTR(OMT.PROPERTY_NAME, 1, 5))
WHERE work_Order_id =
(SELECT Request_id
FROM tb3
WHERE tb3.request_id =
tb1.WORK_ORDER_ID)
AND address =
(SELECT PROPERTY_ADDRESS
FROM tb3
WHERE tb3.PROPERTY_ADDRESS =
tb1.ADDRESS)
答案 0 :(得分:1)
您可以使用包含with .. as
子句的Update语句:
UPDATE tb1 t
SET room_number =
(
WITH tt as
(
SELECT DECODE(building_1, 'No', '0', building_1) +
COALESCE(building_2, '0') + COALESCE(Building_3, '0') +
COALESCE(Building_4, '0') + COALESCE(Building_5, '0') +
COALESCE(Building_6, '0') + COALESCE(Building_7, '0') as r_number,
t3.request_id,
t3.property_address
FROM tb2 t2
JOIN tb3 t3
ON t2.marsha_code = SUBSTR(t3.property_name, 1, 5)
)
SELECT tt.r_number
FROM tt
WHERE tt.request_id = t.work_order_id
AND tt.property_address = t.address
)
甚至可以使用Merge语句:
MERGE INTO tb1 t
USING
(
SELECT DECODE(building_1, 'No', '0', building_1) +
COALESCE(building_2, '0') + COALESCE(Building_3, '0') +
COALESCE(Building_4, '0') + COALESCE(Building_5, '0') +
COALESCE(Building_6, '0') + COALESCE(Building_7, '0') as r_number,
omt.request_id,
omt.property_address
FROM tb2 t2
JOIN tb3 t3
ON t2.marsha_code = SUBSTR(t3.property_name, 1, 5)
) tt
ON (tt.request_id = t.work_order_id AND tt.property_address = t.address)
WHEN MATCHED THEN SET t.room_number = tt.r_number;