使用SYS_CONNECT_BY_PATH函数时出现Oracle ORA-30004错误

11

使用SYS_CONNECT_BY_PATH函数时出现ORA-30004错误,不能将分隔符作为列的一部分。

解决方法:使用另一个在任何列值中不存在的分隔符,然后重试。

错误位置:

select ...
Sys_Connect_By_Path(myVariable || ':' || mySecondVariable, ' --> ') "myNewVar",
...

有效:

select ...
Sys_Connect_By_Path(myVariable || ':' || mySecondVariable, ' -> ') "myNewVar",
...

我们在数据中发现了这样的文本:

  • 一些文本 B--更多文本
  • 一些文本 A--更多文本

由于数据中没有 '-->' 或者说没有 '-->',那么为什么第一个会出错?而第二个则在前后有空格。


是指 --> 前后的空格吗? - hol
是的 - 我认为之前的程序员试图添加一个空格-->空格。 - Nathan Stanford
你是不是打错了,应该是 SomeText B-->More Text 而不是 SomeText B--More Text?因为这样就很明显:前后空格不相等。 - hol
1个回答

13

这是因为---->分隔符的一部分,但不是->分隔符的一部分。

即使您的数据值具有-->,此查询也不应出错。如下所示。

SQL> select Sys_Connect_By_Path('SomeText B-->More Text' || ':' || 'SomeText A-->More Text', ' --> ') "myNewVar"
from dual
connect by  rownum<=3;

myNewVar                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
----------------------------------------------------
--> SomeText B-->More Text:SomeText A-->More Text                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
--> SomeText B-->More Text:SomeText A-->More Text --> SomeText B-->More Text:SomeText A-->More Text                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
--> SomeText B-->More Text:SomeText A-->More Text --> SomeText B-->More Text:SomeText A-->More Text --> SomeText B-->More Text:SomeText A-->More Text

上面的分隔符是-->,请注意空格。这个空格被认为是分隔符的一部分,即chr(1)||chr(45)||chr(45)||chr(62)||chr(1)。整个字符串不是您的数据或列值的一部分。

而下面会出错

SQL> select Sys_Connect_By_Path('SomeText B-->More Text' || ':' || 'SomeText A-->More Text', '-->') "myNewVar"
from dual
connect by  rownum<=3;

ORA-30004: when using SYS_CONNECT_BY_PATH function, cannot have seperator as part of column value
30004. 00000 -  "when using SYS_CONNECT_BY_PATH function, cannot have seperator as part of column value"
*Cause:    
*Action:   Use another seperator which does not occur in any column value,
           then retry.

上面的分隔符是-->,请注意没有空格,即chr(45)||chr(45)||chr(62)。整个字符串确实是数据或列值的一部分,因此导致了错误。

以下是解决方案(性能未经测试)

select regexp_replace(Sys_Connect_By_Path('SomeText B-->More Text' || ':' || 'SomeText A-->More Text', ' -> '),' -> ','-->') "myNewVar"
from dual
connect by  rownum<=3;

myNewVar                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
--------------------------------------
-->SomeText B-->More Text:SomeText A-->More Text                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
-->SomeText B-->More Text:SomeText A-->More Text-->SomeText B-->More Text:SomeText A-->More Text                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
-->SomeText B-->More Text:SomeText A-->More Text-->SomeText B-->More Text:SomeText A-->More Text-->SomeText B-->More Text:SomeText A-->More Text 
说明 - 这里(在上面的查询中)-> (带有空格)不是数据的一部分,即-->。一旦该列被路径连接,regexp_replace 将替换所有出现的->-->,这样您仍然可以将-->作为分隔符而不是->.

我听到你的意思,但当我使用“--”时它会出错,而当我将其从“-->”更改为“->”时它可以工作。 - Nathan Stanford
这是因为 ----> 的一部分,但不是 -> 的一部分。 - Anjan Biswas
然而数据里有一个“-”,为什么不会像之前那样出现错误呢? - Nathan Stanford
1
我已经纠正了。请看修改后的答案。 - Anjan Biswas
@hol 您得到的结果是正确的。带有“--”的字符串在使用分隔符“-->”时不会出错。OP没有说明“myVariable”或“mySecondVariable”包含什么,但假设它们只包含“--”,则他提到的查询不应该失败。 - Anjan Biswas
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接