Sql触发器不工作

嗨,我有一个学生表,其中我有像用户名,usertype,密码等列 我已经写了一个触发器,为这个表创建一个用户名为usertype'学生'这里是触发代码Sql触发器不工作

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

这是工作之前,但现在它显示出一些错误,指出,有近一个语法错误“==”学生')声明我VARCHAR(2)。等我不知道如何解决它 请帮助您的宝贵答案。提前致谢。我试过“如果(usertype ='学生')THEN”,但它然后显示错误消息像语法错误附近declare我varchar(2);

回答:

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

  • MySQL OPERATORS

因此,在你TRIGGER,只应IF (usertype = 'Student') THEN

FULL触发代码

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 ;

回答:

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

以上是 Sql触发器不工作 的全部内容, 来源链接: utcz.com/qa/258476.html

回到顶部