SQL 查询仅返回 5 条记录而不是 6 行

SQL Query Returning only 5 records instead of 6 rows

我正在尝试获取单个列中存在的数据,该列以双哈希 (##) 分隔。根据下面提到的查询,我只能获取 5 条记录而不是 6 行。

我认为我的 connectby 表达有问题。非常感谢任何帮助。

数据

Line1## Line2## Line3 ## Line4 ## Line5  ## Line6  ##

查询用于获取单个记录中的记录,这些记录由双散列分隔##

复制场景:

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 

Error Message 是包含要分隔的信息的列。现在很容易在您的任何数据库中复制问题。

也许您正在寻找类似的东西:

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 中的 * 更改为 + 以及 '#' 中的 '##' =29=].