3
根據each()
的文檔,它返回setof(key text, value text)
。但是,從SELECT
使用它時,我似乎無法訪問key
或value
。我試過尋找關於record
類型的信息,但我能找到的所有信息都與plpgsql無關。記錄的訪問成員
說我有如下表:
CREATE TABLE mytable (
id SERIAL NOT NULL PRIMARY KEY,
data HSTORE NOT NULL
);
我運行查詢:
SELECT pair
FROM (
SELECT each(data) AS pair
FROM mytable
) AS pairs
我得到record
類型的行:
(key1,value1)
(key2,value2)
...
,如果我嘗試訪問key
或value
,我得到各種錯誤。
此:
SELECT pair.key, pair.value FROM ( SELECT each(data) AS pair FROM mytable ) AS pairs
給出:
ERROR: missing FROM-clause entry for table "pair" LINE 1: pair.key, ^ ********** Error ********** ERROR: missing FROM-clause entry for table "pair" SQL state: 42P01 Character: 8
此:
SELECT pair.key, pair.value FROM ( SELECT each(data) AS pair (key, value) FROM mytable ) AS pairs
給出:
ERROR: syntax error at or near "(" LINE 4: each(attributes) AS pair (key, value) ^ ********** Error ********** ERROR: syntax error at or near "(" SQL state: 42601 Character: 71
此:
SELECT pairs.pair.key, pairs.pair.value FROM ( SELECT each(data) AS pair FROM mytable ) AS pairs
給出:
ERROR: schema "pairs" does not exist ********** Error ********** ERROR: schema "pairs" does not exist SQL state: 3F000
此:
SELECT pair[1], pair[2] FROM ( SELECT each(data) AS pair FROM mytable ) AS pairs
給出:
ERROR: cannot subscript type record because it is not an array ********** Error ********** ERROR: cannot subscript type record because it is not an array SQL state: 42804
如何在PostgreSQL中正確訪問record
類型的成員?
'each'用於'hstore',而不是'record'。 – 2014-09-26 16:49:50
@CraigRinger'each(hstore)'返回'setof record'。 – cpburnz 2014-09-26 16:51:13