触发器创建中的错误也是如何优化它

时间:2013-04-18 06:26:36

标签: mysql triggers

请告诉我代码有什么问题。无法找到错误

DELIMITER $$
CREATE TRIGGER update_status  BEFORE Update ON listing_basic_new_updated 
FOR EACH ROW
 if new.processing_status is not null 
    then begin
    SET new.rep_status = New.processing_status;
    end; 
  elseif new.televeri_status is not null 
    then begin
    SET new.rep_status = New.televeri_status;
    end; 
  elseif new.verification_status is not null 
    then begin
    SET new.rep_status = New.verification_status;
    end;
end if;
END$$
DELIMITER ;

1 个答案:

答案 0 :(得分:1)

我认为你错过的BEGIN最终会与END相匹配:

DELIMITER $$
CREATE TRIGGER update_status  BEFORE Update ON listing_basic_new_updated 
FOR EACH ROW
BEGIN
 if new.processing_status is not null 
    then begin
    SET new.rep_status = New.processing_status;
    end; 
  elseif new.televeri_status is not null 
    then begin
    SET new.rep_status = New.televeri_status;
    end; 
  elseif new.verification_status is not null 
    then begin
    SET new.rep_status = New.verification_status;
    end;
end if;
END$$
DELIMITER ;

我认为您可以用

替换所有内容
SET new.rep_status = COALESCE(new.processing_status, new.televeri_status,
                           new.verification_status, new.rep_status);

COALESCE:“返回列表中的第一个非NULL值,如果没有非NULL值,则返回NULL。”