MYSQL Trigger上的语法错误

时间:2012-12-01 11:59:31

标签: mysql

我的MYSQL触发器出错了。我已经使用了各种形式的语法,我在这里和互联网上的其他地方都找到了,但似乎没什么用。以下是我使用的3个语法示例:

>         CREATE TRIGGER LOG_UPDATE BEFORE UPDATE ON WORKLOG
>     FOR EACH ROW BEGIN
>       INSERT INTO WORKLOG_BACKUP VALUES(NULL, CURRENT_TIMESTAMP, NEW.LOGNO, NEW.JOBNO, NEW.EMPLOYEENO, NEW.WORKDATE, NEW.WORKTIME,
> 'UPDATE');    
>     END;
>     
>     CREATE TRIGGER LOG_UPDATE BEFORE UPDATE ON WORKLOG
>     FOR EACH ROW BEGIN
>       UPDATE WORKLOG_BACKUP SET BACKUPSTAMP = CURRENT_TIMESTAMP,
>       UPDATE WORKLOG_BACKUP SET LOGNO = NEW.LOGNO,
>       UPDATE WORKLOG_BACKUP SET JOBNO = NEW.JOBNO,
>       UPDATE WORKLOG_BACKUP SET EMPLOYEENO = NEW.EMPLOYEENO,
>       UPDATE WORKLOG_BACKUP SET WORKDATE = NEW.WORKDATE,
>       UPDATE WORKLOG_BACKUP SET WORKTIME = NEW.WORKTIME,
>       UPDATE WORKLOG_BACKUP SET CAUSE = 'UPDATE'; 
>     END;
>     
>     CREATE TRIGGER LOG_UPDATE BEFORE UPDATE ON WORKLOG
>     FOR EACH ROW BEGIN
>       INSERT INTO WORKLOG_BACKUP VALUES(
>       SET BACKUPSTAMP = CURRENT_TIMESTAMP,
>       SET LOGNO = NEW.LOGNO,
>       SET JOBNO = NEW.JOBNO,
>       SET EMPLOYEENO = NEW.EMPLOYEENO,
>       SET WORKDATE = NEW.WORKDATE,
>       SET WORKTIME = NEW.WORKTIME,
>       SET CAUSE = 'UPDATE');  
>     END;

我会帮助任何人提供任何帮助。我正在使用phpmyadmin。

1 个答案:

答案 0 :(得分:4)

为了让您的MySQL客户端不要将;语句的INSERT解释为CREATE TRIGGER语句的结尾,您必须告知它您希望使用其他语句陈述分隔符。

mysql命令行客户端中,您可以使用DELIMITER command执行此操作。例如,要将语句分隔符更改为双分号:

DELIMITER ;;

然后你可以这样做:

CREATE TRIGGER LOG_UPDATE BEFORE UPDATE ON WORKLOG FOR EACH ROW BEGIN
  INSERT INTO WORKLOG_BACKUP VALUES (
    NULL,
    CURRENT_TIMESTAMP,
    NEW.LOGNO,
    NEW.JOBNO,
    NEW.EMPLOYEENO,
    NEW.WORKDATE,
    NEW.WORKTIME,
    'UPDATE'
  );
END;;

但是,因为在这种情况下你的触发器只包含一个语句,所以你不需要使用BEGIN ... END compound statement block,因此可以完全避免更改分隔符:

CREATE TRIGGER LOG_UPDATE BEFORE UPDATE ON WORKLOG FOR EACH ROW
  INSERT INTO WORKLOG_BACKUP VALUES (
    NULL,
    CURRENT_TIMESTAMP,
    NEW.LOGNO,
    NEW.JOBNO,
    NEW.EMPLOYEENO,
    NEW.WORKDATE,
    NEW.WORKTIME,
    'UPDATE'
  )
;