【问题标题】:plpgsql how to insert results of query into table?plpgsql如何将查询结果插入表中?
【发布时间】:2015-07-26 20:38:51
【问题描述】:

我有一个查询 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

但我找不到正确的语法

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    你会做这样的事情:

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多