stored-procedure.sql

    1. ## 所有的用户表:
    2. update jmf_sys_user set invite_code = createInviteCode(id) where invite_code is null;
    3. ## 函数脚本:用完后删除即可
    4. CREATE FUNCTION createInviteCode(num BIGINT(20)) RETURNS varchar(50)
    5. BEGIN
    6. DECLARE remainder BIGINT (20);
    7. DECLARE inviteCode VARCHAR (50);
    8. DECLARE chars VARCHAR (62);
    9. DECLARE str VARCHAR (2);
    10. DECLARE scale BIGINT (20);
    11. DECLARE temp BIGINT;
    12. SET remainder = 0;
    13. SET inviteCode = '';
    14. SET chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    15. SET str = '';
    16. SET scale = 62;
    17. SET temp = 0;
    18. WHILE num > scale-1 DO
    19. SET remainder = MOD (num, scale);
    20. SET temp = remainder + 1;
    21. SET str = substr(chars, temp, 1);
    22. SET inviteCode = CONCAT(inviteCode, str);
    23. SET num = floor(CAST(num / scale AS DECIMAL(24,4)));
    24. END
    25. WHILE;
    26. SET temp = num + 1;
    27. SET str = substr(chars, temp, 1);
    28. SET inviteCode = CONCAT(inviteCode, str);
    29. SET inviteCode = REVERSE(inviteCode);
    30. SET inviteCode = lpad(inviteCode, 11, 'A');
    31. RETURN inviteCode;
    32. END