[置顶] 【mysql 存储过程】存储过程的应用--数据迁移和更新
2018-02-08 17:45
549 查看
数据迁移过程中,往往要更新数据,插入数据,此时运用存储过程,利用脚本自动化完成增删改查,业务逻辑,得心用手。
简单存储过程例子1:
复杂存储过程例子2:
附录:(python调用存储过程)
简单存储过程例子1:
CREATE DEFINER=`admin`@`%` PROCEDURE `update_busi_process`(IN idx int(10)) begin SELECT workflow_id into @m1 from busi_process WHERE process_owner=2 and id=idx; SELECT franchiser_code into@m2 from busi_workflow WHERE id=@m1; SELECT id into@m3 from b_company WHERE code=@m2; UPDATE busi_process set process_owner=@m3 WHERE id=idx; end
复杂存储过程例子2:
CREATE DEFINER=`admin`@`%` PROCEDURE `update_information_config`(IN t_process_id int(10)) SQL SECURITY INVOKER begin declare done1 int default 0; declare m_id int(10); declare m_process_id int(10); declare m_pid int(10); declare m_name varchar(50); declare m_level char(4); declare m_gmt_create datetime; declare m_gmt_modified datetime; DECLARE cur1 cursor for SELECT id,process_id,pid,`name`,`level`,gmt_create,gmt_modified from busi_information_config WHERE process_id=t_process_id and pid=0; declare continue handler for not found set done1 = 1; open cur1; fetch cur1 into m_id,m_process_id,m_pid,m_name,m_level,m_gmt_create,m_gmt_modified; while done1<>1 do /*******************************操作 **************************************/ if(m_name="借款人") then INSERT INTO `busi_information_config` (`process_id`, `pid`, `name`, `level`, `gmt_create`, `gmt_modified`) VALUES (t_process_id, '0', '借款人信息', '2',now() ,now()); SELECT id into @w_id from busi_information_config WHERE `name`="借款人信息" and pid=0 and process_id=t_process_id; UPDATE busi_information_config set pid=@w_id,`level`='3' WHERE `name`="借款人" and process_id=t_process_id; ELSEIF(m_name="直系亲属") then INSERT INTO `busi_information_config` (`process_id`, `pid`, `name`, `level`, `gmt_create`, `gmt_modified`) VALUES (t_process_id, '0', '次借人信息', '2',now() ,now()); SELECT id into @w_id from busi_information_config WHERE `name`="次借人信息" and pid=0 and process_id=t_process_id; UPDATE busi_information_config set pid=@w_id,`level`='3' WHERE `name`="直系亲属" and process_id=t_process_id; ELSEIF(m_name="担保人1") then INSERT INTO `busi_information_config` (`process_id`, `pid`, `name`, `level`, `gmt_create`, `gmt_modified`) VALUES (t_process_id, '0', '担保人信息', '2',now() ,now()); SELECT id into @w_id from busi_information_config WHERE `name`="担保人信息" and pid=0 and process_id=t_process_id; UPDATE busi_information_config set pid=@w_id,`level`='3' WHERE `name`="担保人1" and process_id=t_process_id; ELSEIF(m_name="担保人2") then SELECT id into @w_id from busi_information_config WHERE `name`="担保人信息" and pid=0 and process_id=t_process_id; UPDATE busi_information_config set pid=@w_id,`level`='3' WHERE `name`="担保人2" and process_id=t_process_id; ELSEIF(m_name="签署合同") then INSERT INTO `busi_information_config` (`process_id`, `pid`, `name`, `level`, `gmt_create`, `gmt_modified`) VALUES (t_process_id, '0', '合同信息', '2',now() ,now()); SELECT id into @w_id from busi_information_config WHERE `name`="合同信息" and pid=0 and process_id=t_process_id; UPDATE busi_information_config set pid=@w_id,`level`='3' WHERE `name`="签署合同" and process_id=t_process_id; end if; commit; set done1 = 0; fetch cur1 into m_id,m_process_id,m_pid,m_name,m_level,m_gmt_create,m_gmt_modified; end while ; close cur1; end
附录:(python调用存储过程)
# encoding: utf-8 import time import sys reload(sys) sys.setdefaultencoding('utf-8') time1 = time.time() import pandas as pd import pymysql ###########################################从数据库读数据########################################### # ########线上###################### # ##加上字符集参数,防止中文乱码 # conn=pymysql.connect( # host="*************", # database="cgjr", # user="**************", # password="**************", # port=******, # charset='utf8' # ) ############测试库###################### # 加上字符集参数,防止中文乱码 conn=pymysql.connect( host="***************", database="cgdj_busi_3", user="***********", password="***********", port=**********, charset='utf8' ) # sqlcmd4=""" SELECT DISTINCT id from busi_process WHERE process_owner=2 ; """ # #利用pandas 模块导入mysql数据 data=pd.read_sql(sqlcmd4,conn) print data print len(data) # # ###################更新order_rapay表 for each in data["id"]: print each # 创建游标 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #有参数存储过程 cursor.execute('call update_busi_process(%s)',(str(each))) conn.commit() # cursor.close() print '调用存储过程完毕................' conn.close() time2=time.time() print u'总共耗时:' + str(time2 - time1) + 's'
相关文章推荐
- [置顶] 【mysql 存储过程】应用存储过程的优点与缺点
- [置顶] 【mysql 存储过程】存储过程之流程控制语句
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- mysql 存储过程 通过存储过程的方式简化sql操作
- MYSQL存储过程:批量更新数据
- mysql数据存储过程应用
- phpMyAdmin调用和生成MySQL的存储过程以及CURSOR的应用
- MySQL存储过程与触发器(应用场景)
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- 初探mysql的存储过程和函数-----自定义存储过程和函数
- MySQL存储过程详解 mysql 存储过程
- MySQL存储过程详解 mysql 存储过程
- 百度查询复制粘贴:MySQL存储过程详解 mysql 存储过程
- mysql存储过程学习及java调用存储过程
- MySQL存储过程和函数(3) - 存储过程
- mysql存储过程游标与全局事务应用范例