【问题标题】:Postgresql ERROR: syntax error at or near "PERFORM" when inserting a nested IF statement in a procedurePostgresql 错误:在过程中插入嵌套 IF 语句时在“PERFORM”处或附近出现语法错误
【发布时间】:2017-10-28 20:34:58
【问题描述】:

我有以下工作正常的过程查询:

CREATE OR REPLACE FUNCTION table_update_notify() RETURNS trigger AS $$
DECLARE
notification_channel text := TG_ARGV[0];
owner_id numeric := TG_ARGV[1];
owner_lat numeric := TG_ARGV[2];
owner_lng numeric := TG_ARGV[3];
trigger_radius numeric := TG_ARGV[4];
nearby_radius numeric := TG_ARGV[5];
changed_lat numeric;
changed_lng numeric;
user_id numeric;
is_close boolean;
name text;
BEGIN
  IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
    changed_lat = NEW.lat;
    changed_lng = NEW.lng;
    user_id = NEW.user_id;
    name = NEW.name;
  ELSE
    changed_lat = OLD.lat;
    changed_lng = OLD.lng;
    user_id = OLD.user_id;
    name = OLD.name;
  END IF;
  -- If updated user's location is within the trigger radius of the trigger owner's location
  IF earth_box(ll_to_earth(owner_lat, owner_lng), trigger_radius) @> ll_to_earth(changed_lat, changed_lng)
  -- Don't notify owner if the owner's location changes
  AND user_id != owner_id
  THEN

  PERFORM pg_notify(notification_channel, json_build_object('user_id', user_id, 'name', name, 'is_close', is_close)::text);

  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

但是如果我在“THEN”之后插入另一个“IF”系统,就像这样,我会得到一个错误:

CREATE OR REPLACE FUNCTION table_update_notify() RETURNS trigger AS $$
DECLARE
notification_channel text := TG_ARGV[0];
owner_id numeric := TG_ARGV[1];
owner_lat numeric := TG_ARGV[2];
owner_lng numeric := TG_ARGV[3];
trigger_radius numeric := TG_ARGV[4];
nearby_radius numeric := TG_ARGV[5];
changed_lat numeric;
changed_lng numeric;
user_id numeric;
is_close boolean;
name text;
BEGIN
  IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
    changed_lat = NEW.lat;
    changed_lng = NEW.lng;
    user_id = NEW.user_id;
    name = NEW.name;
  ELSE
    changed_lat = OLD.lat;
    changed_lng = OLD.lng;
    user_id = OLD.user_id;
    name = OLD.name;
  END IF;
  -- If updated user's location is within the trigger radius of the trigger owner's location
  IF earth_box(ll_to_earth(owner_lat, owner_lng), trigger_radius) @> ll_to_earth(changed_lat, changed_lng)
  -- Don't notify owner if the owner's location changes
  AND user_id != owner_id
  THEN

   -- If the user is close enough to the user to be considered nearby
  IF earth_box(ll_to_earth(owner_lat, owner_lng), trigger_radius) @> ll_to_earth(changed_lat, changed_lng) THEN
  is_close = true;
  ELSE
  is_close = false;
  END IF

  PERFORM pg_notify(notification_channel, json_build_object('user_id', user_id, 'name', name, 'is_close', is_close)::text);

  END IF;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

错误是:

ERROR:  syntax error at or near "PERFORM"
LINE 39:   PERFORM pg_notify(notification_channel, json_build_object(...

根据我的研究,当语言未设置为 plpgsql 时会发生这种情况,但我显然正在这样做。如何执行这个嵌套的 IF 语句?

【问题讨论】:

    标签: sql database postgresql performance stored-procedures


    【解决方案1】:

    END IF 后面缺少分号:

    END IF    /* need semicolon here */
    
    PERFORM pg_notify
    

    祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 2021-12-04
      • 2020-02-05
      • 2021-12-13
      • 2021-12-24
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多