如何使用 for 循环 select 字段值并将它们插入另一个 table
How to select field values and insert them in another table with for loop
我想在 Postgres 中编写一个函数,其中 select 来自 table A 的列然后循环遍历值并将它们插入到 table B.
这是我目前的情况:
declare
aid integer
begin
for aid in select a_id from table_a
loop
insert into table_b (a_id)
values (aid)
end loop;
end;
但是,我在第 for aid in select a_id from table_a
行遇到语法错误。正确的做法是什么?
您不需要循环 - 只需插入-select 语句:
INSERT INTO table_b (a_id)
SELECT a_id FROM table_a
我想在 Postgres 中编写一个函数,其中 select 来自 table A 的列然后循环遍历值并将它们插入到 table B.
这是我目前的情况:
declare
aid integer
begin
for aid in select a_id from table_a
loop
insert into table_b (a_id)
values (aid)
end loop;
end;
但是,我在第 for aid in select a_id from table_a
行遇到语法错误。正确的做法是什么?
您不需要循环 - 只需插入-select 语句:
INSERT INTO table_b (a_id)
SELECT a_id FROM table_a