PostgreSQL 触发器程序根据不同的 table 字段更改 INSERT 字段值

PostgreSQL trigger procedure to change an INSERT field value based on different table's field

我有一个 table 个具有以下字段的命名案例:

在其他领域。

在另一个 table 发票中,我将 caseid 称为外键,例如

现在,每当我将记录插入发票时,我想检查发票日期是否大于或等于 cases.dateoffiling(对于该特定发票),如果不是,我想触发异常或设置invoicedate 等于 dateoffiling(您不能在提交案例之前开具发票)。

我试图用 Postgresql pgsql 触发器(包括文档和示例)打破我的头脑,但我无法在任何地方找到上述场景。任何帮助或指点将不胜感激。

我想我已经找到了触发功能的解决方案:

CREATE OR REPLACE FUNCTION invoicedatecheck()
   RETURNS trigger AS
$BODY$declare 
    filingdate date;
begin
    select dateoffiling from cases where casesid=new.caseid into filingdate;

    if new.invoicedate < filingdate then
    new.invoicedate = filingdate;
    end if;
    return new;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION invoicedatecheck()
OWNER TO remoteuser;