MySQL汉字转对应的拼音字母(游标)
show variables like '%func%';
set global log_bin_trust_function_creators=1;
grant all privileges on *.* to root@"%" identified by ".";
SELECT LEFT('张三丰',1);
SELECT HEX(LEFT('张三丰',1));
SELECT HEX(CONVERT(LEFT('张三丰',1) USING GBK));
SELECT CONV(HEX(CONVERT(LEFT('张三丰',1) USING GBK)),16,10);
SELECT INTERVAL(CONV(HEX(CONVERT(LEFT('张三丰',1) USING GBK)),16,10),0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA ,0xCEF4,0xD1B9,0xD4D1);
SELECT ELT(INTERVAL(CONV(HEX(CONVERT(LEFT('张三丰',1) USING GBK)),16,10),0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA ,0xCEF4,0xD1B9,0xD4D1),'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
=================下面是针对 eperson 表姓名提取首字母的存储过程===============
DELIMITER ;;
drop procedure if exists getFirstHanZiCode;
CREATE DEFINER = CURRENT_USER PROCEDURE `getFirstHanZiCode`()
BEGIN
DECLARE userid INT;
DECLARE myname VARCHAR(100);
DECLARE len INT DEFAULT 0;
DECLARE i int DEFAULT 1;
DECLARE firstChar VARCHAR(50);
DECLARE allChar VARCHAR(50);
DECLARE s int default 0;
DECLARE cursor_name CURSOR FOR
SELECT eperson_id,scholar_name FROM eperson WHERE initials IS NULL;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;
OPEN cursor_name;
fetch cursor_name into userid,myname;
while s <> 1 do
SET firstChar = '';
SET allChar = '';
SET len = CHAR_LENGTH(myname);
IF len IS NOT NULL THEN
SET i = 1;
WHILE i <= len DO
SET firstChar = ELT(INTERVAL(CONV(HEX(CONVERT(SUBSTR(myname,i,1) USING GBK)),16,10),0xB0A1,0xB0C5,0xB2C1,0xB4EE,0xB6EA,0xB7A2,0xB8C1,0xB9FE,0xBBF7,0xBFA6,0xC0AC,0xC2E8,0xC4C3,0xC5B6,0xC5BE,0xC6DA,0xC8BB,0xC8F6,0xCBFA,0xCDDA ,0xCEF4,0xD1B9,0xD4D1),'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
SET allChar = CONCAT(allChar,firstChar) ;
SET i = i+1;
END WHILE;
END IF;
UPDATE `eperson` SET `initials` = allChar WHERE eperson_id =userid;
FETCH cursor_name INTO userid,myname;
END WHILE;
CLOSE cursor_name ;
SELECT 'done';
END;