2017-10-28 138 views
0

我有以下程序查询工作正常:PostgreSQL的错误:语法错误或接近“执行”插入一个嵌套的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 

    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; 

但如果我插入另外的“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语句?

回答

1

你是一个END IF后失踪分号:

END IF /* need semicolon here */ 

PERFORM pg_notify 

好运。

+0

哇,谢谢。 – BeardMagician

相关问题