这是因为--
是-->
分隔符的一部分,但不是->
分隔符的一部分。
即使您的数据值具有-->
,此查询也不应出错。如下所示。
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
将替换所有出现的
->
为
-->
,这样您仍然可以将
-->
作为分隔符而不是
->
.
SomeText B-->More Text
而不是SomeText B--More Text
?因为这样就很明显:前后空格不相等。 - hol