您的位置:首页 > 数据库 > MySQL

mysql根据身份证获取省份、生日、性别,存储过程

2017-03-08 10:27 246 查看
DROP PROCEDURE IF EXISTS `extract_userAccounts_idnumber_info`;
DELIMITER ;;
CREATE PROCEDURE `update_userAccounts_idnumber_info`()
BEGIN
DECLARE birthdayUserAccount date;
DECLARE genderUserAccount VARCHAR(8);
DECLARE addrProvinceUserAccount VARCHAR(32);
DECLARE s int default 0;
DECLARE userAccountsId int(10);
DECLARE userAccounts CURSOR FOR select birthday,gender,addrProvince,id from userAccounts  where  LENGTH(idCard)=15 or LENGTH(idCard) =18  and (addrProvince is null or addrProvince ='' or gender is NULL OR gender = '' or birthday is null);
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;
OPEN userAccounts;
FETCH userAccounts INTO birthdayUserAccount,genderUserAccount,addrProvinceUserAccount,userAccountsId;
select s;
while s <> 1 do
begin
DECLARE birthday date;
DECLARE gender VARCHAR(8);
DECLARE addrProvince VARCHAR(32);
select  case left(idCard,2)
when '11' then '北京市'
when '12' then '天津市'
when '13' then '河北省'
when '14' then '山西省'
when '15' then '内蒙古自治区'
when '21' then '辽宁省'
when '22' then '吉林省'
when '23' then '黑龙江省'
when '31' then '上海市'
when '32' then '江苏省'
when '33' then '浙江省'
when '34' then '安徽省'
when '35' then '福建省'
when '36' then '江西省'
when '37' then '山东省'
when '41' then '河南省'
when '42' then '湖北省'
when '43' then '湖南省'
when '44' then '广东省'
when '45' then '广西壮族自治区'
when '46' then '海南省'
when '50' then '重庆市'
when '51' then '四川省'
when '52' then '贵州省'
when '53' then '云南省'
when '54' then '西藏自治区'
when '61' then '陕西省'
when '62' then '甘肃省'
when '63' then '青海省'
when '64' then '宁夏回族自治区'
when '65' then '新疆维吾尔自治区'
when '71' then '台湾'
when '81' then '香港'
when '82' then '澳门'
else '未知'
end   as addrProvince,
if(IsDate(SUBSTRING(idCard,7,8))=1,str_to_date(SUBSTRING(idCard,7,8),'%Y%m%d'),NULL) as birthday,
case if(length(idCard)=18, cast(substring(idCard,17,1) as UNSIGNED)%2, if(length(idCard)=15,cast(substring(idCard,15,1) as UNSIGNED)%2,3))
when 1 then 'M'
when 0 then 'F'
else 'UNKNOWN'
end as gender
from userAccounts  where id = userAccountsId iNTO addrProvince,birthday,gender;
if genderUserAccount is null or genderUserAccount = '' then
UPDATE userAccounts a set a.gender = gender where a.id = userAccountsId;
end if;
if addrProvinceUserAccount is null or addrProvinceUserAccount ='' then
UPDATE userAccounts a set a.addrProvince = addrProvince where a.id = userAccountsId;
end if;
if birthdayUserAccount is null then
UPDATE userAccounts a set a.birthday =birthday where a.id = userAccountsId;
end if;
end;
FETCH userAccounts INTO birthdayUserAccount,genderUserAccount,addrProvinceUserAccount,userAccountsId;
end while;
CLOSE userAccounts;
END
;;
DELIMITER ;

DROP FUNCTION IF EXISTS `IsDate`;
DELIMITER ;;
CREATE FUNCTION `IsDate`(sIn varchar(1024)) RETURNS int(11)
BEGIN
declare tp int;
if (select length(date(sIn)) is null )then
set tp = 0;
else
set tp = 1;
end if;
RETURN tp;
END
;;
DELIMITER ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐