Thread: auto datetime update on UPDATE?

auto datetime update on UPDATE?

From
Tulassay Zsolt
Date:
Hi,

i have set up a table, where two fields (creation_dt and modification_dt)
always get the actual date/time by default when doing an INSERT.
my question is, how is it possible to let the field modification_dt
change when doing an UPDATE?
Is there a way to solve it in SQL?

(actually i found something in the mailing list archive,
where the sender solves the problem by creating a trigger
and a C function, but i would really apppreciate if there was
a solution in SQL only.
anyway, that solution is at
http://www.PostgreSQL.ORG/mhonarc/pgsql-sql/1998-06/msg00056.html )

Zsolt Tulassay

ps i'm not on the mailing list so please send a copy to my address  as well. thanks





Re: [SQL] auto datetime update on UPDATE?

From
Christophe Labouisse
Date:
Tulassay Zsolt <zsolt@tek.bke.hu> writes:

> Hi,
> 
> i have set up a table, where two fields (creation_dt and modification_dt)
> always get the actual date/time by default when doing an INSERT.
> my question is, how is it possible to let the field modification_dt
> change when doing an UPDATE?
> Is there a way to solve it in SQL?

To do this I use a trigger in plpgsql :

create function liens_maj_fun () returns opaque as '
begin       new.liens_maj := ''now'';       return new;
end;
' language 'plpgsql';

create trigger liens_maj_trig before update or insert   on liens for each row execute procedure liens_maj_fun();


Since it uses plpgsql don't forget to add it when you create your
base:


CREATE FUNCTION plpgsql_call_handler()  RETURNS opaque  AS '/usr/local/pgsql/lib/plpgsql.so'  LANGUAGE 'c';

CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'  HANDLER plpgsql_call_handler  LANCOMPILER 'PL/pgSQL';


-- 
christophe.labouisse@dial.oleane.com
Le cinéma en Lumière : http://www.lumiere.org/
Vote en cours pour la création du forum fr.rec.arts.sf.starwars
pour en savoir plus : <news:38146a03.373985713@news.mp-poissy.ac-versailles.fr>


Re: [SQL] auto datetime update on UPDATE?

From
Darrin Crook
Date:
This is wat I have done to solve the problem.

UPDATE table SET modification_dt = datetime('now'::abstime);

You can update any other field at the same time. This enters the date-time
stamp into the filed. And you can use this format for INSET INTO also.

I hope this helps.

Darrin Crook                crook@nexus.as.utexas.edu
McDonald Observatory            wk: 915-426-4167
The University of Texas at Austin    hm: 915-426-3652

On Sun, 31 Oct 1999, Tulassay Zsolt wrote:
> i have set up a table, where two fields (creation_dt and modification_dt)
> always get the actual date/time by default when doing an INSERT.
> my question is, how is it possible to let the field modification_dt
> change when doing an UPDATE?
> Is there a way to solve it in SQL?