我试图找到一种简单的方法来生成一个SQL查询作为JSON,但我发现空白。这是查询:
SELECT DISTINCT owner
FROM dba_segments
WHERE owner NOT IN ('SYS', 'SYSTEM', 'EXFSYS', 'DBSNMP', 'SQLTXPLAIN', 'XDB', 'WMSYS', 'PERFSTAT', 'OUTLN');
我希望这个显示器是json类似于:
{
"data": [{
"{#Owner}": "ABC123Test"
}, {
"{#Owner}": "Accounting"
}, {
"{#Owner}": "Treasury"
}, {
"{#Owner}": "Production1"
}]
}
在SQL Server中,我只使用XML PATH并执行类似的操作 -
SELECT '{"data":[' + (SELECT CAST(STUFF((SELECT ',' + '{"{#Owner}":"' + DISTINCT[owner] + '"}'
FROM dba_segments
where owner not in ('SYS', 'SYSTEM', 'EXFSYS', 'DBSNMP', 'SQLTXPLAIN', 'XDB', 'WMSYS', 'PERFSTAT', 'OUTLN') FOR XML PATH('')), 1, 1, '') AS VARCHAR(MAX))) + ']}' as 'XML_F52E2B61-18A1-11d1-B105-00805F49916B'
答案 0 :(得分:1)
这样的事情:
SELECT '{ "data": [{'||listagg('"{#owner}":"'||owner||'"}', ',') within group (order by owner)||'}]}' as json
FROM (
select distinct owner
from dba_segments
WHERE owner NOT IN ('SYS', 'SYSTEM', 'EXFSYS', 'DBSNMP', 'SQLTXPLAIN', 'XDB', 'WMSYS', 'PERFSTAT', 'OUTLN', 'APEX_040200')
) t;
(不确定我是否嵌套了所有{
和[
权利
答案 1 :(得分:1)
使用12.2.0.1.0这是有效的(注意我必须使用实现WITH子句来获得正确的结果)
SQL> set lines 89
SQL> with DBA_SEGMENT_OWNERS
2 as
3 (
4 select /*+ MATERIALIZE */ DISTINCT OWNER
5 from dba_segments
6 where owner NOT IN ('SYS', 'SYSTEM', 'EXFSYS', 'DBSNMP', 'SQLTXPLAIN', 'XDB', 'WMSYS', 'PERFSTAT')
7 )
8 select JSON_OBJECT('data' is JSON_ARRAYAGG(JSON_OBJECT('{#Owner}' is OWNER)))
9 from DBA_SEGMENT_OWNERS
10 /
{"data":[{"{#Owner}":"MDSYS"},{"{#Owner}":"VPDTEST"},{"{#Owner}":"CTXSYS"},{"{#Owner}":"H
R"},{"{#Owner}":"DVSYS"},{"{#Owner}":"AUDSYS"},{"{#Owner}":"SCOTT"},{"{#Owner}":"VPDOWNER
"},{"{#Owner}":"GSMADMIN_INTERNAL"},{"{#Owner}":"OJVMSYS"},{"{#Owner}":"ORDSYS"},{"{#Owne
r}":"ORDS_METADATA"},{"{#Owner}":"ORDDATA"},{"{#Owner}":"XDBEXT"},{"{#Owner}":"LBACSYS"},
{"{#Owner}":"XFILES"}]}
SQL>
答案 2 :(得分:0)