PgSQL没有编译RETURNING ... INTO

时间:2018-04-12 01:14:19

标签: sql postgresql

我创建了一个视图,并且正在尝试为它创建一个INSERT RULE。

这是我的代码:

Fonte

我的INSERT分两步进行,首先插入Telecontagem表,然后插入id表。

为此,我需要从第一个插页中“存储”新的RETURNING id INTO NEW.telecontagem_id_fonte,因此我尝试使用INTO

然而,PgSQL抱怨告诉我RETURNING之后预计不会ERROR: syntax error at or near "INTO" at character ...

RULE

我不能在id内使用它吗?如果我无法存储新创建的{{1}}?

,如何执行此插入操作

1 个答案:

答案 0 :(得分:0)

因此,@ a_horse_with_no_name建议的解决方案是使用这样的触发器:

CREATE OR REPLACE FUNCTION v_Telecontagem_func()
    RETURNS TRIGGER
LANGUAGE plpgsql
AS $function$
    BEGIN
        IF TG_OP = 'INSERT' THEN
            INSERT INTO Fonte (id, nome, tipo_fonte) VALUES (DEFAULT, NEW.fonte_nome, NEW.fonte_tipo_fonte) RETURNING id INTO NEW.telecontagem_id_fonte;
            INSERT INTO Telecontagem (id_fornecedor, fornecedor, nome, id_fonte) VALUES (NEW.telecontagem_id_fornecedor, NEW.telecontagem_fornecedor, NEW.telecontagem_nome, NEW.telecontagem_id_fonte);
            RETURN NEW;
        ELSIF TG_OP = 'UPDATE' THEN
            -- UPDATE HERE
            RETURN NEW;
        ELSIF TG_OP = 'DELETE' THEN
            -- DELETE HERE
            RETURN NULL;
        END IF;
        RETURN NEW;
    END;
$function$;

CREATE TRIGGER v_Telecontagem__trig
    INSTEAD OF INSERT OR UPDATE OR DELETE ON
    v_Telecontagem FOR EACH ROW EXECUTE PROCEDURE v_Telecontagem_func();

基本上,对于同一个视图,我现在使用函数和触发器来执行INSERTUPDATEDELETE上的函数。