【问题标题】:where is mistake in my MySql query for create trigger创建触发器的 MySql 查询中的错误在哪里
【发布时间】:2017-09-06 18:20:31
【问题描述】:

我有一个 stu 表,它有很多列(名称、lname、用户名、密码和其他),我想在每个新用户注册之后或之前插入一个长度为 8 或 6 个数字和字母字符的随机唯一字符串首次登录时每个新用户的密码。 经过多次搜索发现此代码:

    declare ready int default 0;
    declare rnd_str text;

    while not ready do
        set rnd_str := lpad(conv(floor(rand()*pow(36,6)), 10, 36), 6, 0);
        if not exists (select * from stu where pass = rnd_str) then
            set new.pass = rnd_str;
            set ready := 1;
        end if;
    end while;

我尝试通过 phpmyadmin 创建触发器,然后单击 GO 按钮给我一个错误。

谁能帮我解决这个问题?

phpmyadmin 中的错误是这个文本:

“处理您的请求时出现一个或多个错误:

The following query has failed: "CREATE TRIGGER `set_random_8char_pass` BEFORE INSERT ON `stu` FOR EACH ROW declare ready int default 0; declare rnd_str text; while not ready DO set rnd_str := lpad(conv(floor(rand()*pow(36,6)), 10, 36), 6, 0); if not exists (select * from stu where pass = rnd_str) then set new.pass = rnd_str; set ready := 1; end if; end while;"

MySQL said: #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 'declare ready int default 0; declare rnd_str text; while not ready' at line 1

"

谢谢大家

【问题讨论】:

    标签: mysql random triggers phpmyadmin passwords


    【解决方案1】:

    DECLARE 只允许在 BEGIN ... END 复合语句中 并且必须在它的开头,在任何其他语句之前。

    阅读https://dev.mysql.com/doc/refman/5.7/en/declare.html

    所以你的触发代码应该是这样的。

    CREATE TRIGGER `set_random_8char_pass` BEFORE INSERT ON `stu` FOR EACH ROW 
    
    BEGIN
      declare ready int default 0;
      declare rnd_str text;
    
      while not ready DO set rnd_str := lpad(conv(floor(rand()*pow(36,6)),        10, 36), 6, 0);
         if not exists (select * from stu where pass = rnd_str)
         then
           set new.pass = rnd_str;
           set ready := 1;
         end if;
       end while;
    END
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-17
      • 2013-07-16
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 2018-04-23
      • 2011-01-20
      • 1970-01-01
      相关资源
      最近更新 更多