Triggers and Function's

Started by Campos Chavesalmost 23 years ago7 messagesgeneral
Jump to latest
#1Campos Chaves
camposchaves@yahoo.com.br

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/

#2Ewald Geschwinde
webmaster@geschwinde.net
In reply to: Campos Chaves (#1)
Re: Triggers and Function's

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

#3Fabrizio Mazzoni
mazzofab@tiscalinet.it
In reply to: Ewald Geschwinde (#2)
Re: Triggers and Function's

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&amp;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

--

#4scott.marlowe
scott.marlowe@ihs.com
In reply to: Campos Chaves (#1)
Re: Triggers and Function's

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.

#5Alam Surya
alam_surya@telkom.net
In reply to: Campos Chaves (#1)
Re: Triggers and Function's

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)

#6Alam Surya
alam_surya@telkom.net
In reply to: Alam Surya (#5)
Re: Triggers and Function's

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

Show quoted text

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'

#7Berend Tober
btober@seaworthysys.com
In reply to: Fabrizio Mazzoni (#3)
Re: Triggers and Function's

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