Re: trigger to access only the last transaction

From: Frank Pinto <frank(at)ayalo(dot)co>
To: avpro avpro <avprowebeden(at)gmail(dot)com>
Cc: Marcos Almeida Azevedo <marcos(dot)al(dot)azevedo(at)gmail(dot)com>, "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>, pgsql-sql(at)postgresql(dot)org
Subject: Re: trigger to access only the last transaction
Date: 2015-03-09 08:00:37
Message-ID: CAATpuJoqeUzVkZpfB4Pm505TmZJfDdP-uzXQ7Rn61J5JsWsCxQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice pgsql-sql

That's what the FOR EACH ROW part of the statement is for. Your trigger
function will be executed once per each row that is modified / inserted:
http://www.postgresql.org/docs/9.4/static/sql-createtrigger.html.

Frank

On Mon, Mar 9, 2015 at 1:50 AM, avpro avpro <avprowebeden(at)gmail(dot)com> wrote:

> Sorry I forgot to mention: last insert could have multiple rows inserted
> and I would like to access all of them, not necessarily only the last one.
> That's my problem. Or I misunderstand the "last entry".
> Thank you.
> On 9 Mar 2015 08:29, "Marcos Almeida Azevedo" <marcos(dot)al(dot)azevedo(at)gmail(dot)com>
> wrote:
>
>>
>>
>> On Mon, Mar 9, 2015 at 6:01 AM, avpro avpro <avprowebeden(at)gmail(dot)com>
>> wrote:
>>
>>> hi all,
>>>
>>> i have two tables with several columns:
>>> table1
>>> idtaskhistory
>>> performedat
>>> ...
>>> idtask
>>>
>>> and
>>>
>>> table2
>>> idtask,
>>> manualdueat
>>> .....
>>>
>>>
>>> i created a trigger that does the following:
>>>
>>> CREATE TRIGGER del1
>>> AFTER INSERT
>>> ON table1
>>> FOR EACH ROW
>>> EXECUTE PROCEDURE taskdel;
>>>
>>> taskdel:
>>> CREATE OR REPLACE FUNCTION taskdel()
>>> RETURNS trigger AS
>>> $BODY$begin
>>> update table2
>>> SET
>>> manualdueat = null,
>>> FROM table1,
>>> where table1.idtask = table2.idtask;
>>> return new;
>>> END$BODY$
>>> LANGUAGE plpgsql VOLATILE
>>> COST 100;
>>> ALTER FUNCTION taskdel()
>>> OWNER TO user1;
>>>
>>> my idea was to have the info from column "manualdueat" deleted after the
>>> table1 has been updated. the problem i face is that all columns from table2
>>> will be deleted, not only my last entry where the insert has been done (I
>>> was thinking that table1.idtask = table2.idtask will work, but isn't); my
>>> question to you would be how to access only the last insert id in table1
>>> and make this trigger working. or do you have another walk arround?
>>>
>>
>> How about querying and order by primary key descending and get the first
>> one?
>>
>>
>>
>>> I'm using psql 9.4 on a windows system
>>>
>>> thank you for your ideas,
>>> John
>>>
>>
>>
>>
>> --
>> Marcos | I love PHP, Linux, and Java
>> <http://javadevnotes.com/java-float-to-string-examples>
>>
>

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message s d 2015-03-09 08:32:08 Re: trigger to access only the last transaction
Previous Message avpro avpro 2015-03-09 07:50:47 Re: trigger to access only the last transaction

Browse pgsql-sql by date

  From Date Subject
Next Message s d 2015-03-09 08:32:08 Re: trigger to access only the last transaction
Previous Message avpro avpro 2015-03-09 07:50:47 Re: trigger to access only the last transaction