2015-12-09 60 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;只返回「英語」。這不是一個表值函數,而是一個調用其中的遊標的函數,並將遊標名作爲參數傳遞。 –