如何在PostgreSQL触发函数中获取表名? [英] How can I get the table name in a PostgreSQL trigger function?
问题描述
我有一个触发功能:
CREATE OR REPLACE FUNCTION "trigger_deleteUsers"()
RETURNS trigger AS
$BODY$
BEGIN
INSERT INTO "DeletedEntities" ("uuidKey", "dateCreated", "dateModified", "dateSynced", "username", "entityName")
VALUES (OLD."uuidKey", OLD."dateCreated", OLD."dateModified", "dateSynced", OLD."username", 'Users');
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql;
CREATE TRIGGER "deleteUsers" AFTER DELETE ON "Users" FOR EACH ROW EXECUTE PROCEDURE "trigger_deleteUsers"();
此功能适用于用户表。每次我从用户表中删除一行时,数据库都会在表 DeletedEntities中插入一行( uuidKey, dateCreated, dateModified, dateSynced, username, entityName)
This works for the table "Users". Every time I delete a row from the "Users" table the database inserts a row with ("uuidKey", "dateCreated", "dateModified", "dateSynced", "username", "entityName") into the table "DeletedEntities" that I will use for syncing purposes later.
上面的作品。这是我的问题,我大约有两打桌子。我知道我需要在每个表上创建触发器,但是我不想为每个表创建自定义触发函数。与上面的第一个函数相比,唯一会发生变化的是该函数中INSERT语句中的最后一个值。
The above works. Here's my problem I have about two dozen tables. I know I need to CREATE TRIGGER on each table, but I don't want to have to create a custom trigger function for each table. The only thing that would change from first function above is the last value in the INSERT statement within the function; instead of 'Users' it would be "Ledgers", or "Journal", or whatever.
在PostgreSQL触发函数中,如何获取表的名称,而不是用户。
Within a PostgreSQL trigger function, how do I get the name of the table that the OLD row belongs too?
推荐答案
TG_TABLE_NAME。有关其他触发器参数,请参阅文档: http://www.postgresql.org/docs/current/ static / plpgsql-trigger.html
TG_TABLE_NAME. See the docs for other trigger arguments: http://www.postgresql.org/docs/current/static/plpgsql-trigger.html
这篇关于如何在PostgreSQL触发函数中获取表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!