英文:
confused by oracle error msg : PL/SQL: ORA-25137: Data value out of range
问题
在Oracle 21c中执行以下代码时报告了一个错误(PL/SQL: ORA-25137: 数据值超出范围)。
create table t(id int ,j json);
insert into t values(1,'{"key":"value"}');
DECLARE
b varchar2(100);
BEGIN
select cast(j as varchar2(100)) into b from t where id=1;
DBMS_OUTPUT.PUT_LINE(b);
END;
错误消息如下:
错误行 4:
ORA-06550: 行 4, 列 15:
PL/SQL: ORA-25137: 数据值超出范围
ORA-06550: 行 4, 列 3:
PL/SQL: SQL 语句被忽略
我使用的Oracle版本是
Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0
我无法理解为什么会报告这个错误。我认为我设置的转换目标大小足够大,但却报告了"ORA-25137: 数据值超出范围"。有人能解释为什么吗?
英文:
An error(PL/SQL: ORA-25137: Data value out of range ) was reported when I executed the following code in Oracle 21c.
create table t(id int ,j json);
insert into t values(1,'{"key":"valus"}');
DECLARE
b varchar2(100);
BEGIN
select cast(j as varchar2(100)) into b from t where id=1;
DBMS_OUTPUT.PUT_LINE(b);
END;
The error message is
ERROR at line 4:
ORA-06550: line 4, column 15:
PL/SQL: ORA-25137: Data value out of range
ORA-06550: line 4, column 3:
PL/SQL: SQL Statement ignored
The oracle version I used is
> Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0
I can't understand why this error was reported.
I think the cast target size I set is enough, but "ORA-25137: Data value out of range" was reported.
Can anyone explain why?
答案1
得分: 3
数据以二进制格式存储在表中,因此您应该使用 JSON_SERIALIZE 函数将 JSON 从任何支持的类型转换为文本。该函数从 19c 版本开始提供:
Select t.id, json_serialize(t.j) From tbl t;
id j
---- -------------------
1 {"key":"value"}
或者,对于较早版本,可以使用一些 JSON 函数,例如:
Select t.id, json_value(t.j, '$.key') as json_key From tbl t;
id json_key
---- -------------------
1 value
还有更多的函数和选项可用于使用 JSON 数据类型的 SQL 查询(例如,json_query()、json_table()等等)。
您可以在此处找到更多信息:https://oracle-base.com/articles/21c/json-data-type-21c
英文:
The data is stored in the table in binary format so you should use JSON_SERIALIZE function to convert JSON from any supported type into text. It is available since 19c:
Select t.id, json_serialize(t.j) From tbl t;
id j
---- -------------------
1 {"key":"valus"}
or, for previous versions use some of json functions like:
Select t.id, json_value(t.j, '$.key') as json_key From tbl t;
id json_key
---- -------------------
1 valus
There are more functions and options to get your data using sql with json datatype (json_query(), json_table(), ...)
You can find more about it -- https://oracle-base.com/articles/21c/json-data-type-21c
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论