Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions & Triggers

Status
Not open for further replies.

teroy

Programmer
Oct 17, 2000
67
AU
I'm fairly new at writing triggers and function in postgres and i was wondering if this is the right way to go about it

Basically what i want it to do..is to update the last_update field (datetime) with the current date time whenever a row is accessed. Is this the right way to go about it?

Thanks in advance

CREATE FUNCTION troyupdate() RETURNS opaque AS '
BEGIN
UPDATE sessions SET sessions.last_update = now();
RETURN sessions;
END;
' LANGUAGE 'plpgsql';


CREATE TRIGGER "troytrigger" AFTER UPDATE ON "sessions" FOR EACH ROW EXECUTE PROCEDURE troyupdate();
 
Try
SNIP..
NEW.last_update = now();
RETURN NEW;
SNIP..

CREATE TRIGGER "troytrigger" BEFORE UPDATE ON "sessions" FOR EACH ROW EXECUTE PROCEDURE troyupdate();

Make sure its BEFORE UPDATE

 
Thankyou PGExplorer..

That worked perfectly :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top