reach参数

z9smfwbn  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(180)

我正在尝试在insert之前编写一个触发器,以检查参数是否符合要求。我尝试了很多方法,如果我对d和e的值进行硬编码,它就会像预期的那样工作,如果e=d。但是当我用new中的变量替换它时,它就不起作用了。带触发器的表有列id和languagetag\ id。请告诉我我做错了什么?

CREATE or replace FUNCTION insert_name() RETURNS trigger AS $insert_name$
declare d int;
declare e int;
    begin  
        d = (select distinct languagetagid from "scheme".table1 as a
                                    left join "scheme".table2 as b on (a.id = b.country_id )
                                    where b.id = NEW.id);
        e = NEW.languagetag_id;                     
    IF ( e != d ) THEN
                RAISE EXCEPTION 'error: % ', e;
    END IF;
    RETURN NULL;
    END;
$insert_name$ LANGUAGE plpgsql;

CREATE TRIGGER insert_name BEFORE INSERT or UPDATE ON "scheme".table
    EXECUTE PROCEDURE insert_name();
wwodge7n

wwodge7n1#

访问 NEW 以及 OLD 值时,必须对每一行执行触发器,而不是对整个语句

CREATE TRIGGER insert_name BEFORE INSERT or UPDATE ON "scheme".table
     FOR EACH ROW EXECUTE PROCEDURE insert_name();

相关问题