我正在尝试获取单个列中存在的数据,该列使用双重哈希(##)分隔。根据我下面提到的查询,我只能获取5条记录而不是6条线。
我认为我的connectby
表达式存在一些问题。非常感谢任何帮助。
数据
Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##
Query用于获取单个记录中的记录,这些记录使用double hash ##
分隔复制方案:
create table temp (errormessage varchar2(300))
insert into temp errormessage values('Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##')
WITH sample_data
AS ( SELECT errormessage AS Error_Message
FROM TEMP )
SELECT Regexp_substr( error_message, ',?([^#]*)', 1, LEVEL, 'i', 1 ) AS Error_Message
FROM sample_data
WHERE Length( Regexp_substr( error_message, ',?([^#]*)', 1, LEVEL, 'i', 1 ) ) != 0
CONNECT BY ( Regexp_count(error_message, '#') + 1 >= LEVEL AND
PRIOR dbms_random.value IS NOT NULL )
ORDER BY LEVEL
错误消息是具有要分隔的信息的列。现在很容易在任何数据库中复制问题。
答案 0 :(得分:2)
也许你在以下的事情之后:
WITH sample_data AS (SELECT 1 stagging_id,
'A' status,
'Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##' error_message
FROM dual UNION ALL
SELECT 2 stagging_id,
'B' status,
'Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##Line7 ##' error_message
FROM dual)
SELECT stagging_id,
status,
regexp_substr(error_message, '[^#]+', 1, LEVEL) err_msg
FROM sample_data
CONNECT BY PRIOR stagging_id = stagging_id
AND PRIOR sys_guid() IS NOT NULL
AND regexp_substr(error_message, '[^#]+', 1, LEVEL) IS NOT NULL;
STAGGING_ID STATUS ERR_MSG
----------- ------ --------------------------------------------------------------
1 A Line1
1 A Line2
1 A Line3
1 A Line4
1 A Line5
1 A Line6
2 B Line1
2 B Line2
2 B Line3
2 B Line4
2 B Line5
2 B Line6
2 B Line7
现有代码的问题是regexp_substr中的*
,以及您计算单#
而您的分隔符为##
的事实。
您可以像这样修复查询:
WITH sample_data AS (SELECT 1 stagging_id,
'A' status,
'Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##' error_message
FROM dual UNION ALL
SELECT 2 stagging_id,
'B' status,
'Line1## Line2## Line3 ## Line4 ## Line5 ## Line6 ##Line7 ##' error_message
FROM dual)
SELECT Regexp_substr( error_message, ',?([^#]+)', 1, LEVEL, 'i', 1 ) AS Error_Message
FROM sample_dataCONNECT BY ( Regexp_count(error_message, '##') >= LEVEL AND
PRIOR stagging_id = stagging_id AND
PRIOR dbms_random.value IS NOT NULL )
ORDER BY stagging_id, LEVEL;
ERROR_MESSAGE
--------------------------------------------------------------
Line1
Line2
Line3
Line4
Line5
Line6
Line1
Line2
Line3
Line4
Line5
Line6
Line7
请注意我已将regexp_substr中的*
更改为+
并将regexp_count中的'#'
更改为'##'
。< / p>