欢迎来到天天文库
浏览记录
ID:34724642
大小:280.18 KB
页数:3页
时间:2019-03-10
《mysql存储过程中临时表的建立及游标遍历》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、最近在做公司月报表的时候出现了一个很让人为难的问题,查询一个支付的过程,内容如下:两个表:支付记录表A,支付日志表BA表内容如下:B表内容如下:现在要做的事把A表中的记录关联到B表中字段operate每次checkNo操作的最后一步,当然,这个用程序实现是一个小儿科的事,可是我最也是脑子发热,偏偏只想用sql来实现,在两天研究中终于是实现了。用到的方法主要有两个,一个是游标的遍历和临时表插入查询实现代码如下: CREATEDEFINER=`root`@`localhost`PROCEDURE`Jfind`(StartTimeDateTime,EndTimeDateTime)BEGIND
2、ECLARErint;DECLAREDoneINTDEFAULT0;DECLARERCodeint(3)DEFAULT0;DECLARESidvarchar(32);DECLARESphoneNumbervarchar(10);DECLAREScutPaymentDatedatetime;DECLARESmoneydouble(10,2);DECLARESisCutPaymentSucceedint(2);DECLAREScheckNOvarchar(32);DECLARESipAddressvarchar(20);/*建立游标*/DECLARErsCURSORFORSELECT*FR
3、OMAwherecutPaymentDate>STR_TO_DATE(StartTime,'%Y-%m-%d%H:%i:%s')andcutPaymentDate4、rchar(10)NOTNULL,`cutPaymentDate`datetimeNOTNULL,`money`double(10,2)NOTNULLdefault'0.00',`isCutPaymentSucceed`int(2)NOTNULL,`checkNO`varchar(32)NOTNULL,`ipAddress`varchar(20)NOTNULL,`returnCode`int(3)NOTNULL,PRIMARYKEY(`ID`))TYPE=HEAP;OPENrs;/*开启游标*/FETCHNEXTFROMrsINTOSid,SphoneNumber,ScutPaymen5、tDate,Smoney,SisCutPaymentSucceed,ScheckNO,SipAddress;REPEATIFNOTDoneTHENSELECT`returnCode`intoRCodefrom`B`where`checkNO`=ScheckNOorderby`operate`desclimit0,1;INSERTINTO`tmp_paymentReport`set`id`=Sid,`phoneNumber`=SphoneNumber,`cutPaymentDate`=ScutPaymentDate,`money`=Smoney,`isCutPaymentSucceed`6、=SisCutPaymentSucceed,`checkNO`=ScheckNO,`ipAddress`=SipAddress,`returnCode`=RCode;FETCHNEXTFROMrsINTOSid,SphoneNumber,ScutPaymentDate,Smoney,SisCutPaymentSucceed,ScheckNO,SipAddress;ENDIF;UNTILDoneENDREPEAT;CLOSErs;set@RunSQL="select*fromtmp_paymentReport";preparesmtmfrom@RunSQL;executesmtm;END7、 调用方法是: callJfind('2011-06-0100:00:00','2011-07-0100:00:00');/*查询六月的记录*/
4、rchar(10)NOTNULL,`cutPaymentDate`datetimeNOTNULL,`money`double(10,2)NOTNULLdefault'0.00',`isCutPaymentSucceed`int(2)NOTNULL,`checkNO`varchar(32)NOTNULL,`ipAddress`varchar(20)NOTNULL,`returnCode`int(3)NOTNULL,PRIMARYKEY(`ID`))TYPE=HEAP;OPENrs;/*开启游标*/FETCHNEXTFROMrsINTOSid,SphoneNumber,ScutPaymen
5、tDate,Smoney,SisCutPaymentSucceed,ScheckNO,SipAddress;REPEATIFNOTDoneTHENSELECT`returnCode`intoRCodefrom`B`where`checkNO`=ScheckNOorderby`operate`desclimit0,1;INSERTINTO`tmp_paymentReport`set`id`=Sid,`phoneNumber`=SphoneNumber,`cutPaymentDate`=ScutPaymentDate,`money`=Smoney,`isCutPaymentSucceed`
6、=SisCutPaymentSucceed,`checkNO`=ScheckNO,`ipAddress`=SipAddress,`returnCode`=RCode;FETCHNEXTFROMrsINTOSid,SphoneNumber,ScutPaymentDate,Smoney,SisCutPaymentSucceed,ScheckNO,SipAddress;ENDIF;UNTILDoneENDREPEAT;CLOSErs;set@RunSQL="select*fromtmp_paymentReport";preparesmtmfrom@RunSQL;executesmtm;END
7、 调用方法是: callJfind('2011-06-0100:00:00','2011-07-0100:00:00');/*查询六月的记录*/
此文档下载收益归作者所有