-- 转卡开单品项 yjsj 历史回填（品项开单明细按 yjsj 筛时间，空值会导致转卡明细查不到）
-- 口径：yjsj 为空时，用开单主表 kdrq 回填

-- 预览影响行数
SELECT COUNT(*) AS will_update
FROM lq_kd_pxmx px
INNER JOIN lq_kd_kdjlb k ON px.glkdbh = k.F_Id
WHERE px.yjsj IS NULL
  AND k.kdrq IS NOT NULL
  AND px.F_IsEffective = 1
  AND k.F_IsEffective = 1
  AND k.fkfs = '转卡';

-- 执行回填
UPDATE lq_kd_pxmx px
INNER JOIN lq_kd_kdjlb k ON px.glkdbh = k.F_Id
SET px.yjsj = k.kdrq
WHERE px.yjsj IS NULL
  AND k.kdrq IS NOT NULL
  AND px.F_IsEffective = 1
  AND k.F_IsEffective = 1
  AND k.fkfs = '转卡';
