1
使用PostgreSQL 9.2,我定義了一個函數,它接受一個參數並返回一個多列表。我想將該函數應用於CTE行中的多個參數。我得到的結果是一列元組,而不是我想要的多列。即:從記錄類型的單列中分離/提取值,將用戶定義的函數應用於多行CTE
sandbox=# CREATE FUNCTION myfunc(arg int) RETURNS table(col1 int, col2 int) AS
sandbox-# 'SELECT arg+1, arg+2'
sandbox-# LANGUAGE SQL;
CREATE FUNCTION
sandbox=# select * from myfunc(1);
col1 | col2
------+------
2 | 3
(1 row)
sandbox=# WITH rows AS (
sandbox(# SELECT 1 AS arg UNION SELECT 2 AS arg
sandbox(#)
sandbox-# SELECT * FROM (SELECT myfunc(arg) FROM rows) x;
myfunc
--------
(2,3)
(3,4)
(2 rows)
鑑於CTE,下面的兩個SELECT
條款相同的評估,以對方爲表record
類型的單個列:
SELECT myfunc(arg) FROM rows;
SELECT * FROM (select myfunc(arg) FROM rows) x;
而且下面三個都失敗(CTE未示出):
SELECT myfunc(arg).col1 FROM rows;
ERROR: syntax error at or near "."
LINE 4: SELECT myfunc(arg).col1 FROM rows;
^
SELECT col1 FROM (select myfunc(arg) FROM rows) x;
ERROR: column "col1" does not exist
LINE 4: SELECT col1 FROM (select myfunc(arg) FROM rows) x;
^
SELECT col1 FROM (SELECT * FROM (select myfunc(arg) FROM rows) x) y;
ERROR: column "col1" does not exist
LINE 4: SELECT col1 FROM (SELECT * FROM (select myfunc(arg) FROM row...
^
如何獲取包含單列元組中的值的多列表?
如果您有postgres 9.3 –