if 附近的 Postgres 语法错误

Postgres syntax error near if

我有下面的代码,这是table上的触发器函数的缩小版我想应用触发器,table实际上有很多字段:

Create OR REPLACE FUNCTION PRC_EPV_() 
RETURNS trigger AS $PRC_EPV_$

begin

  Declare BaseCount bigint default 0;
  Declare PeakCount bigint default 0;
  Declare BaseSUM DOUBLE PRECISION default 0;
  Declare PeakSum DOUBLE PRECISION default 0;

    IF new.epv_kw_1 IS NOT NULL THEN
         set BaseSum = BaseSum + new.EPV_KW_1;
         set BaseCount = BaseCount + 1;
    END if;

     IF  new.EPV_KW_38 IS NOT NULL Then
         set BaseSum = BaseSum + new.EPV_KW_38;
         set PeakSum = PeakSum + new.EPV_KW_38;
         set BaseCount = BaseCount + 1;
         set PeakCount = PeakCount + 1;
     END if;
     IF new.EPV_KW_91 IS NOT NULL Then
         set BaseSum = BaseSum + new.EPV_KW_91;
         set BaseCount = BaseCount + 1;
     END if;

     set new.EPV_Peak_Sum = PeakSum ;
     set new.EPV_Base_Sum = BaseSum ;

     IF PeakCount > 0 Then
      set new.EPV_Peak_AVE =  new.EPV_peak_Sum / PeakCount * 4;
     END if;

     IF BaseCount > 0 Then
     set new.EPV_Base_AVE =  new.EPV_base_Sum / BaseCount * 4;
     END if;

    RETURN NEW;
    END;
$PRC_EPV_$ LANGUAGE plpgsql;

pgAdmin SQL 编者告诉我 错误:"IF" 处或附近的语法错误 第 11 行:IF new.epv_kw_1 IS NOT NULL THEN

我这辈子都弄不明白为什么我总是尝试用 'true'、'1'、'-1'、'1=1' 替换条件语句,它给我的结果是一样的IF

附近的语法错误

尝试更改

RETURNS trigger AS $PRC_EPV_$

begin

  Declare BaseCount bigint default 0;
  Declare PeakCount bigint default 0;
  Declare BaseSUM DOUBLE PRECISION default 0;
  Declare PeakSum DOUBLE PRECISION default 0;

    IF new.epv_kw_1

进入

RETURNS trigger AS $PRC_EPV_$
Declare 
  BaseCount bigint default 0;
  PeakCount bigint default 0;
  BaseSUM DOUBLE PRECISION default 0;
  PeakSum DOUBLE PRECISION default 0;
begin
    IF new.epv_kw_1