刚开始用 update table set ()=(…… where ……);(…… where ……) 的后面没有再加加 where ……,结果把 table 不该修改的记录的相应字段也刷成了 null;写成 update table set ()=(…… where ……) where (a1, b1) in (select x, y from t1, t2 where……)又担心如果 xy 组合不唯一的话,会不会有其它问题,或者这种 update 写法有其它要注意的地方。下面是我在 Oracle Erp 里面写的,用到了这种update,请大侠有空帮忙指正一下,谢谢。
--最近成本 tLast====================================================================
UPDATE SJH_CST_ITEM_COSTS SJH SET(/*,*/
MATERIAL_COST,
MATERIAL_OVERHEAD_COST,
OUTSIDE_PROCESSING_COST,
OVERHEAD_COST
,LAST_UPDATED_BY, LAST_UPDATE_DATE
) = (
select
--nvl( tNow.poNowCst, cst.MATERIAL_COST ) 现存成本之物料成本,
nvl(poLastCst, cst.MATERIAL_COST) 最近成本之物料成本,
--nvl(poPreCst, cst.MATERIAL_COST) 预期成本之物料成本,
cst.material_overhead_cost 物料附加成本,
cst.outside_processing_cost 外协费用,
cst.overhead_cost 制造费用
,user_id, sysdate
from
CST_COST_TYPES cType
inner join
cst_quantity_layers cst on cst.Organization_Id = cType.Organization_Id
left join (
select t2.organization_id, t2.Item_Id, rcv21.po_unit_price poLastCst
from rcv_transactions rcv21
inner join po_lines_all poLine21 on rcv21.po_line_id = poLine21.Po_Line_Id
inner join (
select rcv22.organization_id, poLine22.Item_Id, max(rcv22.creation_date) lastMaxCreateDate
from rcv_transactions rcv22
inner join po_lines_all poLine22 on rcv22.po_line_id = poLine22.Po_Line_Id
where trunc( sysdate, 'DD') - trunc(rcv22.creation_date, 'DD') <= 30
group by rcv22.organization_id, poLine22.Item_Id
) t2 on rcv21.organization_id = t2.organization_id and poLine21.Item_Id = t2.Item_Id and rcv21.creation_date = t2.lastMaxCreateDate
) tLast on tLast.organization_id = cst.organization_id and tLast.Item_Id = cst.inventory_item_id
where cType.DESCRIPTION = '最近成本'
and SJH.ORGANIZATION_ID = cst.organization_id
and SJH.INVENTORY_ITEM_ID = cst.inventory_item_id
and SJH.COST_TYPE_ID = cType.Cost_Type_Id
)
where (SJH.ORGANIZATION_ID, SJH.INVENTORY_ITEM_ID, COST_TYPE_ID) IN (
SELECT A.organization_id, A.inventory_item_id, B.COST_TYPE_ID
FROM cst_quantity_layers A, CST_COST_TYPES B
WHERE A.organization_id=B.organization_id AND B.DESCRIPTION = '最近成本')
;
--最近成本 tLast====================================================================
UPDATE SJH_CST_ITEM_COSTS SJH SET(/*,*/
MATERIAL_COST,
MATERIAL_OVERHEAD_COST,
OUTSIDE_PROCESSING_COST,
OVERHEAD_COST
,LAST_UPDATED_BY, LAST_UPDATE_DATE
) = (
select
--nvl( tNow.poNowCst, cst.MATERIAL_COST ) 现存成本之物料成本,
nvl(poLastCst, cst.MATERIAL_COST) 最近成本之物料成本,
--nvl(poPreCst, cst.MATERIAL_COST) 预期成本之物料成本,
cst.material_overhead_cost 物料附加成本,
cst.outside_processing_cost 外协费用,
cst.overhead_cost 制造费用
,user_id, sysdate
from
CST_COST_TYPES cType
inner join
cst_quantity_layers cst on cst.Organization_Id = cType.Organization_Id
left join (
select t2.organization_id, t2.Item_Id, rcv21.po_unit_price poLastCst
from rcv_transactions rcv21
inner join po_lines_all poLine21 on rcv21.po_line_id = poLine21.Po_Line_Id
inner join (
select rcv22.organization_id, poLine22.Item_Id, max(rcv22.creation_date) lastMaxCreateDate
from rcv_transactions rcv22
inner join po_lines_all poLine22 on rcv22.po_line_id = poLine22.Po_Line_Id
where trunc( sysdate, 'DD') - trunc(rcv22.creation_date, 'DD') <= 30
group by rcv22.organization_id, poLine22.Item_Id
) t2 on rcv21.organization_id = t2.organization_id and poLine21.Item_Id = t2.Item_Id and rcv21.creation_date = t2.lastMaxCreateDate
) tLast on tLast.organization_id = cst.organization_id and tLast.Item_Id = cst.inventory_item_id
where cType.DESCRIPTION = '最近成本'
and SJH.ORGANIZATION_ID = cst.organization_id
and SJH.INVENTORY_ITEM_ID = cst.inventory_item_id
and SJH.COST_TYPE_ID = cType.Cost_Type_Id
)
where (SJH.ORGANIZATION_ID, SJH.INVENTORY_ITEM_ID, COST_TYPE_ID) IN (
SELECT A.organization_id, A.inventory_item_id, B.COST_TYPE_ID
FROM cst_quantity_layers A, CST_COST_TYPES B
WHERE A.organization_id=B.organization_id AND B.DESCRIPTION = '最近成本')
;
你可以在pl/sql中先看看(…… where ……)的执行结果啊.
这样就清楚些.