|
|
@@ -0,0 +1,40 @@
|
|
|
+-- =============================================================================
|
|
|
+-- S0-STANDARD-BOM-MATERIAL-FK-BACKFILL-EXECUTION-1
|
|
|
+-- 目的:回填 ProductStructureMaster legacy 行(2026-05-27/28 一次性导入只写父/子项编码、
|
|
|
+-- 未写后加的 FK 列)的 ParentMaterialId / ComponentMaterialId,按 工厂 + 租户 限定
|
|
|
+-- 1:1 关联 ItemMaster.RecID(ItemMaster 唯一索引 uk_ItemMaster_factory_item=(factory_ref_id,ItemNum)
|
|
|
+-- 保证工厂内编码唯一 → 无多匹配)。
|
|
|
+-- 范围:仅 UPDATE FK IS NULL 的行;只写 FK,不改 ParentItem/ComponentItem 编码、不改 ItemMaster、
|
|
|
+-- 不改状态字段(IsActive/IsEnabled)、不删行;不覆盖已有非 NULL FK;不更新 demo RecID=1
|
|
|
+-- (其 FK=0 非 NULL,IS NULL 不命中,且 RecID<>1 显式排除)。
|
|
|
+-- 安全:工厂限定(p.FactoryRefId=im.factory_ref_id) + 租户限定(p.tenant_id=im.tenant_id);
|
|
|
+-- 阶段 0 只读校验:父/子各 53031 行可回填,多匹配 0,匹配不到 0,跨工厂/跨租户 0。
|
|
|
+-- 幂等:WHERE FK IS NULL → 重跑只命中剩余 NULL,已回填行不再变动。
|
|
|
+-- 回滚(如需,人工执行,非本脚本自动):
|
|
|
+-- UPDATE ProductStructureMaster SET ParentMaterialId=NULL, ComponentMaterialId=NULL
|
|
|
+-- WHERE RecID<>1 AND CreatedAt BETWEEN '2026-05-27' AND '2026-05-28 23:59:59';
|
|
|
+-- =============================================================================
|
|
|
+
|
|
|
+-- 段 1:回填父项 ParentMaterialId
|
|
|
+UPDATE ProductStructureMaster p
|
|
|
+JOIN ItemMaster im
|
|
|
+ ON im.ItemNum = p.ParentItem
|
|
|
+ AND im.factory_ref_id = p.FactoryRefId
|
|
|
+ AND im.tenant_id = p.tenant_id
|
|
|
+SET p.ParentMaterialId = im.RecID
|
|
|
+WHERE p.ParentMaterialId IS NULL
|
|
|
+ AND p.ParentItem IS NOT NULL
|
|
|
+ AND p.ParentItem <> ''
|
|
|
+ AND p.RecID <> 1;
|
|
|
+
|
|
|
+-- 段 2:回填子项 ComponentMaterialId
|
|
|
+UPDATE ProductStructureMaster p
|
|
|
+JOIN ItemMaster im
|
|
|
+ ON im.ItemNum = p.ComponentItem
|
|
|
+ AND im.factory_ref_id = p.FactoryRefId
|
|
|
+ AND im.tenant_id = p.tenant_id
|
|
|
+SET p.ComponentMaterialId = im.RecID
|
|
|
+WHERE p.ComponentMaterialId IS NULL
|
|
|
+ AND p.ComponentItem IS NOT NULL
|
|
|
+ AND p.ComponentItem <> ''
|
|
|
+ AND p.RecID <> 1;
|