2015-12-09 65 views
0

我在之前的post中发布的参考解决方案导致了另一种情况。 试图插入我的目标表(如下图所示)。从Postgres中的动态查询插入

-- 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 
) 

使用像

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

结果查询到的错误:

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:

什么是从这个结果插入到表中适当的方式。

+1

选择gen_Test( '英语')的检查输出;它是否返回相同的各列account_number,currency,trade_date?。我认为你已经定义了它的功能。你可以在函数本身写入插入。 – compyutech

+0

@compyutech select gen_Test('english')是/将返回'english',因为这个名字传递给游标。关于在函数**中有插入的推荐**可以在函数结果为静态或仅用于一个表的情况下。这个函数被设计为动态查询任何具有任何列的表的查询结果,并进一步将其用于插入到目的地。根据您对100个表的建议,需要为每个表创建100个函数。 –

+0

您对我的建议的看法很好。但是我对你的函数返回结果有困惑,如果它没有以相同的列顺序返回数据,我们不能在目标表中插入。你可以分享你的功能创建声明?所以我可以从我身边检查一次。 – compyutech

回答

0

你可以尝试像这样:

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

SELECT foo。* FROM gen_Test('english')as foo;只返回“英语”。这不是一个表值函数,而是一个调用其中的游标的函数,并将游标名作为参数传递。 –