How to change the schema by arg text in postgres procedure

我只是一个虾纸丫 提交于 2021-02-17 03:37:53

问题


CREATE OR REPLACE FUNCTION newfunction (Schema1 text, Schema2 text)
RETURNS integer
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$ 
.
.
.
insert into [Schema1].table (name,phone,address,......)
select name,phone,address,..... from [Schema2].table where....;

I want to change the schema by arg text is this possible to do this?


回答1:


You'll have to use dynamic SQL throughout:

EXECUTE
   format(
      E'INSERT INTO %I.tab (...)\n'
      'SELECT ... FROM %I.tab WHERE ...',
      schema1,
      schema2
   );


来源:https://stackoverflow.com/questions/58636295/how-to-change-the-schema-by-arg-text-in-postgres-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!