存储过程实例

函数:

BEGIN
DECLARE status_p int DEFAULT 0 ;
DECLARE status_e int DEFAULT 0 ;
select count(1) into status_p from tms_member where name = names and `password` = passwords limit 1;
if status_p > 0 THEN
return 114;
END if;
select count(1) into status_e from tms_member where  `email` = emails limit 1;
if status_e > 0 THEN
return 118;
END if;
return 0;
END


存储过程:

BEGIN

 DECLARE num int;
 DECLARE sqlstatus int;
 DECLARE returnCode int;
 DECLARE showStatus varchar(20) DEFAULT '';
 DECLARE showName varchar(20) DEFAULT '';
 DECLARE deviceMac varchar(20) DEFAULT '';
 DECLARE var_status varchar(20) DEFAULT '';
 DECLARE var_username varchar(20) DEFAULT '';
 DECLARE var_mac varchar(20) DEFAULT '';
 DECLARE done INT DEFAULT 0;
 DECLARE sqlstr cursor for 
 select p1.status,p3.serialNo as mac from tms_member p1
 LEFT JOIN tms_member_terminal_bind p2 on p1.id =p2.member_id
 LEFT JOIN tms_terminal_infos p3 on p2.terminal_id = p3.id
 where p1.`name`=username and p1.`password`=password and p3.mac_address=in_mac;
 DECLARE sqlstr2 cursor for 
 select p4.name  from tms_member p4
 where p4.`name`=username and p4.`password`=password;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN sqlstr2;
 REPEAT
 FETCH sqlstr2 INTO showName;
 IF NOT done THEN
 set var_username = showName;
 end if;
 UNTIL done end repeat;
 close sqlstr2;
OPEN sqlstr;
 REPEAT
 FETCH sqlstr INTO showStatus,deviceMac;
 IF NOT done THEN
 set var_status = showStatus;
 set var_mac = deviceMac;
 end if;
 UNTIL done end repeat;
 close sqlstr;

 if  var_username = "" THEN
     set returnCode = 103;
 ELSEIF var_mac = "" THEN
set returnCode = 900;
 ELSEIF  var_status = "1" THEN
     set returnCode = 119;
 ELSE
select count(*) into num from tms_member 
where  name=username  and `password`=password
and now() between token_create_time and token_expire_time limit 1;
 
if num = 0 THEN
update tms_member 
set token = token ,`token_create_time` = now(),token_expire_time = date_add(now(),INTERVAL 3 MONTH),login_count=login_count+1,
          last_login_time = now(),last_ip = ip
where  name=username  and `password`=password;
END IF;  
set sqlstatus = ROW_COUNT();
        if sqlstatus > 0 then
if num > 0 THEN
set returnCode = 2;
ELSE
set returnCode = 1;
end if;
else
set returnCode = 103;
end if;
 END IF;
select returnCode;

END


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caidimin/article/details/52355025
文章标签: 存储 函数
个人分类: mysql
上一篇Javamail体验
下一篇LDAP阶段性小结
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭