mysql> source D:\jreate_fun_getID.sql
Query OK, 0 rows affected, 1 warning (0.00 sec)ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' atline 3
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'DECLARE char_round varchar(6)' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'DECLARE char_return varchar(26)' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual thatcorresponds to your MySQL serverversion for the right syntax to use near 'DECLA
RE count TINYINT' at line 1
ERROR 1193 (HY000): Unknown system variable 'count'
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'while
 count=1
         do
         select NOW()+'' into char_now from dual' at line 1
ERROR 1327 (42000): Undeclared variable: char_round
ERROR 1193 (HY000): Unknown system variable 'char_return'
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'if LE
NGTH(char_return)=20 then
          select count('x') into count from pk_tbl where' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'if co
unt=0 THEN
                        insert into pk_tbl (cloum) values(char_return)' at line
1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'end i
f' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'end i
f' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'end w
hile' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'retur
n char_return' at line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'end'
at line 1

解决方案 »

  1.   

    贴出D:\jreate_fun_getID.sql看上去应该是有 create procedure 语句,但未使用 delimiter //
      

  2.   


    drop function if EXISTS getID;#删除方法
    #创建获取唯一20位ID的方法
    create FUNCTION getID()RETURNS VARCHAR(26) 
    BEGIN
     DECLARE char_now VARCHAR(20);
     DECLARE char_round varchar(6);
     DECLARE char_return varchar(26);
     DECLARE count TINYINT ;
     set count=1;
     while count=1 
     do
     select NOW()+'' into char_now from dual;
     select round(round(rand(),6)*1000000) into char_round from dual;
     set char_return=CONCAT(char_now,char_round);
     if LENGTH(char_return)=20 then
      select count('x') into count from pk_tbl where cloum=char_return;
       #判断是否存在
     if count=0 THEN
    insert into pk_tbl (cloum) values(char_return);
     end if;
     end if;
     end while;
     return char_return;
    end;
    要怎么写 啊
      

  3.   

    delimiter //
    create FUNCTION getID()RETURNS VARCHAR(26) 
    BEGIN
     DECLARE char_now VARCHAR(20);
     DECLARE char_round varchar(6);
     DECLARE char_return varchar(26);
     DECLARE count TINYINT ;
     set count=1;
     while count=1 
     do
     select NOW()+'' into char_now from dual;
     select round(round(rand(),6)*1000000) into char_round from dual;
     set char_return=CONCAT(char_now,char_round);
     if LENGTH(char_return)=20 then
      select count('x') into count from pk_tbl where cloum=char_return;
       #判断是否存在
     if count=0 THEN
    insert into pk_tbl (cloum) values(char_return);
     end if;
     end if;
     end while;
     return char_return;
    end;
    //
    delimiter ;
      

  4.   


    按你说的 我成功了,在cmd里面执行 可以,还有两个问题帮忙解答下:1,怎么些批次执行脚本
    2 我在可视花窗口执行报错下面的:
    mysql> \. D:\jreate_fun_getID.sql;
    1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\. D:\jreate_fun_getID.sql' at line 1
    mysql> 
      

  5.   

    修改脚本,在WINDOWS的DOS窗口下
    mysql -u -p<D:\jreate_fun_getID.sql;