Selective Auto-Timestamp [using triggers?]

Поиск
Список
Период
Сортировка
Искать

Selective Auto-Timestamp [using triggers?]

От:
Rajit Singh <singh.raj@studychoice.com>
Дата:
Dear List,

I've recently found that it would be useful if I could update a timestamp field in some of my tables automatically so that whenever someone updates the tables in question, the timestamp reflects when that change was made.

The thing is, I also want to be able to update the timestamp field if I want to... thus if I update, say, the 'name' field - the timestamp field would be updated to 'now()'.  However, if I updated the timestamp field then that field would simply take on the value I had suggested.

I managed to find a partially working solution as follows:

CREATE FUNCTION update_modtime() RETURNS opaque AS 'BEGIN IF OLD.modtime = NEW.modtime THEN NEW.modtime = now() END IF; RETURN NEW; END;' LANGUAGE 'plpgsql';
CREATE TRIGGER autostamp BEFORE UPDATE ON  FOR EACH ROW EXECUTE PROCEDURE update_modtime();

The problem with this is, if I do UPDATE  set modtime =  WHERE  where  is the current value of modtime, modtime then takes the value of now().  But if I'm explicitly setting it to its existing value, I don't want the trigger to do this.  Of course, I could modify my scripts and stuff so that, if I don't want modtime to change, I don't try to change it.  But I'm not the only user - and I think the behaviour would be more pleasant for the different users if modtime always updated to what was specified, if a user was explicitly setting it.

Thanks for your time,
Any help greatly appreciated.
Rajit

Selective Auto-Timestamp [using triggers?]

От:
Dan Lyke <danlyke@flutterby.com>
Дата:
Rajit Singh writes:
> The thing is, I also want to be able to update the timestamp field
> if I want to...

Here's my solution:

CREATE FUNCTION updated_stamp () RETURNS OPAQUE AS
'    BEGIN
	IF NEW.updated ISNULL THEN
		NEW.updated := ''now'';
	END IF;
        RETURN NEW;
    END;
' LANGUAGE 'plpgsql';

Re: Selective Auto-Timestamp [using triggers?]

От:
"rob" <rob@cabrion.com>
Дата:
Change:

if old.modtime = new.modtime

To:

if new.modtime is null


I *think* that's what you are looking for.

--rob

----- Original Message -----
From: "Rajit Singh" 
To: 
Sent: Thursday, January 11, 2001 7:00 AM
Subject: Selective Auto-Timestamp [using triggers?]


> Dear List,
>
> I've recently found that it would be useful if I could update a timestamp
field in some of my tables automatically so that whenever someone updates
the tables in question, the timestamp reflects when that change was made.
>
> The thing is, I also want to be able to update the timestamp field if I
want to... thus if I update, say, the 'name' field - the timestamp field
would be updated to 'now()'.  However, if I updated the timestamp field then
that field would simply take on the value I had suggested.
>
> I managed to find a partially working solution as follows:
>
> CREATE FUNCTION update_modtime() RETURNS opaque AS 'BEGIN IF OLD.modtime =
NEW.modtime THEN NEW.modtime = now() END IF; RETURN NEW; END;' LANGUAGE
'plpgsql';
> CREATE TRIGGER autostamp BEFORE UPDATE ON  FOR EACH ROW
EXECUTE PROCEDURE update_modtime();
>
> The problem with this is, if I do UPDATE  set modtime =
 WHERE  where  is the current
value of modtime, modtime then takes the value of now().  But if I'm
explicitly setting it to its existing value, I don't want the trigger to do
this.  Of course, I could modify my scripts and stuff so that, if I don't
want modtime to change, I don't try to change it.  But I'm not the only
user - and I think the behaviour would be more pleasant for the different
users if modtime always updated to what was specified, if a user was
explicitly setting it.
>
> Thanks for your time,
> Any help greatly appreciated.
> Rajit
>

FAQ