我正在尝试创建以下函数来检查是否存在某些内容。它应该返回true或false。然后我创建了一个触发器来执行该功能。到处都不起作用/语法错误。求救!
CREATE FUNCTION checkForGPU() RETURNS BOOLEAN AS $approved$
BEGIN
IF EXISTS (
SELECT C.ID
FROM Components C, ComputerComponents CC, ComponentType T, Computer PC
WHERE CC.ComputerID = C.ID
AND CC.ComputerID = PC.ID
AND C.Type = T.ID
AND T.Name = 'GPU')
THEN
RETURN TRUE
ELSE THEN
RETURN FALSE
END;
$approved$ LANGUAGE plpgsql;
CREATE TRIGGER checkGPU
AFTER INSERT OR UPDATE OR DELETE ON ComputerComponents
EXECUTE PROCEDURE checkForGPU();
答案 0 :(得分:2)
如果没有IF-THEN-ELSE语句
,您的功能可以更简单地重写CREATE FUNCTION checkForGPU() RETURNS BOOLEAN AS $approved$
BEGIN
RETURN EXISTS(
SELECT C.ID
FROM Components AS C, ComputerComponents AS CC, ComponentType AS T, Computer AS PC
WHERE CC.ComputerID = C.ID
AND CC.ComputerID = PC.ID
AND C.Type = T.ID
AND T.Name = 'GPU'
);
END;
$approved$ LANGUAGE plpgsql;