postgres存储过程来更新多个表

时间:2016-04-12 10:27:27

标签: postgresql stored-procedures postgresql-9.4

我正在编写一个存储过程来更新Postgres 9.4中的多个表格数据。我将从python执行此过程。

CREATE OR REPLACE FUNCTION update_data(
    list_table_name TEXT[],
    list_parameters TEXT[]
)
RETURNS BOOLEAN LANGUAGE plpgsql SECURITY DEFINER AS $$

BEGIN
FOR i IN list_table_name LOOP
  FOR j IN dict_parameters LOOP
    EXECUTE '
   UPDATE ' || i || '
   SET    default_value = ' || j.param_val || '
   WHERE  ' || i.id = j.id ||''
  END LOOP;
END LOOP;
END;
$$
LANGUAGE 'plpgsql';
-- dict_parameters would be python list of dictionary [{"id":val,"param_val":[val1,val2,val3]},{"id":val,"param_val":[val1,val2,val3]}]
-- list_table_name would be python list containing table names.

首先,这给了我错误 -

ERROR:  syntax error at or near "BEGIN"
LINE 8: BEGIN
        ^
optimation=# END LOOP;
ERROR:  syntax error at or near "LOOP"
LINE 1: END LOOP;
            ^

即使它运行,我也不确定我是否以正确的方式提取值。如果有人可以指导我。

0 个答案:

没有答案