Mysql存储过程中游标的用法实例
本文实例讲述了Mysql存储过程中游标的用法。分享给大家供大家参考。具体如下:
1.批量插入商户路由关联数据:
DELIMITER$$ USE`mmm_mac`$$ DROPPROCEDUREIFEXISTS`批量插入商户路由关联数据`$$ CREATEDEFINER=`root`@`%`PROCEDURE`批量插入商户路由关联数据`() BEGIN DECLAREv_partner_noVARCHAR(32); DECLAREv_partner_idINT(11); DECLAREv_sc_pidINT(11); DECLAREv_mac_noVARCHAR(32); DECLAREv_mac_addrVARCHAR(32); DECLAREn_mac_noBIGINT; DECLAREn_mac_addrBIGINT; DECLAREn_mac_addr_strVARCHAR(32); DECLAREdoneINT; #取得商户数据 DECLAREcur_partnerlistCURSOR FOR SELECTcomp_id,partner_no,sc_pidFROMmmm_partner.anl_partner; SETn_mac_no=100000000; SETn_mac_addr=1000000000; OPENcur_partnerlist; REPEAT FETCHcur_partnerlistINTOv_partner_id,v_partner_no,v_sc_pid; SETv_mac_no=CONCAT('MAC',v_sc_pid,n_mac_no); SETn_mac_addr_str=CONCAT(SUBSTR(n_mac_addr,1,2),':',SUBSTR(n_mac_addr,3,2),':',SUBSTR(n_mac_addr,5,2),':',SUBSTR(n_mac_addr,7,2),':',SUBSTR(n_mac_addr,9,2)); SETv_mac_addr=CONCAT('CC:',n_mac_addr_str); SETn_mac_no=n_mac_no+1; SETn_mac_addr=n_mac_addr+1; #向t_machine_sc_config表中插入商户关联路由的数据 #insertintot_machine_sc_config(mac_no,partner_no,partner_id,sc_pid,mac_addr,comp_id,is_lock)values('MAC2016000000001','44060430603381',1,4403,'C8:87:18:AB:79:66',1,1); INSERTINTOt_machine_sc_config(mac_no,partner_no,partner_id,sc_pid,mac_addr,comp_id,is_lock)VALUES(v_mac_no,v_partner_no,v_partner_id,v_sc_pid,v_mac_addr,1,1); UNTIL0ENDREPEAT; CLOSEcur_partnerlist; END$$ DELIMITER;
2.更新商户表:
DELIMITER$$ USE`mmm_partner`$$ DROPPROCEDUREIFEXISTS`更新商户表`$$ CREATEDEFINER=`root`@`%`PROCEDURE`更新商户表`() BEGIN DECLAREv_partner_noVARCHAR(32); DECLAREvpartner_noVARCHAR(32); DECLAREv_partner_idVARCHAR(32); DECLAREnBIGINT; DECLAREpartnerid_listCURSOR FOR SELECTcomp_idFROM100msh_partner.anl_partnerWHERETRIM(partner_no)=''; SETvpartner_no='2015415parno'; SETn=10000000; OPENpartnerid_list; REPEAT FETCHpartnerid_listINTOv_partner_id; SETv_partner_no=CONCAT(vpartner_no,n); SETn=n+1; UPDATEmmm_partner.anl_partnerSETpartner_no=v_partner_noWHEREcomp_id=v_partner_id; UNTIL0ENDREPEAT; CLOSEpartnerid_list; END$$ DELIMITER;
希望本文所述对大家的mysql数据库程序设计有所帮助。