本文介绍了在将分组基础资料转换为树形基础资料的过程中,涉及的数据迁移问题。首先,需要创建新的树形基础资料表,并理解两者表结构的差异。其次,通过SQL语句实现数据迁移,包括将分组基础资料的数据迁移至树形基础资料表中,并处理树形基础资料特有的字段如长编码、节点层级、是否为叶子节点、父节点ID等。同时,还涉及到多语言表和审核状态记录表的转换。最后,通过Insert语句将转换后的数据插入到新的树形基础资料表中。
背景说明
分组基础资料使用一段时间后,要改成树形基础资料。这个时候,面临2个问题:
分组基础资料已经录入了不少数据。
单据引用了这个分组基础资料。
在这个情况下,要将分组基础资料调整为树形基础资料,可以简单分为3步:
创建新的树形基础资料;
将分组基础资料数据迁移到树形基础资料中;
将单据中分组基础资料的引用修改为树形基础资的引用。
创建新的树形基础资料,和修改基础资料引用属于基本操作,不多做介绍,本文主要介绍如何使用SQL迁移数据。
表结构
本地创建一个分组基础资料和树形分组资料,如下图:
分组基础资料 ↑ 树形基础资料 ↓
创建后基础资料后,发现数据库里多了9个表:
分组基础资料左侧 | 分组基础资料右侧 | 树形分组资料 | |
数据表 | tk_gkk5_test01_basegroup | tk_gkk5_test01_base | tk_gkk5_test01_tree |
多语言表 | tk_gkk5_test01_basegroup_l | tk_gkk5_test01_base_l | tk_gkk5_test01_tree_l |
记录已审核的数据 | tk_gkk5_test01_basegroup_r3 | tk_gkk5_test01_base_r3 | tk_gkk5_test01_tree_r3 |
具体的表结构可以自行创建基础资料后查看。在这里直接说明,树形基础资料的表与前2者差别在于多了几个字段:
flongnumber, flevel, fisleaf, fparentid,对应界面中的这4个字段。在界面中还多了长名称这个字段,位于tk_gkk5_test01_tree_l的ffullname字段。
SQL思路
分组和树形两者的表结构差不多,唯一区别是树形拥有独有的字段。所以迁移数据时,同名字段的值直接拷贝过来即可,同时fid一致,单据修改引用后就可以直接使用。对于独有的字段,需要了解其业务含义才能写出处理逻辑。
flongnumber:长编码规则是[父节点编码+.]+自身编码
flevel:节点层级,从1计起
fisleaf:是否为叶子节点
fparentid:父节点ID,如果没有父节点则为0
长名称:存储在tk_gkk5_test01_tree_l表中,只有中文下才有值。
树形分组表转换
对于分组基础资料左侧,必定是第一层节点,但它可能没有子节点,所以数据可以这样转换:
-- 将基础资料的分组转为树形基础资料 select bgroup.fid, bgroup.fnumber, bgroup.fname, bgroup.fstatus, bgroup.fcreatorid, bgroup.fmodifierid, bgroup.fenable, bgroup.fcreatetime, bgroup.fmodifytime, bgroup.fmasterid, bgroup.fnumber as flongnumber, 1 as flevel, case when sum(base.fid) is null then 1 else 0 end as fisleaf, 0 as fparentid from tk_gkk5_test01_basegroup bgroup left join tk_gkk5_test01_base base on bgroup.fid=base.fgroupid group by bgroup.fid, bgroup.fnumber, bgroup.fname, bgroup.fstatus, bgroup.fcreatorid, bgroup.fmodifierid, bgroup.fenable, bgroup.fcreatetime, bgroup.fmodifytime, bgroup.fmasterid ;
对于分组基础资料右侧,一般是第二层节点,但也可能没有父节点,从而成为第一层节点,所以可以这样转换:
select base.fid, base.fnumber, base.fname, base.fstatus, base.fcreatorid, base.fmodifierid, base.fenable, base.fcreatetime, base.fmodifytime, base.fmasterid, case when bgroup.fid is null then base.fnumber else bgroup.fnumber || '.' || base.fnumber end as flongnumber, case when bgroup.fid is null then 1 else 2 end as flevel, 1 as fisleaf, case when bgroup.fid is null then 0 else bgroup.fid end as fparentid from tk_gkk5_test01_base base left join tk_gkk5_test01_basegroup bgroup on bgroup.fid=base.fgroupid ;
树形基础资料l表转换
转换分组基础资料左侧
select fpkid, fid, flocaleid, fname, fname as ffullname from tk_gkk5_test01_basegroup_l bgroupl
转换分组基础资料右侧
select basel.fpkid, basel.fid, basel.flocaleid, basel.fname, case when base.fgroupid = 0 then basel.fname else bgroupl.fname || '.' || basel.fname end ffullname from tk_gkk5_test01_base_l basel inner join tk_gkk5_test01_base base on base.fid=basel.fid left join tk_gkk5_test01_basegroup_l bgroupl on bgroupl.fid=base.fgroupid and basel.flocaleid=bgroupl.flocaleid
树形基础资料r3表转换
原样转换即可。
转换分组基础资料左侧
select fid, frefstatus from tk_gkk5_test01_basegro_r3 bgroupr3
转换分组基础资料右侧
select fid, frefstatus from tk_gkk5_test01_base_r3 baser3
SQL转换
我们已经获取了转换的Select语句,只需要简单添加Insert部分就可以执行。
数据表
insert into tk_gkk5_test01_tree (fid, fnumber, fname, fstatus, fcreatorid, fmodifierid, fenable, fcreatetime, fmodifytime, fmasterid, flongnumber, flevel, fisleaf, fparentid) select bgroup.fid, bgroup.fnumber, bgroup.fname, bgroup.fstatus, bgroup.fcreatorid, bgroup.fmodifierid, bgroup.fenable, bgroup.fcreatetime, bgroup.fmodifytime, bgroup.fmasterid, bgroup.fnumber as flongnumber, 1 as flevel, case when sum(base.fid) is null then 1 else 0 end as fisleaf, 0 as fparentid from tk_gkk5_test01_basegroup bgroup left join tk_gkk5_test01_base base on bgroup.fid=base.fgroupid group by bgroup.fid, bgroup.fnumber, bgroup.fname, bgroup.fstatus, bgroup.fcreatorid, bgroup.fmodifierid, bgroup.fenable, bgroup.fcreatetime, bgroup.fmodifytime, bgroup.fmasterid ; insert into tk_gkk5_test01_tree (fid, fnumber, fname, fstatus, fcreatorid, fmodifierid, fenable, fcreatetime, fmodifytime, fmasterid, flongnumber, flevel, fisleaf, fparentid) select base.fid, base.fnumber, base.fname, base.fstatus, base.fcreatorid, base.fmodifierid, base.fenable, base.fcreatetime, base.fmodifytime, base.fmasterid, case when bgroup.fid is null then base.fnumber else bgroup.fnumber || '.' || base.fnumber end as flongnumber, case when bgroup.fid is null then 1 else 2 end as flevel, 1 as fisleaf, case when bgroup.fid is null then 0 else bgroup.fid end as fparentid from tk_gkk5_test01_base base left join tk_gkk5_test01_basegroup bgroup on bgroup.fid=base.fgroupid ;
l表
insert into tk_gkk5_test01_tree_l(fpkid, fid, flocaleid, fname, ffullname) select fpkid, fid, flocaleid, fname, ffullname from ( select fpkid, fid, flocaleid, fname, fname as ffullname from tk_gkk5_test01_basegroup_l bgroupl union all select basel.fpkid, basel.fid, basel.flocaleid, basel.fname, case when base.fgroupid = 0 then basel.fname else bgroupl.fname || '.' || basel.fname end ffullname from tk_gkk5_test01_base_l basel inner join tk_gkk5_test01_base base on base.fid=basel.fid left join tk_gkk5_test01_basegroup_l bgroupl on bgroupl.fid=base.fgroupid and basel.flocaleid=bgroupl.flocaleid ) mergel
r3表
insert into tk_gkk5_test01_tree_r3 (fid, frefstatus) select fid, frefstatus from ( select fid, frefstatus from tk_gkk5_test01_basegro_r3 bgroupr3 union all select fid, frefstatus from tk_gkk5_test01_base_r3 baser3 ) merger3
推荐阅读