Oracle Database 19c 中的 JSON_OBJECT 函式的增強功能
本文講述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>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29337971/viewspace-2654283/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle Database 19c中的自動索引OracleDatabase索引
- Oracle 19c中基於函式的索引Oracle函式索引
- Python巢狀定義函式增強reduce()函式功能Python巢狀函式
- Oracle 19c Concepts(01):Introduction to Oracle DatabaseOracleDatabase
- Oracle 19c Concepts(13):Oracle Database InstanceOracleDatabase
- Oracle 19c Database Management ToolsOracleDatabase
- Sqlcl 連線Oracle DataBase 19cSQLOracleDatabase
- 1 Oracle Database 19c 新特性OracleDatabase
- Oracle database 19c中獲取當前資料庫版本的方法OracleDatabase資料庫
- Oracle 19c Concepts(00):Changes in This Release for Oracle Database ConceptsOracleDatabase
- Oracle 19c Concepts(18):Concepts for Database AdministratorsOracleDatabase
- Oracle 19c Concepts(19):Concepts for Database DevelopersOracleDatabaseDeveloper
- Oracle Database 19c安裝Sample SchemasOracleDatabase
- Oracle中Decode()函式的使用Oracle函式
- Oracle 19c Concepts(17):Topics for Database Administrators and DevelopersOracleDatabaseDeveloper
- Disable Database Audit In Oracle 19c RAC-20220111DatabaseOracle
- Oracle 19c Database Configure the HTTPS Port for EM ExpressOracleDatabaseHTTPExpress
- 【kingsql分享】Oracle Database 19c的各種新特性介紹SQLOracleDatabase
- 6、Oracle中的分組函式Oracle函式
- Oracle 19c中的TomcatOracleTomcat
- Oracle Database 19c(19.9) RAC On RedHat 8.3 Using VirtualBox and MacBookOracleDatabaseRedhatMac
- Oracle 19c DBA's Guide(01): Getting Started with Database AdministrationOracleGUIIDEDatabase
- Mybatis 中如何優雅的增強日誌功能?MyBatis
- Oracle中的正規表示式(及函式)詳解Oracle函式
- 使用免費的Oracle雲服務-在雲主機上安裝Oracle Database 19cOracleDatabase
- 【Oracle的NVL函式用法】Oracle函式
- excel最常用的八個函式彙總 excel中各函式的用途功能Excel函式
- Oracle中pivot函式詳解Oracle函式
- 工作中,Oracle常用函式Oracle函式
- oracle interval日期函式的bug!Oracle函式
- Oracle的LAST_DAY函式OracleAST函式
- Ubuntu在Vbox中安裝增強功能Ubuntu
- Oracle 遷移到 OB 過程中的函式改造案例Oracle函式
- oracle 函式Oracle函式
- oracle or 函式Oracle函式
- Oracle 19c透過recover standby database from service修復GAP案例OracleDatabase
- Oracle 19c 新特性:ADG的自動DML重定向增強讀寫分離--ADG_REDIRECT_DMLOracle
- Oracle的SYS_CONNECT_BY_PATH函式Oracle函式