Re: Triggers and Function's - Mailing list pgsql-general

From scott.marlowe
Subject Re: Triggers and Function's
Date
Msg-id Pine.LNX.4.33.0305281559530.28136-100000@css120.ihs.com
Whole thread Raw
In response to Triggers and Function's  (Campos Chaves <camposchaves@yahoo.com.br>)
List pgsql-general
On Wed, 28 May 2003, Campos Chaves wrote:

> Hi,
>
> After I insert on rocord on Table A, I need to insert
> another record on Table B. I think that I have to use
> triggers and functions. But I don't know how! You can
> help me?

You can either do it by hand in a transaction, or in something like
plpgsql / triggers.

To do it by hand, you do:

begin;
insert into table a (
    field1,
    field2,
    idfield
) values (
    'data',
    number,
    nextval('sequence')
);
insert into table b (
    fielda,
    fieldb,
    fktotable1
) values (
    'date',
    number3,
    currval('sequence')
);
commit;

Just make the whole thing a plsql function and feed it all the data it
needs and you don't have to worry about anything but checking for errors
in your app.

If you need to update other tables based on some kind of caclulation, then
you'll be needing triggers or rules.


pgsql-general by date:

Previous
From: Hadley Willan
Date:
Subject: Re: Can anybody recommend an IDE for writing SQL/PLPSQL
Next
From: "Andy Kriger"
Date:
Subject: how much does a connection cost?