除了 - 只需要通過與臺限定它們的歧義查詢中的表列名稱。試試這個(希望重新格式化,以提高可讀性):
CREATE OR REPLACE FUNCTION test_proc4(sr_num bigint)
RETURNS TABLE(sr_number bigint, product_serial_number varchar(35)) AS $$
BEGIN
RETURN QUERY
SELECT
temp_table.sr_number, temp_table.product_serial_number
from temp_table
where temp_table.sr_number=sr_num;
END;
$$ LANGUAGE 'plpgsql' VOLATILE;
警告:我只在PG 9.4測試這一點,所以在您的版本沒有測試它,但(這是不再支持,我可以補充)。用我用虛擬數據填充表
CREATE OR REPLACE FUNCTION test_proc4(sr_num bigint)
RETURNS TABLE(sr_number bigint, product_serial_number varchar(35)) AS $$
BEGIN
FOR sr_number, product_serial_number IN
SELECT
temp_table.sr_number, temp_table.product_serial_number
from temp_table
where temp_table.sr_number=sr_num
LOOP
RETURN NEXT;
END LOOP;
RETURN;
END;
$$ LANGUAGE 'plpgsql' VOLATILE;
一個小的完整性檢查:
postgres=# select * from temp_table;
sr_number | product_serial_number
-----------+-----------------------
1 | product 1
2 | product 2
2 | another product 2
(3 rows)
postgres=# select * from test_proc4(1);
sr_number | product_serial_number
-----------+-----------------------
1 | product 1
(1 row)
postgres=# select * from test_proc4(2);
sr_number | product_serial_number
-----------+-----------------------
2 | product 2
2 | another product 2
(2 rows)
它如果有關於您的版本和9.4之間PLPGSQL執行問題,你可以嘗試這種形式作爲替代給我同樣的錯誤'返回不能有函數返回集中的參數;請在「QUERY」處或其附近使用RETURN NEXT' – user2569524 2015-01-15 18:02:49
@ user2569524因此,您的PG版本可能會略微不同於PLPGSQL(特別是'RETURN QUERY')的支持/實現。我在我的回答中添加了一個備用表單。 – rchang 2015-01-15 18:25:20