我正在尝试为以下表创建触发器:
CREATE TABLE public.first (
userid bigint,
name varchar
);
CREATE TABLE public.second (
userid bigint,
companyid bigint
);
CREATE TABLE public.visibility_matrix (
name varchar,
companyid bigint
);
每当表格first
中的某一行更新时,触发器都应该使用userid
并在表userid
中搜索相同的second
。如果存在,则从visibility_matrix
中删除companyid
中从第二个表中提取CREATE OR REPLACE FUNCTION pos_org_rel_refresh()
RETURNS trigger AS
$$
DECLARE
r Integer ;
BEGIN
IF TG_OP='UPDATE' THEN
DELETE FROM visibility_matrix where companyid=NEW.companyid;
RETURN NEW;
END IF;
END;
$$
LANGUAGE 'plpgsql';
的行/。
以下是我的尝试:
触发功能:
CREATE TRIGGER test_trigger
AFTER UPDATE
ON first
FOR EACH ROW
EXECUTE PROCEDURE pos_org_rel_refresh();
触发:
res.push({date: data21.date,description: data21.action});
console.log(JSON.stringify(res));
console.log(res.toString());
答案 0 :(得分:1)
使用USING
clause of DELETE
加入另一个表:
触发功能:
CREATE OR REPLACE FUNCTION pos_org_rel_refresh()
RETURNS trigger AS
$func$
-- DECLARE
-- r int; -- not used in function body
BEGIN
-- IF TG_OP='UPDATE' THEN -- redundant while func is only used in AFTER UPDATE trigger
DELETE FROM public.visibility_matrix v
USING public.second s
WHERE s.userid = NEW.userid
AND v.companyid = s.companyid;
-- END IF;
RETURN NEW; -- and don't place this inside the IF block either way
END
$func$ LANGUAGE plpgsql; -- don't quote the language name
触发:
CREATE TRIGGER test_trigger
AFTER UPDATE ON first
FOR EACH ROW EXECUTE PROCEDURE pos_org_rel_refresh();