Skip to content

原生脚本 ​

用法:整段内容复制,修改字典编码、字典明细编码参数即可(目前只支持一行明细字典脚本导出)

sql
-- 字典明细迁移 SQL 生成器 (tb_hr_sys_dictitem)
-- 用法: 填 @dictcode 和 @dictitemcode, 执行后取 generated_sql 在生产执行
-- 设计: 明细 id 雪花 id 直接用; dict_id 用 t1.id 在生产按 dictcode 解析, 规避父节点 id 漂移

-- 开始: 清理变量
SET @dictcode = NULL, @dictitemcode = NULL, @field_value = NULL, @item_value = NULL, @error_msg = NULL;

-- 输入 (字典编码、字典明细编码均为必填)
SET @dictcode = 'MenuExport';
SET @dictitemcode = 'commonProd';

-- INSERT 列部分
SET @field_value = 'INSERT INTO `tb_hr_sys_dictitem` (`id`, `dict_id`, `lineid`, `modify_time`, `create_time`, `branch_id`, `dictcode`, `dictitemcode`, `memorycode`, `is_deleted`, `revision`, `guidstring`, `originalguidstring`, `note`, `dictitemname`, `branchname`, `levelno`) \n';

-- 值列表
SET @item_value = (
  SELECT CONCAT_WS(', ',
           IFNULL(di.id, 'NULL'),             -- 雪花 id 直接用
           't1.id',                           -- dict_id: 生产按 dictcode 解析
           IFNULL(di.lineid, 'NULL'),
           IFNULL(QUOTE(di.modify_time), 'NULL'),
           IFNULL(QUOTE(di.create_time), 'NULL'),
           IFNULL(QUOTE(di.branch_id), 'NULL'),
           IFNULL(QUOTE(di.dictcode), 'NULL'),
           IFNULL(QUOTE(di.dictitemcode), 'NULL'),
           IFNULL(QUOTE(di.memorycode), 'NULL'),
           '0',
           IFNULL(di.revision, 'NULL'),
           IFNULL(QUOTE(di.guidstring), 'NULL'),
           IFNULL(QUOTE(di.originalguidstring), 'NULL'),
           IFNULL(QUOTE(di.note), 'NULL'),
           IFNULL(QUOTE(di.dictitemname), 'NULL'),
           IFNULL(QUOTE(di.branchname), 'NULL'),
           IFNULL(QUOTE(di.levelno), 'NULL')  -- levelno 为 varchar, 需加引号
         )
  FROM tb_hr_sys_dictitem di
  WHERE di.dictcode = @dictcode
    AND di.dictitemcode = @dictitemcode
		-- 可能功能上有点问题,这里有的数据[is_deleted]没值
    AND (di.is_deleted = 0 OR di.is_deleted is null)
);

-- 错误校验
SET @error_msg = '';
SET @error_msg = IF(@dictcode IS NULL OR @dictcode = '', CONCAT(@error_msg, '字典头code未填;'), @error_msg);
SET @error_msg = IF(@dictitemcode IS NULL OR @dictitemcode = '', CONCAT(@error_msg, '明细code未填;'), @error_msg);
SET @error_msg = IF((@item_value IS NULL OR @item_value = ''), CONCAT(@error_msg, '未能查询到对应字典明细;'), @error_msg);

-- 输出: 完整拼接放最终 SELECT 并整体 CAST AS CHAR
SELECT
    IF(@error_msg != '', @error_msg,
       CAST(
               CONCAT(@field_value, 'SELECT ', @item_value,
                      '\nFROM tb_hr_sys_dict t1 WHERE t1.dictcode = ', QUOTE(@dictcode), ';')
           AS CHAR)
    ) AS generated_sql
FROM DUAL;

-- 结束: 清理变量
SET @dictcode = NULL, @dictitemcode = NULL, @field_value = NULL, @item_value = NULL, @error_msg = NULL;

页脚:版权前显示的信息