嗨,我有一个学生表格,其中包含用户名,用户类型,密码等列.
我已经为该表编写了一个触发器,该触发器为用户类型’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 ;
它以前工作过,但现在显示了一些错误,该错误表明在’==’Student’)附近声明i varchar(2)时出现语法错误.等,我不知道如何解决
请帮助您提供宝贵的答案.提前致谢.
我也尝试过“ if(usertype =’Student’)THEN”,但随后在声明i varchar(2)时显示错误消息,如语法错误;
解决方法:
当我浏览手册时,看不到任何==支持,
因此,在您的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 ;