Sql触发器无法正常工作

时间:2013-03-29 03:36:40

标签: mysql sql triggers

您好我有一个学生表,我有像用户名,用户类型,密码等列。 我为这个表写了一个触发器,它为usertype'Student'创建了一个用户名,这是触发器代码

> 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 ;

它以前工作过,但现在它显示一些错误,表明'=='学生'附近有语法错误)声明i varchar(2)。我不知道如何解决它 请帮助您获得有价值的答案。提前谢谢。

我已尝试使用“if(usertype ='Student')THEN”,但它会在声明i varchar(2)附近显示语法错误等错误消息;

2 个答案:

答案 0 :(得分: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 ; 

答案 1 :(得分:1)

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