本帖最后由 icerlion 于 2014-03-11 22:54:45 编辑

解决方案 »

  1.   

     INSERT INTO `tab_user_auth`(`user_name`, `user_password`, `account_type`, `create_time`) VALUES(
    CONCAT("test",CONVERT(account_index, CHAR(20))), "123456", 1, NOW());
         
      

  2.   


    DELIMITER $$
    DROP PROCEDURE IF EXISTS `sp_tab_user_auth_add_test_account`$$CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_tab_user_auth_add_test_account`(IN _account_count INT )
    BEGIN
    DECLARE account_index_ INT;
    SET account_index_=0;
    WHILE account_index<_account_count do
    INSERT INTO `tab_user_auth`(`user_name`, `user_password`, `account_type`, `create_time`) VALUES("test" + CONCAT(account_index, ''), "123456", 1, NOW());
    SET account_index_=account_index_+1;
    END while;

    END$$DELIMITER ;
      

  3.   


    前面的语法也存在问题
    这里的转换也存在问题CONVERT(account_index, VARCHAR(20)), 可以写成CONVERT(account_index, CHAR(20))
    也可以写成CONCAT(account_index,'')
      

  4.   

    DELIMITER $$
     
     
    DROP PROCEDURE IF EXISTS `sp_tab_user_auth_add_test_account`$$
     
    CREATE PROCEDURE `sp_tab_user_auth_add_test_account`(IN _account_count INT )
    BEGIN
        DECLARE account_index_ INT;
        SET account_index_=0;
        WHILE account_index<_account_count DO
            INSERT INTO `tab_user_auth`(`user_name`,`user_password`,`account_type`,`create_time`) VALUES(CONCAT("test",CONVERT(account_index, CHAR(20))),'123456', 1, NOW());
            SET account_index_=account_index_+1;
        END WHILE;
        END$$
    DELIMITER ;
      

  5.   


    MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值,并产生另一个类型的值。两者具体的语法如下:
    CAST(value as type);
    CONVERT(value, type);就是CAST(xxx AS 类型), CONVERT(xxx,类型)。可以转换的类型是有限制的。这个类型可以是以下值其中的一个:    二进制,同带binary前缀的效果 : BINARY    
     字符型,可带参数 : CHAR()     
        日期 : DATE     
        时间: TIME     
        日期时间型 : DATETIME     
        浮点数 : DECIMAL      
        整数 : SIGNED     
        无符号整数 : UNSIGNED
      
    没有  varcharwhile 循环用法大体如下:
      WHILE  条件判断 do
             ............#其他操作
       end while;另外建议:定义ps变量时 不要 使用 _ 开头或结尾 ,容易遗忘  ,建议直接使用 " ps_"开头。