我在 pgadmin4 中创建了一个脚本。该脚本包括查询表。
在这个表中,我有一个包含 value1 | 的元素。值2 | valueX(元素的数量可能因行而异)。
在 pgadmin4 中,我使用了这个脚本:
#!/bin/bash
psql "postgresql://id:[email protected]/table" << EOF
do $$
DECLARE
_id int;
_name text;
_value text;
begin
FOR _id, _name IN select id, unnest(string_to_array(themes, '|')) from data LOOP
if EXISTS (select id_theme from theme where uri_theme = concat('<',_name,'>')) then
insert into data_themes(data_id, theme_id) values (_id, (select id_theme from theme where uri_theme = concat('<',_name,'>')) );
RAISE NOTICE 'test % / %', _id, _name;
end if;
end loop;
end;
$$;
EOF
该脚本在 pgadmin4 中按我的意愿工作。
但是,当我想在 bash 脚本中运行这个脚本时,它给了我一个错误
警告:没有正在进行的事务提交
它停在循环的中间(大约 25,000 行)并向我显示错误。
我把这个:
\echo :AUTOCOMMIT
\set AUTOCOMMIT off
\echo :AUTOCOMMIT
我不明白为什么该脚本适用于 pgadmin 而不适用于 bash 脚本。谢谢你的帮助
我不明白为什么该脚本适用于 pgadmin 而不适用于 bash 脚本。
因为您有效地运行了不同的脚本。$$
是 中的一个变量bash
。在 here-document ( << EOF ... EOF
) 中的变量被扩展。因此,您运行类似
do 1234
....
1234;
要解决此问题,请引用此处的文档:
psql "postgresql://id:[email protected]/table" << 'EOF'
do $$
...
$$;
EOF
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句