本文讲述oracle Database 19c 中的 JSON_OBJECT 函数的增强功能。 1、初始化 本文中的示例使用 SCOTT模式中的 DEPT表,如下: -- DROP&n
本文讲述oracle Database 19c 中的 JSON_OBJECT 函数的增强功能。
1、初始化
本文中的示例使用 SCOTT模式中的 DEPT表,如下:
-- DROP TABLE DEPT PURGE;
CREATE TABLE DEPT (
DEPTNO NUMBER(2) CONSTRaiNT PK_DEPT PRIMARY KEY,
DNAME VARCHAR2(14),
LOC VARCHAR2(13)
);
INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES (30,'SALES','CHICAGo');
INSERT INTO DEPT VALUES (40,'OPERATioNS','BOSTON');
COMMIT;
2、通配符
通配符“*”可用作 jsON_OBJECT 函数的输入,以在单个步骤中引用所有列。使用列名作为键,将每列转换为键:key:value。
SELECT JSON_OBJECT(*) AS json_data
FROM dept;
JSON_DATA
-------------------------------------------------------
{"DEPTNO":10,"DNAME":"ACCOUNTING","LOC":"NEW YORK"}
{"DEPTNO":20,"DNAME":"RESEARCH","LOC":"DALLAS"}
{"DEPTNO":30,"DNAME":"SALES","LOC":"CHICAGO"}
{"DEPTNO":40,"DNAME":"OPERATIONS","LOC":"BOSTON"}
sql>
通配符也可以是表或视图别名的前缀。
SELECT JSON_OBJECT(a.*) AS json_data
FROM dept a;
JSON_DATA
-------------------------------------------------------
{"DEPTNO":10,"DNAME":"ACCOUNTING","LOC":"NEW YORK"}
{"DEPTNO":20,"DNAME":"RESEARCH","LOC":"DALLAS"}
{"DEPTNO":30,"DNAME":"SALES","LOC":"CHICAGO"}
{"DEPTNO":40,"DNAME":"OPERATIONS","LOC":"BOSTON"}
SQL>
3、列列表
可以将逗号分隔的列列表指定为 JSON_OBJECT 函数的输入。在查询中使用的情况下,键名与列表中的列名匹配。以下查询使用小写的列名称,因此输出的键字是小写的。
SELECT JSON_OBJECT(deptno, dname) AS json_data
FROM dept;
JSON_DATA
-------------------------------------------------------
{"deptno":10,"dname":"ACCOUNTING"}
{"deptno":20,"dname":"RESEARCH"}
{"deptno":30,"dname":"SALES"}
{"deptno":40,"dname":"OPERATIONS"}
SQL>
在以下示例中,列名称是首字段大写,因此键名称在输出中也是首字母大写。
SELECT JSON_OBJECT(Deptno, Dname) AS json_data
FROM dept;
JSON_DATA
-------------------------------------------------------
{"Deptno":10,"Dname":"ACCOUNTING"}
{"Deptno":20,"Dname":"RESEARCH"}
{"Deptno":30,"Dname":"SALES"}
{"Deptno":40,"Dname":"OPERATIONS"}
SQL>
4、键值(Key-Value)定义
在以前的版本中,键值对以两种方式之一定义,使用KEY和VALUE关键字,或省略KEY关键字,以下所示:
SELECT JSON_OBJECT(KEY 'deptno' VALUE deptno,
KEY 'dname' VALUE dname) AS json_data
FROM dept;
SELECT JSON_OBJECT('deptno' VALUE deptno,
'dname' VALUE dname) AS json_data
在Oracle 19c中,有一个更短的选项,用“:”代替VALUE关键字。
SELECT JSON_OBJECT('deptno' : deptno,
'dname' : dname) AS json_data
FROM dept;
JSON_DATA
-------------------------------------------------------
{"deptno":10,"dname":"ACCOUNTING"}
{"deptno":20,"dname":"RESEARCH"}
{"deptno":30,"dname":"SALES"}
{"deptno":40,"dname":"OPERATIONS"}
SQL>
5、列别名
您不能在
JSON_OBJECT
函数调用本身中对列进行别名,也不需要这样做,但可以在
WITH
子
句或内联视图中进行别名。
WITH converted_data AS (
SELECT deptno AS "deptnoCol",
dname AS "dnameCol"
FROM dept
)
SELECT JSON_OBJECT(a.*) AS json_data
FROM converted_data a;
JSON_DATA
-------------------------------------------------------
{"deptnoCol":10,"dnameCol":"ACCOUNTING"}
{"deptnoCol":20,"dnameCol":"RESEARCH"}
{"deptnoCol":30,"dnameCol":"SALES"}
{"deptnoCol":40,"dnameCol":"OPERATIONS"}
SQL>SELECT JSON_OBJECT(a.*) AS json_data
FROM (SELECT deptno AS "deptnoCol",
dname AS "dnameCol"
FROM dept) a;
JSON_DATA
-------------------------------------------------------
{"deptnoCol":10,"dnameCol":"ACCOUNTING"}
{"deptnoCol":20,"dnameCol":"RESEARCH"}
{"deptnoCol":30,"dnameCol":"SALES"}
{"deptnoCol":40,"dnameCol":"OPERATIONS"}
SQL>
--结束END--
本文标题: Oracle Database 19c 中的 JSON_OBJECT 函数的增强功能
本文链接: https://lsjlt.com/news/46729.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0