简体   繁体   English

从Postgres中的动态查询插入

[英]Insert from Dynamic Query in Postgres

With reference solution I've posted in my previous post resulted in one more situation. 使用我以前的文章中发布的参考解决方案,导致了另外一种情况。 While trying to insert into my destination table(schema as below). 尝试插入我的目标表时(如下图所示)。

-- Table: normalized_transaction

-- DROP TABLE normalized_transaction;

CREATE TABLE normalized_transaction
(
  transaction_id uuid,
  file_id uuid,
  account_number character varying(40),
  currency character varying(3),
  trade_date date,
  value_date date,
  narration character varying(200),
  amount numeric,
  mesitis_account_number character varying(50),
  tag character varying(255),
  supporting_file_id uuid,
  supporting_record_id uuid,
  status integer DEFAULT 0,
  source_type integer,
  record_index integer DEFAULT 0
)

with using a query like 使用类似的查询

INSERT INTO normalized_transaction(account_number, currency, trade_date)
select gen_Test('english');
fetch all in english;

Result into error: 结果出错:

ERROR:  INSERT has more target columns than expressions
LINE 2: ...NSERT INTO normalized_transaction(account_number, currency, ...
                                                             ^
********** Error **********

ERROR: INSERT has more target columns than expressions
SQL state: 42601
Character: 53



select gen_Test('english');
    fetch all in english;

Just for Reference Output of above Query: 仅用于上述查询的参考输出:

What is appropriate way to insert the result from this into table. 将结果从表中插入的合适方法是什么。

You could try like so: 您可以这样尝试:

INSERT INTO normalized_transaction(account_number, currency, trade_date)
SELECT foo.*
FROM gen_Test('english') as foo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM