遍历所有用户表并在每个表中插入行
Loop through all user tables and insert row in each
出于某种原因,我就是想不通。我在 PostgreSQL 中有一个单独的模式,用于为连接到服务器的每个用户提供与 table 相关的通知。我的计划是让每个用户创建一个 TEMP table 以接收来自 Xojo 的额外通知信息,因为 Xojo 不支持 PostgreSQL 负载。
我觉得我开始接近了,所以我将 post 我的触发函数中的代码。
DECLARE
my_table RECORD;
BEGIN
FOR my_table IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'information_schema'
LOOP
INSERT INTO my_table.table_name (effected_row_id)
VALUES (NEW.effected_row_id);
END LOOP;
END;
如果我错了请告诉我,但我相信我的主要问题是弄清楚如何在 INSERT 语句中使用从 SELECT 语句返回的 table 名称。
编辑:
这是我当前的触发函数
-- Function: notification.my_insert_trigger_function()
-- DROP FUNCTION notification.my_insert_trigger_function();
CREATE OR REPLACE FUNCTION notification.my_insert_trigger_function()
RETURNS trigger AS
$BODY$DECLARE
my_table RECORD;
BEGIN
FOR my_table IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'notification' AND table_name <> 'notification_global' AND table_name <> 'switcher'
LOOP
EXECUTE(FORMAT($f$
INSERT INTO %s (effected_row_username)
VALUES (%s);
$f$, 'notification.' || my_table.table_name, NEW.effected_row_username));
END LOOP;
RETURN new;
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION notification.my_insert_trigger_function()
OWNER TO serveradmin;
您需要在触发函数中使用 dynamic commands。
函数 format()
通常很有帮助。
DECLARE
my_table RECORD;
BEGIN
FOR my_table IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'information_schema'
LOOP
EXECUTE(FORMAT($f$
INSERT INTO %s (effected_row_id)
VALUES (%s);
$f$, my_table.tablename, NEW.effected_row_id));
END LOOP;
END;
出于某种原因,我就是想不通。我在 PostgreSQL 中有一个单独的模式,用于为连接到服务器的每个用户提供与 table 相关的通知。我的计划是让每个用户创建一个 TEMP table 以接收来自 Xojo 的额外通知信息,因为 Xojo 不支持 PostgreSQL 负载。
我觉得我开始接近了,所以我将 post 我的触发函数中的代码。
DECLARE
my_table RECORD;
BEGIN
FOR my_table IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'information_schema'
LOOP
INSERT INTO my_table.table_name (effected_row_id)
VALUES (NEW.effected_row_id);
END LOOP;
END;
如果我错了请告诉我,但我相信我的主要问题是弄清楚如何在 INSERT 语句中使用从 SELECT 语句返回的 table 名称。
编辑: 这是我当前的触发函数
-- Function: notification.my_insert_trigger_function()
-- DROP FUNCTION notification.my_insert_trigger_function();
CREATE OR REPLACE FUNCTION notification.my_insert_trigger_function()
RETURNS trigger AS
$BODY$DECLARE
my_table RECORD;
BEGIN
FOR my_table IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'notification' AND table_name <> 'notification_global' AND table_name <> 'switcher'
LOOP
EXECUTE(FORMAT($f$
INSERT INTO %s (effected_row_username)
VALUES (%s);
$f$, 'notification.' || my_table.table_name, NEW.effected_row_username));
END LOOP;
RETURN new;
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION notification.my_insert_trigger_function()
OWNER TO serveradmin;
您需要在触发函数中使用 dynamic commands。
函数 format()
通常很有帮助。
DECLARE
my_table RECORD;
BEGIN
FOR my_table IN
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'information_schema'
LOOP
EXECUTE(FORMAT($f$
INSERT INTO %s (effected_row_id)
VALUES (%s);
$f$, my_table.tablename, NEW.effected_row_id));
END LOOP;
END;