2016-05-17 271 views
0

我有一个表名为例具有以下字段:PostgreSQL的触发器过程以改变INSERT字段值

  • caseid(主键)
  • casenumber
  • dateoffiling等

等领域。

在另一个表中,发票,我指的caseid作为foriegn键如

  • invoiceid(主键)
  • invoicecode
  • caseid(foriegn键 - > cases.caseid)
  • invoicedate 等

现在,每当我插入记录,发票,我要检查INVO icedate大于或等于cases.dateoffiling(对于特定的发票),如果不是,我想要触发异常或设置invoicedate等于dateoffiling(您无法在提交案例前提出发票)。

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

回答

0

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

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;