PL/SQL错误消息:ORA-25137:数据值超出范围。

huangapple go评论91阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月19日 10:15:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497595.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定