gpt4 book ai didi

mysql - SQL触发器不工作

转载 作者:可可西里 更新时间:2023-11-01 08:06:22 25 4
gpt4 key购买 nike

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

> 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') declare i varchar(2) 附近存在语法错误。等等,我不知道如何解决请帮助您提供宝贵的答案。提前致谢。

我也尝试过使用“if (usertype= 'Student') THEN”,但随后它显示错误消息,如 declare 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 ;

关于mysql - SQL触发器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15696931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com