Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql trigger not not working

Hi I've a student table where I have columns like username, usertype, password etc. I have wrote a trigger for the this table which creates a username for the for the usertype 'Student', Here is the trigger codes

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

It was working before, but now its showing some error which states that there is a syntax error near '=='Student') declare i varchar(2). etc. And I have no clue how to solve it please help with your valuable answers. Thanks in advance.

I have tried with "if (usertype= 'Student') THEN" as well but then it shows error message like syntax error near declare i varchar(2);

like image 252
Pranjut Avatar asked Mar 24 '26 22:03

Pranjut


2 Answers

As I browsed on the manual, I can't see any == supported,

  • MySQL OPERATORS

So in your TRIGGER, should only be IF (usertype = 'Student') THEN

FULL TRIGGER CODE

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 ; 
like image 88
John Woo Avatar answered Mar 26 '26 10:03

John Woo


are you sure mysql allows == as equality operator?

like image 37
Brian Avatar answered Mar 26 '26 10:03

Brian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!