存储过程小记

DELIMITER $$
CREATE PROCEDURE `PROC_UPDATELOGINADDRESS`()
    BEGIN
     DECLARE UserIdVar BIGINT(20);
     DECLARE OldUserIdVar BIGINT(20);
     DECLARE IPToIntVar BIGINT(20);
     DECLARE LastLoginIpVar VARCHAR(32);
     DECLARE LastLoginDateVar INT(11);
     DECLARE NoMoreRecords INT DEFAULT 0; 
     DECLARE AddressVar VARCHAR(300);
        DECLARE CURSOR_LOGINDATA CURSOR FOR
         SELECT t.`UserId`,t.`LastLoginIp`,t.`LastLoginDate` FROM `usercenter`.`uc_userlastlogin_new` t
          WHERE t.`LastLoginDate`=(SELECT REPLACE(DATE_SUB(CURDATE(),INTERVAL 1 DAY),'-',''));
     DECLARE CONTINUE HANDLER FOR NOT FOUND BEGIN END;
        OPEN CURSOR_LOGINDATA; 
        FETCH CURSOR_LOGINDATA INTO UserIdVar, LastLoginIpVar, LastLoginDateVar; 
        REPEAT
            SET AddressVar = '';
            SET OldUserIdVar = UserIdVar;
            SELECT SUBSTRING_INDEX(LastLoginIpVar,'.',1)*1000000000+SUBSTRING(SUBSTRING_INDEX(LastLoginIpVar,'.',2),INSTR(LastLoginIpVar,'.')+1)*1000000
          +SUBSTRING_INDEX(SUBSTRING_INDEX(LastLoginIpVar,'.',-2),'.',1)*1000+SUBSTRING_INDEX(LastLoginIpVar,'.',-1)*1 INTO IPToIntVar;
            SELECT `Address` INTO AddressVar FROM `pt_ipbank_new` WHERE `IPStart` <= IPToIntVar AND `IPEnd` >= IPToIntVar ;
         -- 插入IP转换后的地址数据
         INSERT INTO `userlastlogin`(`UserId`,`LastLoginDate`,`LastLoginAddress`) VALUES (UserIdVar,LastLoginDateVar,AddressVar)
          ON DUPLICATE KEY UPDATE LastLoginDate=LastLoginDateVar,LastLoginAddress=AddressVar;
         FETCH CURSOR_LOGINDATA INTO UserIdVar, LastLoginIpVar, LastLoginDateVar; 
         IF UserIdVar = OldUserIdVar THEN
          SET NoMoreRecords = 1;
         END IF;
        UNTIL NoMoreRecords = 1
        END REPEAT;
        CLOSE  CURSOR_LOGINDATA;
    END$$
DELIMITER ;

之前写的一个存储过程,总是忘记存储过程的格式,在博客上记一下,顺便回顾个函数:)

SELECT SUBSTRING_INDEX('www.111cn.net', '.', 2);

www.111cn

SELECT SUBSTRING_INDEX('www.111cn.net', '.', -2);

111cn.net

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值