这是我的postgreSQL代码
CREATE FUNCTION connectedExhibitionFunction()
RETURNS trigger AS
$$
BEGIN
IF (SELECT COUNT("exName") FROM Exhibitions WHERE NEW."exName" = "exName") > 0 THEN
IF (SELECT Count(doorLoc1) FROM Doors, ExhibitionLocations WHERE (dorLoc1=NEW.elLocation AND dorLoc2=elLocations) OR (dorLoc2=NEW.elLocation AND dorLoc1=elLocations) > 0 THEN
RAISE EXCEPTION 'You can't have the exhibition there, the same exhibition is in an unconnected room';
END IF;
END IF;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER connectedExhibitionTrigger
BEFORE INSERT
ON ExhibitionsLocations
EXECUTE PROCEDURE connectedExhibitionFunction();
这就是我得到的错误
psql:file.txt:62: ERROR: syntax error at or near "CREATE"
LINE 8: CREATE FUNCTION connectedExhibitionFunction()
^
psql:file.txt:67: ERROR: current transaction is aborted, commands ignored until end of transaction block
我似乎无法弄清楚错误,有人可以在这里找到任何东西吗?
答案 0 :(得分:3)
我猜你错过了选择“)”并且提出异常条款不能“不能” 你可以使用END而不是END IF。据我所知,你的问题就是这些。
请试试。
IF (SELECT Count(doorLoc1) FROM Doors, ExhibitionLocations
WHERE (dorLoc1=NEW.elLocation AND dorLoc2=elLocations) OR
(dorLoc2=NEW.elLocation AND dorLoc1=elLocations)) > 0 THEN
RAISE EXCEPTION 'You cant have the exhibition there, the same exhibition is in an unconnected room';
END
答案 1 :(得分:1)
您不应使用count()
函数来测试是否存在数据,而是使用PERFORM
命令。您还应该从触发器功能RETURN NEW
或RETURN NULL
或您的插入将失败的定义。通过其他一些改进,您将最终得到:
CREATE FUNCTION connectedExhibitionFunction() RETURNS trigger AS $$
BEGIN
PERFORM * FROM Exhibitions WHERE "exName" = NEW."exName";
IF FOUND THEN
PERFORM * FROM Doors, ExhibitionLocations
WHERE (dorLoc1 = NEW.elLocation AND dorLoc2 = elLocations)
OR (dorLoc2 = NEW.elLocation AND dorLoc1 = elLocations);
IF FOUND THEN
RAISE EXCEPTION 'You can''t have the exhibition there, the same exhibition is in an unconnected room';
RETURN NULL; -- Make the INSERT fail
END IF;
END IF;
RETURN NEW; -- Make the INSERT succeed
END;
$$ LANGUAGE plpgsql;