如何将此 function/trigger 从 postgres 迁移到 oracle?
how to migrate this function/trigger from postgres to oracle?
您好,我正在尝试迁移此函数和触发器,以便它可以在 oracle 中使用,我有几个需要这样做,只是寻求其中一个的帮助,这样我就可以弄清楚其余的没问题。感谢您的帮助。
CREATE FUNCTION podium_core.entity_tag_sync()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
BEGIN
insert into podium_core.pd_entity_tag(entity_nid,tag,version,modifiedby, createdby)
select distinct FOO.entity_nid, NEW.tag,0,'trigger','trigger' from
(select entity_nid from podium_core.pd_field where nid in (
select child_field_nid from podium_core.pd_field_pc_rel where parent_field_nid in
(select max(f.nid) from podium_core.pd_field f join podium_core.pd_entity e on e.nid= f.entity_nid
where entity_nid=NEW.entity_nid and entity_type='EXTERNAL')))FOO
where NEW.createdby <> 'trigger';
delete from podium_core.pd_entity_tag t1
USING podium_core.pd_entity_tag t2
WHERE t1.nid < t2.nid
AND t1.tag = t2.tag
AND t1.entity_nid = t2.entity_nid;
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION podium_core.entity_tag_sync()
OWNER TO postgres;
-- Trigger: trigger_entity_tag_sync
-- DROP TRIGGER trigger_entity_tag_sync ON podium_core.pd_entity_tag;
CREATE TRIGGER trigger_entity_tag_sync
AFTER INSERT
ON podium_core.pd_entity_tag
FOR EACH ROW
EXECUTE PROCEDURE podium_core.entity_tag_sync();
所以我刚刚重写了您的代码以匹配 Oracle 语法,但我怀疑它是否真的适合您 - 我认为它可能会引发 "table is mutating" 错误。祝你好运。
CREATE OR REPLACE TRIGGER trigger_entity_tag_sync
AFTER INSERT ON podium_core.pd_entity_tag
FOR EACH ROW
BEGIN
insert into podium_core.pd_entity_tag(entity_nid,tag,version,modifiedby, createdby)
select distinct FOO.entity_nid, :NEW.tag,0,'trigger','trigger'
from
(select entity_nid
from podium_core.pd_field
where nid in (
select child_field_nid
from podium_core.pd_field_pc_rel
where parent_field_nid in
(select max(f.nid) from podium_core.pd_field f join podium_core.pd_entity e on e.nid= f.entity_nid
where entity_nid=:NEW.entity_nid and entity_type='EXTERNAL')
)
)FOO
where :NEW.createdby <> 'trigger';
delete from podium_core.pd_entity_tag t1
where exists (select 1 from podium_core.pd_entity_tag t2
WHERE t1.nid < t2.nid
AND t1.tag = t2.tag
AND t1.entity_nid = t2.entity_nid);
END;
/
您好,我正在尝试迁移此函数和触发器,以便它可以在 oracle 中使用,我有几个需要这样做,只是寻求其中一个的帮助,这样我就可以弄清楚其余的没问题。感谢您的帮助。
CREATE FUNCTION podium_core.entity_tag_sync()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
BEGIN
insert into podium_core.pd_entity_tag(entity_nid,tag,version,modifiedby, createdby)
select distinct FOO.entity_nid, NEW.tag,0,'trigger','trigger' from
(select entity_nid from podium_core.pd_field where nid in (
select child_field_nid from podium_core.pd_field_pc_rel where parent_field_nid in
(select max(f.nid) from podium_core.pd_field f join podium_core.pd_entity e on e.nid= f.entity_nid
where entity_nid=NEW.entity_nid and entity_type='EXTERNAL')))FOO
where NEW.createdby <> 'trigger';
delete from podium_core.pd_entity_tag t1
USING podium_core.pd_entity_tag t2
WHERE t1.nid < t2.nid
AND t1.tag = t2.tag
AND t1.entity_nid = t2.entity_nid;
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION podium_core.entity_tag_sync()
OWNER TO postgres;
-- Trigger: trigger_entity_tag_sync
-- DROP TRIGGER trigger_entity_tag_sync ON podium_core.pd_entity_tag;
CREATE TRIGGER trigger_entity_tag_sync
AFTER INSERT
ON podium_core.pd_entity_tag
FOR EACH ROW
EXECUTE PROCEDURE podium_core.entity_tag_sync();
所以我刚刚重写了您的代码以匹配 Oracle 语法,但我怀疑它是否真的适合您 - 我认为它可能会引发 "table is mutating" 错误。祝你好运。
CREATE OR REPLACE TRIGGER trigger_entity_tag_sync
AFTER INSERT ON podium_core.pd_entity_tag
FOR EACH ROW
BEGIN
insert into podium_core.pd_entity_tag(entity_nid,tag,version,modifiedby, createdby)
select distinct FOO.entity_nid, :NEW.tag,0,'trigger','trigger'
from
(select entity_nid
from podium_core.pd_field
where nid in (
select child_field_nid
from podium_core.pd_field_pc_rel
where parent_field_nid in
(select max(f.nid) from podium_core.pd_field f join podium_core.pd_entity e on e.nid= f.entity_nid
where entity_nid=:NEW.entity_nid and entity_type='EXTERNAL')
)
)FOO
where :NEW.createdby <> 'trigger';
delete from podium_core.pd_entity_tag t1
where exists (select 1 from podium_core.pd_entity_tag t2
WHERE t1.nid < t2.nid
AND t1.tag = t2.tag
AND t1.entity_nid = t2.entity_nid);
END;
/