返回表名称变为字符串

时间:2016-09-10 13:32:59

标签: postgresql

你好我公开了500个表,我尝试为所有表创建触发器,首先查找所有表名并创建一个触发器,如果​​还没有存在则代码:

  CREATE OR REPLACE FUNCTION get_all_results() RETURNS VOID AS
  $BODY$
   DECLARE
 r RECORD;
BEGIN
 IF (SELECT "session_user"() <> 'asdf' THEN
 FOR r IN (SELECT 'CREATE TRIGGER ' || tab_name|| '_if_modified_trg AFTER INSERT OR UPDATE OR DELETE ON ' || tab_name|| ' FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func(); ' AS trigger_creation_query
 FROM (
 SELECT  quote_ident(table_name) as tab_name
FROM information_schema.tables
WHERE table_schema='public'
AND table_type != 'VIEW'


)  AS foo WHERE tab_name||'_if_modified_trg' NOT IN (SELECT tgname from pg_trigger where not tgisinternal))

 LOOP

-- can do some processing here
EXECUTE r.trigger_creation_query;
END LOOP;
RETURN;
END IF;
END
$BODY$

LANGUAGE plpgsql;
 SELECT * FROM get_all_results();

我收到此错误:

   ERROR:  syntax error at or near "_if_modified_trg"
   LINE 1: CREATE TRIGGER "position"_if_modified_trg AFTER INSERT OR UP...

我确实在这500个中的一个中有位置,名称不是&#34;位置&#34;。

0 个答案:

没有答案