3

嗨,我有一个学生表,其中有用户名、用户类型、密码等列。我为此表编写了一个触发器,它为用户类型“学生”创建了一个用户名,这是触发器代码

> DROP TRIGGER `sasis`.`USERNAMEAutoGen`;

DELIMITER ||

CREATE TRIGGER `sasis`.`USERNAMEAutoGen` 
BEFORE INSERT ON `sasis`.`userinfo` 
FOR EACH ROW BEGIN  
    if (usertype== 'Student')
      declare i varchar(2);
      declare usrnm varchar(20);
      declare fullusrnm varchar(255);
      declare curyr varchar(10);
      declare curm varchar(10);
      declare rcount varchar(50);
      set i='I';
      select date_format(now(),'%y') into curyr;
      select date_format(now(),'%b') into curm;
      set usrnm=concat(i,curyr,upper(curm));
      select count(*) into rcount from userinfo where username like concat(usrnm,'%');
      if(rcount<9) then
        set rcount=concat('0000',(rcount+1));
      elseif (rcount<99) then
        set rcount=concat('000',(rcount+1));
      elseif (rcount<999) then
        set rcount=concat('00',(rcount+1));
      end  if;
      set fullusrnm=concat(i,curyr,upper(curm),rcount);
       if (new.username is null or trim(new.username)='') then
        set NEW.username=fullusrnm;
      end if;   end if; END||

DELIMITER ;

它以前可以工作,但现在显示一些错误,表明'=='Student'附近存在语法错误)声明i varchar(2)。等等,我不知道如何解决它,请帮助您提供宝贵的答案。提前致谢。

我也尝试过使用“if (usertype='Student') THEN”,但随后它在 declare i varchar(2); 附近显示语法错误之类的错误消息;

4

2 回答 2

3

当我浏览手册时,我看不到任何==支持,

所以在你的TRIGGER, 应该只IF (usertype = 'Student') THEN

完整的触发代码

CREATE TRIGGER `sasis`.`USERNAMEAutoGen` 
BEFORE INSERT ON `sasis`.`userinfo` 
FOR EACH ROW 
BEGIN  

    declare i varchar(2);
    declare usrnm varchar(20);
    declare fullusrnm varchar(255);
    declare curyr varchar(10);
    declare curm varchar(10);
    declare rcount varchar(50);

    IF (usertype== 'Student') THEN

        set i='I';
        select date_format(now(),'%y') into curyr;
        select date_format(now(),'%b') into curm;
        set usrnm=concat(i,curyr,upper(curm));
        select count(*) into rcount from userinfo where username like concat(usrnm,'%');

        if(rcount<9) then
            set rcount=concat('0000',(rcount+1));
        elseif (rcount<99) then
            set rcount=concat('000',(rcount+1));
        elseif (rcount<999) then
            set rcount=concat('00',(rcount+1));
        end  if;

        set fullusrnm=concat(i,curyr,upper(curm),rcount);
        if (new.username is null or trim(new.username)='') then
            set NEW.username=fullusrnm;
        end if; 

      end if; 
END||

DELIMITER ; 
于 2013-03-29T03:43:28.220 回答
1

你确定mysql允许==作为相等运算符吗?

于 2013-03-29T03:39:37.787 回答