Thread: Triggers and Function's

Triggers and Function's

From
Campos Chaves
Date:
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?

Tanks

_______________________________________________________________________
Yahoo! Mail
Mais espaço, mais segurança e gratuito: caixa postal de 6MB, antivírus, proteção contra spam.
http://br.mail.yahoo.com/

Re: Triggers and Function's

From
Ewald Geschwinde
Date:
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?
>
>Tanks
>
>_______________________________________________________________________
>Yahoo! Mail
>Mais espaço, mais segurança e gratuito: caixa postal de 6MB, antivírus, proteção contra spam.
>http://br.mail.yahoo.com/
>
>---------------------------(end of broadcast)---------------------------
>TIP 2: you can get off all lists at once with the unregister command
>    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>
>
>
>
http://www.postgresql.org/docs/view.php?version=7.3&file=triggers.html

look at the docs
Regards Ewald Geschwinde


Re: Triggers and Function's

From
"scott.marlowe"
Date:
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.


Re: Triggers and Function's

From
"Alam Surya"
Date:
just example :

Create or replace function [function_name()] returns triggers as'
begin
insert into table_b ( field1, field2, field3 )  values ( value1 , values2,
value3 ) ;
return;
end ;'
languange 'plpgsql';

Create trigger trigger_name
      After Insert on [table_name]
      for each row
      execute procedure [function_name()];

regard...

Alam Surya

----- Original Message -----
From: "Campos Chaves" <camposchaves@yahoo.com.br>
To: <pgsql-general@postgresql.org>
Sent: Thursday, May 29, 2003 3:51 AM
Subject: [GENERAL] Triggers and Function's


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?

Tanks

_______________________________________________________________________
Yahoo! Mail
Mais espaço, mais segurança e gratuito: caixa postal de 6MB, antivírus,
proteção contra spam.
http://br.mail.yahoo.com/

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)


Re: Triggers and Function's

From
"Alam Surya"
Date:
that's problem is mean your postgresql not exist the plpgsql yet, before you
create that function you have to create languange handler for plpgsql
with.....

testdb=>CREATE FUNCTION plpgsql_call_handler () RETURNS OPAQUE AS
'$libdir/plpgsql' LANGUAGE C;

and then.....

testdb=>CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql HANDLER
plpgsql_call_handler;

best regards.....

Alam Surya


----- Original Message -----
From: "Campos Chaves" <camposchaves@yahoo.com.br>
To: "Alam Surya" <alam_surya@telkom.net>
Sent: Thursday, May 29, 2003 11:41 PM
Subject: Re: [GENERAL] Triggers and Function's


> See what happened: (It sad that the language is
> wrong!)
>
> Error - /var/www/intranet/phpPgAdmin/sql.php -- Line:
> 112
>
> PostgreSQL said: ERROR: language "plpgsql" does not
> exist
> Your query:
> Create function teste6() returns triggers as'
> begin
> insert into "VERSAO" ( codigo, descricao ) values (1,6
> ) ;
> return;
> end ;'
> language 'plpgsql'
>



Re: Triggers and Function's

From
Fabrizio Mazzoni
Date:
Or maybe you can use rules ..

--



On Wed, 28 May 2003 23:18:54 +0200
Ewald Geschwinde <webmaster@geschwinde.net> wrote:

> 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?
> >
> >Tanks
> >
> >_______________________________________________________________________
> >Yahoo! Mail
> >Mais espaço, mais segurança e gratuito: caixa postal de 6MB, antivírus, proteção contra spam.
> >http://br.mail.yahoo.com/
> >
> >---------------------------(end of broadcast)---------------------------
> >TIP 2: you can get off all lists at once with the unregister command
> >    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
> >
> >
> >
> >
> http://www.postgresql.org/docs/view.php?version=7.3&file=triggers.html
>
> look at the docs
> Regards Ewald Geschwinde
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org


--

Re: Triggers and Function's

From
Date:
> Or maybe you can use rules ..
>
> On Wed, 28 May 2003 23:18:54 +0200
> Ewald Geschwinde <webmaster@geschwinde.net> wrote:
>> Campos Chaves wrote:
>> >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?

Definitly for triggers, and probably for rules, you'll have to use
functions. Define the function first, then define the trigger or rule to
invoke the function.

~Berend Tober