Thread: SQL QUERY

SQL QUERY

From
"Pedro Igor"
Date:
I have the following function:

CREATE FUNCTION public.auto_incremento() RETURNS trigger AS '
begin
new.id = (
select (max(id) + 1) from teste);
return new;
end;'
LANGUAGE 'plpgsql'

I created a trigger that uses this function, but i want to the function be usefull for all tables and not only to tbale teste.

Someone know how ???

 

Pedro Igor

 

Re: SQL QUERY

From
Achilleus Mantzios
Date:
On Tue, 3 Dec 2002, Pedro Igor wrote:

> I have the following function:
> CREATE FUNCTION public.auto_incremento() RETURNS trigger AS '
> begin
> new.id = (select (max(id) + 1) from teste);
> return new;
> end;' LANGUAGE 'plpgsql'
>
> I created a trigger that uses this function, but i want to the function be usefull for all tables and not only to
tbaleteste.
 
>
> Someone know how ???

Take a look at sequences.
(In Addition, they are autimatically created when a column is of type
'SERIAL').

>
>
>
> Pedro Igor
>
>
>
>

==================================================================
Achilleus Mantzios
S/W Engineer
IT dept
Dynacom Tankers Mngmt
Nikis 4, Glyfada
Athens 16610
Greece
tel:    +30-10-8981112
fax:    +30-10-8981877
email:  achill@matrix.gatewaynet.com       mantzios@softlab.ece.ntua.gr



Re: SQL QUERY

From
Achilleus Mantzios
Date:
On Tue, 3 Dec 2002, Pedro Igor wrote:

> But i want implement a trigger that auto-increments the id field. This
> options is personal, i don΄t like to depend from the database.
>

Then use table name as parameter to your function.
But trully its an error prone policy the one you try to follow,
what if a table has a primary key named "fobarid"??
Your function will not work correctly.

Or you could programatically read the system tables
and find out which column is the primary key for a given table.
But unfortunately that way you depend on the database
even worse than simply and nicely use sequences.

> Pedro Igor
>
> ----- Original Message -----
> From: "Achilleus Mantzios" <achill@matrix.gatewaynet.com>
> To: "Pedro Igor" <pedroigor@aip.com.br>
> Cc: <pgsql-sql@postgresql.org>
> Sent: Tuesday, December 03, 2002 11:05 AM
> Subject: Re: [SQL] SQL QUERY
>
>
> > On Tue, 3 Dec 2002, Pedro Igor wrote:
> >
> > > I have the following function:
> > > CREATE FUNCTION public.auto_incremento() RETURNS trigger AS '
> > > begin
> > > new.id = (select (max(id) + 1) from teste);
> > > return new;
> > > end;' LANGUAGE 'plpgsql'
> > >
> > > I created a trigger that uses this function, but i want to the function
> be usefull for all tables and not only to tbale teste.
> > >
> > > Someone know how ???
> >
> > Take a look at sequences.
> > (In Addition, they are autimatically created when a column is of type
> > 'SERIAL').
> >
> > >
> > >
> > >
> > > Pedro Igor
> > >
> > >
> > >
> > >
> >
> > ==================================================================
> > Achilleus Mantzios
> > S/W Engineer
> > IT dept
> > Dynacom Tankers Mngmt
> > Nikis 4, Glyfada
> > Athens 16610
> > Greece
> > tel:    +30-10-8981112
> > fax:    +30-10-8981877
> > email:  achill@matrix.gatewaynet.com
> >         mantzios@softlab.ece.ntua.gr
>
>

==================================================================
Achilleus Mantzios
S/W Engineer
IT dept
Dynacom Tankers Mngmt
Nikis 4, Glyfada
Athens 16610
Greece
tel:    +30-10-8981112
fax:    +30-10-8981877
email:  achill@matrix.gatewaynet.com       mantzios@softlab.ece.ntua.gr