2016-07-26 19:48 GMT-03:00 Matheus de Oliveira <[email protected]>:

>
> 2016-07-26 13:29 GMT-03:00 Felipe Rigotti [SBSistemas] <
> [email protected]>:
>
>> CREATE FUNCTION fnc_tgr_tab_a_upd (
>>
>> )
>>
>> RETURNS trigger AS
>>
>> $body$
>>
>> DECLARE chave_tabb integer;
>>
>> BEGIN
>>
>>                 insert into tab_b(campo4) values(new.campo1) returning
>> campo3 into chave_tabb;
>>
>>                return new;
>>
>> END;
>>
>> $body$
>>
>> LANGUAGE 'plpgsql'
>>
>> VOLATILE
>>
>> CALLED ON NULL INPUT
>>
>> SECURITY INVOKER;
>>
>>
>>
>> CREATE TRIGGER tab_a_tr
>>
>>   AFTER UPDATE
>>
>>   ON public.tab_a FOR EACH ROW
>>
>>   EXECUTE PROCEDURE fnc_tgr_tab_a_upd();
>>
>>
>>
>> update tab_a set campo2='z' where campo1=1
>>
>
> Você quer alterar na própria linha que foi atualizada, certo?
>
> Se sim, ao invés de usar AFTER, use BEFORE e faça:
>
>
>     insert into tab_b(campo4) values(new.campo1) returning campo3 into
> chave_tabb;
>     new.campos2 := chave_tabb;
>     return new;
>
> At.
> --
> Matheus de Oliveira
>
>
>
Bom dia, a ideia inicial seria não alterar a "principal" (tab_a), mas se
não tiver jeito vou ter que criar um novo campo nessa tabela para armazenar
a chave da segunda tabela só para pegar pelo update.

Felipe Rigotti
_______________________________________________
pgbr-geral mailing list
[email protected]
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a