0

我只是想使用触发器而不是检查约束和代码一,但它给了我一个错误。

CREATE TRIGGER conflict 
ON roozane 
FOR EACH ROW 
BEGIN 
if rDate = NEW.rDate then
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType);
end if
end if
END;$$ 

和错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON roozane FOR EACH ROW BEGIN if ( rDate=NEW.rDate ) then if ( NEW.rStart' at line 2

编辑

CREATE TRIGGER conflict BEFORE INSERT
ON roozane 
FOR EACH ROW 
BEGIN 
if rDate = NEW.rDate then
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType);
end if
end if
END;$$ 

和错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 7

寻求帮助

4

3 回答 3

1

你需要一个 *trigger_time* 和 *trigger_event*。例如: CREATE TRIGGER conflict AFTER INSERT

于 2013-07-01T20:57:06.110 回答
0

You need a semicolon after each end if to terminate those compound statements.

You don't need a semicolon after the last END, because presumably you have used DELIMITER $$ to change the statement terminator in the mysql client.

I tested the following. It did not get a syntax error, but of course I have no table called roozane so I got a different error. :-)

CREATE TRIGGER conflict BEFORE INSERT
ON roozane 
FOR EACH ROW 
BEGIN 
if rDate = NEW.rDate then
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType);
end if;
end if;
END$$ 
于 2013-07-01T21:56:52.083 回答
0

您的触发器有几个问题。

就语法和逻辑错误而言

  1. DELIMITER $$查看错误消息显然您在脚本开始时没有使用。
  2. 您的触发器rDate, rStartTime,中有三个未声明的变量rEndTime。如果您使用存储过程级别的变量,您需要先声明它们并最终为它们赋值。
  3. 正如@BillKarwin 在他的回答中提到的那样,您必须在每个语句的末尾加上分号,IF ... END IF;并且在关闭触发器块后不需要分号,END因为BEGIN...END您应该DELIMITER早先更改为$$.

话虽如此,您的触发器的语法正确版本可能会遵循

DELIMITER $$
CREATE TRIGGER conflict 
BEFORE INSERT ON roozane 
FOR EACH ROW 
BEGIN 
  DECLARE rDate      DATE;
  DECLARE rStartTime TIME;
  DECLARE rEndTime   TIME;

  IF rDate = NEW.rDate THEN
    IF NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime THEN
        INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType)
        VALUES(NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType);
    END IF;
  END IF;
END$$ 

这是SQLFiddle演示,它显示现在您的触发器已成功创建,但由于默认声明的变量具有的值NULL和其他值尚未分配给它们,因此什么也不做。

这是最重要的部分:如果变量的问题将得到解决,不幸的是,您的触发器无论如何都无法工作,因为 MySql 对触发器的支持相当有限,不允许INSERT在同一张表上使用数据操作语句(在您的情况下) (roozane在您的情况下)您将触发器附加到。

现在,为了帮助您修复触发器,您需要解释您希望触发器检查的内容。

于 2013-07-02T23:29:23.923 回答