我需要了解有关postgres的更多信息,但我正在尝试编写一个链接到postgres db中的触发器的函数。通常我写的函数工作正常,但是,这个特定的函数会在输出结束时保持返回语法周围的错误。
我有一个名为'结果'的灯具表。我想从夹具中读取分数,例如Wolves 0:0 Ipswich,并触发结果' Draw'。
我写的查询如下:
CREATE OR REPLACE FUNCTION sppullscoretrigger()
RETURNS trigger
SECURITY DEFINER
AS $BODY$
DECLARE
payload text;
BEGIN
SELECT NEW.Home_Side_Score, NEW.Away_Side_Score FROM Fixtures;
IF NEW.Home_Side_Score is NULL or NEW.Away_Side_Score is NULL THEN NEW.Result = 'TBC';
IF NEW.Home_Side_Score = NEW.Away_Side_Score THEN NEW.Result = 'Draw';
IF NEW.Home_Side_Score > NEW.Away_Side_Score THEN NEW.Result = 'Home_Win';
IF NEW.Home_Side_Score < NEW.Away_Side_Score THEN NEW.Result = 'Away_Win';
ELSE NEW.Result = 'Error';
END IF;
RETURN NEW;
END
$BODY$
LANGUAGE plpgsql
CREATE TRIGGER trgscore
BEFORE INSERT OR UPDATE
ON Fixtures
FOR EACH ROW
EXECUTE PROCEDURE sppullscoretrigger();
我之前只编写过基本功能,但我真的很难让这个触发器工作。
答案 0 :(得分:1)
当您的意思是IF
时,您似乎正在使用ELSIF
。这个版本编译;
CREATE OR REPLACE FUNCTION sppullscoretrigger()
RETURNS trigger
SECURITY DEFINER
AS $BODY$
DECLARE
payload text;
BEGIN
SELECT NEW.Home_Side_Score, NEW.Away_Side_Score FROM Fixtures;
IF NEW.Home_Side_Score is NULL or NEW.Away_Side_Score is NULL THEN NEW.Result = 'TBC';
ELSIF NEW.Home_Side_Score = NEW.Away_Side_Score THEN NEW.Result = 'Draw';
ELSIF NEW.Home_Side_Score > NEW.Away_Side_Score THEN NEW.Result = 'Home_Win';
ELSIF NEW.Home_Side_Score < NEW.Away_Side_Score THEN NEW.Result = 'Away_Win';
ELSE NEW.Result = 'Error';
END IF;
RETURN NEW;
END
$BODY$
LANGUAGE plpgsql