【发布时间】:2016-07-11 18:25:48
【问题描述】:
在 Pl/Python 中,“RETURNS setof”或“RETURNS table”子句用于返回类似于结构化数据的表。在我看来,必须提供每列的名称才能返回表。如果您有一个包含几列的表格,这很容易。但是,如果您有一个包含 200 列的表,那么最好的方法是什么?我必须输入所有列的名称(如下所示)还是有办法绕过它?任何帮助将非常感激。
以下是使用“RETURNS table”子句的示例。代码 sn-ps 在 postgres 中创建一个表 (mysales),填充它,然后使用 Pl/Python 获取它并返回列值。为简单起见,我只从表中返回 4 列。
DROP TABLE IF EXISTS mysales;
CREATE TABLE mysales (id int, year int, qtr int, day int, region
text) DISTRIBUTED BY (id);
INSERT INTO mysales VALUES
(1, 2014, 1,1, 'north america'),
(2, 2002, 2,2, 'europe'),
(3, 2014, 3,3, 'asia'),
(4, 2010, 4,4, 'north-america'),
(5, 2014, 1,5, 'europe'),
(6, 2009, 2,6, 'asia'),
(7, 2002, 3,7, 'south america');
DROP FUNCTION IF EXISTS myFunc02();
CREATE OR REPLACE FUNCTION myFunc02()
RETURNS TABLE (id integer, x integer, y integer, s text) AS
$$
rv = plpy.execute("SELECT * FROM mysales ORDER BY id", 5)
d = rv.nrows()
return ( (rv[i]['id'],rv[i]['year'], rv[i]['qtr'], rv[i]['region'])
for i in range(0,d) )
$$ LANGUAGE 'plpythonu';
SELECT * FROM myFunc02();
#Here is the output of the SELECT statement:
1; 2014; 1;"north america"
2; 2002; 2;"europe"
3; 2014; 3;"asia"
4; 2010; 4;"north-america"
5; 2014; 1;"europe"
6; 2009; 2;"asia"
7; 2002; 3;"south america"
【问题讨论】:
-
DISTRIBUTED BY仅适用于 Greenplum。 -
正确。谢谢。
标签: python postgresql greenplum