plpgsql如何将查询结果插入表中?

时间:2015-07-26 12:30:07

标签: sql postgresql

我的查询A返回值integer, numeric, integer。 和表格B

   (id integer,
    weight numeric,
    price integer
    )

查询返回许多行。我想直接将这些行插入BB没有也不需要PK ...

CREATE OR REPLACE FUNCTION func()
  RETURNS void AS

$BODY$
begin

      query A
      insert to B?

      continue function operation
end;
$BODY$
  LANGUAGE plpgsql VOLATILE  

我知道如下:

for row in query A
loop insert into B

但我找不到合适的语法

1 个答案:

答案 0 :(得分:1)

您可以这样做:

insert into b(id, weight, price)
    select id, weight, price  -- or whatever the column names are
    from A;

insert . . . select的语法不会改变,因为你在一个函数中。