You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle SQL:如何在同一列中批量替换指定字段值?

批量处理工厂数据替换方案

1. 新增对应行的actual_year值

假设数据表名为factory_data,可以通过映射关系批量插入目标工厂的对应数据,无需逐行操作:

-- 定义工厂映射对,可按需扩展更多条目
WITH plant_mappings AS (
    SELECT '1234' AS source_plant, '1235' AS target_plant UNION ALL
    SELECT '9876' AS source_plant, '5432' AS target_plant
)
INSERT INTO factory_data (plant, actual_year, [其他需同步列])
SELECT pm.target_plant, fd.actual_year, fd.[其他需同步列]
FROM factory_data fd
JOIN plant_mappings pm ON fd.plant = pm.target_plant;

注:将[其他需同步列]替换为表中实际需要保留的列名,多列用逗号分隔;若仅需同步plant和actual_year,可直接删除该部分。

2. 批量替换plant列值

同样借助映射关系一次性完成所有工厂代码替换:

WITH plant_mappings AS (
    SELECT '1234' AS old_plant, '1235' AS new_plant UNION ALL
    SELECT '9876' AS old_plant, '5432' AS new_plant
)
UPDATE fd
SET fd.plant = pm.new_plant
FROM factory_data fd
JOIN plant_mappings pm ON fd.plant = pm.old_plant;

3. 扩展使用说明

后续新增工厂替换需求时,仅需在plant_mappings中添加UNION ALL SELECT '原工厂码', '目标工厂码'即可,无需修改核心逻辑,适配大量替换场景。

内容的提问来源于stack exchange,提问作者Vinod

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:24:17