0

我想复制一个表中的行,该表由另一个表中的字段寻址,如下所示:

CREATE OR REPLACE FUNCTION f_ins_up_vorb()
RETURNS TRIGGER AS $$
DECLARE
    dienst      dienst%ROWTYPE;
    account     record; 
BEGIN
    -- ...
    EXECUTE format('SELECT * FROM %s WHERE id=$1',dienst.tabelle)
    USING NEW.id INTO account;
    EXECUTE 'INSERT INTO ' || dienst.tabelle || 'shadow SELECT ($1).*, now(), $2' USING account, jobid;
    RETURN NEW;
END
$$ LANGUAGE plpgsql;

但这会产生:

ERROR:  record type has not been registered
CONTEXT:  SQL statement "INSERT INTO accountadshadow SELECT ($1).*, now(), $2"
PL/pgSQL function f_ins_up_vorb() line 30 at EXECUTE statement

所寻址的表dienst.tabelle没有通用类型,但目标表 (dienst.tabelle || 'shadow')始终是源表的超集。所以这应该总是有效的(并且我使用的触发器函数中有效NEW有效,它似乎有一个记录类型)。

有没有办法解决?

4

1 回答 1

2

Try something like:

CREATE OR REPLACE FUNCTION f_ins_up_vorb()
RETURNS TRIGGER AS $$
DECLARE
    dienst      dienst%ROWTYPE;
BEGIN
    -- ...
    EXECUTE 'INSERT INTO '||dienst.tabelle||'shadow
             SELECT *, now(), $2
             FROM '||dienst.tabelle||' 
             WHERE id=$1'
    USING NEW.id, jobid;
    RETURN NEW;
END
$$ LANGUAGE plpgsql;

If you are trying to create some kind of log trigger - read this page first.

于 2013-05-14T14:41:31.453 回答