mysql利用存储过程 同实例数据库迁移数据

网上有很多利用存储过程迁移数据,很多是利用游标添加数据 但是 使用游标添加数据 ,会产生数据丢失的问题,,所以在在网上找了相关方法 把相关数据导入到临时表中,我这里是导入到视图中

mysql提供了三张插入的方式:

类别一、 如果两张张表(导出表和目标表)的字段一致,并且希望插入全部数据,可以用这种方法:

INSERT INTO目标表 SELECT  * FROM来源表 ;

类别二、 如果只希望导入指定字段,可以用这种方法:

INSERT INTO目标表 (字段1, 字段2, ...)SELECT字段1, 字段2, ...FROM来源表 ;

类别三、 方法二的升级版 ,从原表取出部分字段数据的同时,目标表有新的字段也需要数据;

insert into 目标表 (列名1,列名2,列名3) values(1,2,(select 列3 from 原表));

我这里采用的是第二种方式;
代码如下;
我这里设计带了行列转换 所以才是这样

create PROCEDURE store_view_repair()
begin 
drop view if EXISTS v3_hlms_onecall_order_repair;
create view v3_hlms_onecall_order_repair as (
select * from (
select order_id as wo_id,b.order_code as service_data_id,'loc_id' as wo_field_code ,loc_id as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'loc_code' as wo_field_code ,loc_code as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'loc_name' as wo_field_code ,loc_name as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'dev_code' as wo_field_code ,dev_code as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'dev_name' as wo_field_code ,dev_name as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'notifier_appearance' as wo_field_code ,notifier_appearance as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'notifier_login' as wo_field_code ,notifier_login as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'notifier_name' as wo_field_code ,notifier_name as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'notifier_tel' as wo_field_code ,notifier_tel as wo_field_value from hlms_onecall.hlms_onecall_order_repair a
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'notifier_wechat' as wo_field_code ,notifier_wechat as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'dept_id' as wo_field_code ,loc_id as wo_field_value from hlms_onecall.hlms_onecall_order_repair a
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'dept_name' as wo_field_code ,dept_name as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'order_source' as wo_field_code ,order_source as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'order_major' as wo_field_code ,order_major as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'order_expect_time' as wo_field_code ,order_expect_time as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'notifier_no' as wo_field_code ,notifier_no as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'notifier_remark' as wo_field_code ,notifier_remark as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'is_self_repair' as wo_field_code ,is_self_repair as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'is_revoked' as wo_field_code ,is_revoked as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
UNION ALL
select order_id as wo_id,b.order_code as service_data_id,'trans_set' as wo_field_code,trans_set as wo_field_value from hlms_onecall.hlms_onecall_order_repair a 
left join hlms_onecall.hlms_onecall_order_base b on a.order_id=b.id
ORDER BY wo_id) as v3_order_repair);
end;
-- 调用视图
call store_view_repair();

insert into nts_datacenter.hlms_wo_field_info_copy3(wo_id,service_data_id,wo_field_code,wo_field_value)  select wo_id,service_data_id,wo_field_code,wo_field_value from nts_datacenter.v3_hlms_onecall_order_repair;

代码二: 正常表

-- 创建工单日志存储操作 开始------
-- 视图存储过程
drop PROCEDURE if EXISTS store_view_log; -- 如果存在则删除
create PROCEDURE store_view_log() --创建存储过程
begin
 drop view if EXISTS v3_hlms_onecall_order_log;
 create view v3_hlms_onecall_order_log as (
SELECT
b.order_id AS wo_id,
IFNULL(b.order_old_status,null) AS wo_old_status,
IFNULL(b.order_status,0) AS wo_status,
b.deal_user AS deal_by,
b.deal_name AS deal_name,
b.deal_phone AS deal_phone,
IFNULL(b.is_overtime,0) AS is_over_time,
b.over_time AS over_time,
r.remark as deal_description
FROM
	hlms_onecall.hlms_onecall_order_log b left join hlms_onecall.hlms_onecall_order_remark r on b.order_id=r.order_id
);
end;
call store_view_log(); -- 调用存储过程
-- 数据添加
 insert into nts_datacenter.hlms_wo_process_copy1(wo_id,wo_old_status,wo_status,deal_by,deal_name,deal_phone,is_over_time,over_time,deal_description) select wo_id,wo_old_status,wo_status,deal_by,deal_name,deal_phone,is_over_time,over_time,deal_description from nts_datacenter.v3_hlms_onecall_order_log;

-- 创建工单日志存储操作 结束------

你可能感兴趣的:(#,mysql,mysql,数据库,sql)