我有一个 JSON table,如何在 PLSQL 中使用此信息创建一个 sql table?
I have a JSON table, how can I create a sql table with this information in PLSQL?
我正在使用 PLSQL 我有这个 JSON table (第一个变量),
如何将此数据插入 sql table,我已经尝试过了,但我有 SQL 语句被忽略的错误:
CREATE TABLE Prueba_ins_json (
userId number(3),
id number(3),
title VARCHAR2(200),
body VARCHAR2(800)
);
DECLARE
json_prueba VARCHAR(5000) := '[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
},
{
"userId": 1,
"id": 3,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
"body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
},
{
"userId": 1,
"id": 4,
"title": "eum et est occaecati",
"body": "ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"
}
]';
BEGIN
INSERT INTO Prueba_ins_json
SELECT * FROM JSON_TABLE( json_prueba, '$[*]'
columns(
userId, id, title, body
)
);
END;
只需要提供PATH
(最好连同数据类型)子句如
INSERT INTO Prueba_ins_json
SELECT *
FROM JSON_TABLE(json_prueba,
'$[*]' COLUMNS(
userId INT PATH '$.userId',
id INT PATH '$.id',
title VARCHAR2(200) PATH '$.title',
body VARCHAR2(800) PATH '$.body'
)
);
编辑(因为您使用的是 DB 11g):
您也可以使用 XMLTABLE()
和 APEX_JSON.TO_XMLTYPE()
代替,例如
INSERT INTO Prueba_ins_json
WITH t(jsCol) AS
(
SELECT '<your_JSON_value>'
FROM dual
)
SELECT userId, id, title, body
FROM t,
XMLTABLE('/json/row'
PASSING APEX_JSON.TO_XMLTYPE(jsCol)
COLUMNS
userId INT PATH 'userId',
id INT PATH 'id',
title VARCHAR2(200) PATH 'title',
body VARCHAR2(800) PATH 'body'
)
我正在使用 PLSQL 我有这个 JSON table (第一个变量), 如何将此数据插入 sql table,我已经尝试过了,但我有 SQL 语句被忽略的错误:
CREATE TABLE Prueba_ins_json (
userId number(3),
id number(3),
title VARCHAR2(200),
body VARCHAR2(800)
);
DECLARE
json_prueba VARCHAR(5000) := '[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
},
{
"userId": 1,
"id": 3,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
"body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
},
{
"userId": 1,
"id": 4,
"title": "eum et est occaecati",
"body": "ullam et saepe reiciendis voluptatem adipisci\nsit amet autem assumenda provident rerum culpa\nquis hic commodi nesciunt rem tenetur doloremque ipsam iure\nquis sunt voluptatem rerum illo velit"
}
]';
BEGIN
INSERT INTO Prueba_ins_json
SELECT * FROM JSON_TABLE( json_prueba, '$[*]'
columns(
userId, id, title, body
)
);
END;
只需要提供PATH
(最好连同数据类型)子句如
INSERT INTO Prueba_ins_json
SELECT *
FROM JSON_TABLE(json_prueba,
'$[*]' COLUMNS(
userId INT PATH '$.userId',
id INT PATH '$.id',
title VARCHAR2(200) PATH '$.title',
body VARCHAR2(800) PATH '$.body'
)
);
编辑(因为您使用的是 DB 11g):
您也可以使用 XMLTABLE()
和 APEX_JSON.TO_XMLTYPE()
代替,例如
INSERT INTO Prueba_ins_json
WITH t(jsCol) AS
(
SELECT '<your_JSON_value>'
FROM dual
)
SELECT userId, id, title, body
FROM t,
XMLTABLE('/json/row'
PASSING APEX_JSON.TO_XMLTYPE(jsCol)
COLUMNS
userId INT PATH 'userId',
id INT PATH 'id',
title VARCHAR2(200) PATH 'title',
body VARCHAR2(800) PATH 'body'
)