本文讲述Oracle Database 19c 中的 JSON_OBJECT 函数的增强功能。
海沧网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站建设等网站项目制作,到程序开发,运营维护。创新互联公司于2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
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>