使用Postgres 9.3。是否可以插入函数的结果?
我试过了:
CREATE FUNCTION apply_func() RETURNS TRIGGER AS $$
DECLARE gwa char(9);
BEGIN
gwa:=(select gwa_calc(new.studid));
IF (SELECT noGrade_check(new.studid)) THEN
RAISE NOTICE 'ACCEPTED: GRADES COMPLETE';
IF (SELECT yrlvl_check(new.studid)) THEN
RAISE NOTICE 'ACCEPTED:YRLVL_CHECK';
IF (SELECT fail_check1(new.studid, new.sch_name)) THEN
RAISE NOTICE 'ACCEPTED:FAIL_CHECK';
IF (SELECT semunit_check1(new.studid)) THEN
RAISE NOTICE 'ACCEPTED:UNIT_CHECK';
IF (SELECT gwa_check(new.studid, new.sch_name)) THEN
RAISE NOTICE 'ACCEPTED:GWA_CHECK';
INSERT INTO apply(studid, sch_name, gpa)
VALUES (new.studid, new.sch_name, gwa);
RETURN NEW;
ELSE
RAISE NOTICE 'DECLINED:GPA_CHECK';
RETURN NULL;
END IF;
ELSE
RAISE NOTICE 'DECLINED:UNIT_CHECK';
RETURN NULL;
END IF;
ELSE
RAISE NOTICE 'DECLINED:FAIL_CHECK';
RETURN NULL;
END IF;
ELSE
RAISE NOTICE 'DECLINED:YRLVL_CHECK';
RETURN NULL;
END IF;
ELSE
RAISE NOTICE 'DECLINED: NULL GRADE EXISTS';
RETURN NULL;
END IF;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER apply_trig
BEFORE INSERT ON apply
FOR EACH ROW EXECUTE PROCEDURE apply_func();
我希望在获得奖学金所需的所有其他要求后,自动插入new.studid
,new.sch_name
和函数gwa_calc(new.studid)
的结果。
我该怎么做?
答案 0 :(得分:1)
以各种方式简化您的代码:
CREATE FUNCTION apply_func()
RETURNS trigger AS
$func$
BEGIN
IF NOT noGrade_check(NEW.studid) THEN
RAISE NOTICE 'DECLINED: NULL GRADE EXISTS';
ELSIF NOT yrlvl_check(NEW.studid) THEN
RAISE NOTICE 'DECLINED:YRLVL_CHECK';
ELSIF NOT fail_check1(NEW.studid, new.sch_name) THEN
RAISE NOTICE 'DECLINED:FAIL_CHECK';
ELSIF NOT semunit_check1(NEW.studid) THEN
RAISE NOTICE 'DECLINED:UNIT_CHECK';
ELSIF NOT gwa_check(NEW.studid, NEW.sch_name) THEN
RAISE NOTICE 'DECLINED:GPA_CHECK';
ELSE
NEW.gpa := gwa_calc(NEW.studid);
RETURN NEW;
END IF;
RETURN NULL;
END;
$func$ LANGUAGE plpgsql;
INSERT
只要您RETURN NEW
自动发生。不要复制它,这将导致无限循环。
我建议你颠倒测试的逻辑,这样你就可以简化并放弃所有的否定。如果您不需要所有这些通知,可以进一步简化。