Postgresql triggers

Is it possible to see the last entry into a table that was triggered on?

For instance, I have a table called "users" and a table called "log". I want to write to "log" the userid that was last written to "users" so each time a user is created, it writes the userid to a log.

I create a trigger on users with a function like the one below...

CREATE OR REPLACE FUNCTION process_learner_creater() RETURNS TRIGGER AS $log$ BEGIN IF (TG_OP = 'INSERT') THEN INSERT INTO log SELECT userid from users; RETURN NULL; END IF; RETURN NULL; END; $log$ LANGUAGE plpgsql;

So when an insert is fired onto the users table it will return the userid from users, the problem is I don't know what was the last user to be added so this function will not work...

Is there a way to say...

INSERT INTO log SELECT <> from users;

This field in users would be a serial field so would auto-increment (and not be part of the the SQL script in the insert as this would be auto-assigned).


From what I understand, you want to use the NEW variable, which contains the new row for which the trigger was invoked (you don't have to SELECT for it). Have you looked at the examples from the documentation?

http://www.postgresql.org/docs/8.4/interactive/plpgsql-trigger.html

链接地址: http://www.djcxy.com/p/93950.html

上一篇: Postgresql,如果行存在一些唯一值,则更新,否则插入

下一篇: Postgresql触发器