SELECT ID,
'['||
LISTAGG ('{' || col || ':' || VALUE || '}', ',')
WITHIN GROUP (ORDER BY ROWNUM)
|| ']' key_value_pair
FROM (SELECT id,Name, TO_CHAR (age) age, Country FROM yourtable)
UNPIVOT (VALUE FOR col IN (Name, Age, Country))
GROUP BY id;
DECLARE
TYPE KEYVALUEPAIR
IS
RECORD
(
KEY_ VARCHAR2(12),
VALUE_ VARCHAR2(12) );
TYPE RECTYPE IS VARRAY(10) OF KEYVALUEPAIR;
RECID1 RECTYPE;
BEGIN
SELECT col,
VALUE BULK COLLECT
INTO RECID1
FROM
(SELECT id, Name, TO_CHAR (age) age, Country FROM yourtable WHERE ID = 1
) UNPIVOT (VALUE FOR col IN (Name, Age, Country)) ;
END;
/
select id,
json_array( json_object('name' value name)
, json_object('age' value age)
, json_object('country' value country)) key_values
from
( select 1 id, 'AAAA' name, 12 age, 'AU' country from dual
union all
select 2 id, 'BBBB' name, 13 age, 'CC' country from dual ) a
3条答案
按热度按时间knsnq2tg1#
可以使用
UNPVIVOT
和LISTAGG
。输出
演示
你说你有一个
TYPE KEYVALUEPAIR<VARCHAR,VARCHAR>
。你想用这种东西吗?yzckvree2#
我不确定你的主要要求是使用VARARY集合还是使用你提到的格式的数据。
如果你需要使用VARRAY集合,这里有一个例子:
qjp7pelc3#