作為 PL/SQL 腳本的一部分,我們正在設定一個名為“V_COMMENT”的 Varchar 變數,如下所示:
V_COMMENT := INCOMING_COMMENT || '[' || V_COMMENT || ']';
因為這條陳述句可能對每條記錄運行多次,我們最終會得到如下評論:
行程 578 [行程 456 [行程 123]]
有一個問題,當重新運行這個腳本時,有時會添加一個重復的標簽,如下所示:
行程 123 [行程 123 [行程 000]]
或者
行程 456 [行程 123 [行程 123]]
評論重復和嵌套的地方。
有沒有辦法使用 Regexp_Replace() 洗掉重復的標簽,嵌套的或其他的?
或者
有沒有辦法用 regexp_like 捕捉這些錯誤評論的出現
uj5u.com熱心網友回復:
您可以使用下面的解決方案來洗掉重復的評論。其實我在pl/sql里面使用動態sql來解決這個問題。
DECLARE
v_comment CLOB ;
v_comment2 CLOB ;
v_sql CLOB;
BEGIN
--V_COMMENT := INCOMING_COMMENT || '[' || V_COMMENT || ']';
v_comment := 'process 456[process 123[process 123[process 456[process 000]]]]' ;
v_sql := q'{
with v_temp1 as (
select '}'||v_comment||q'{' as comm0 from dual
)
, v_temp2 AS (
select comm0
, LEVEL lvl
, regexp_count(comm0, '[^\[] ') cnt
, trim(rtrim( regexp_substr(comm0, '[^\[] ', 1, LEVEL), ']' )) AS comm1
, row_number()OVER(PARTITION BY comm0, trim(rtrim( regexp_substr(comm0, '[^\[] ', 1, LEVEL), ']' )) ORDER BY LEVEL) rnb
from v_temp1
CONNECT BY LEVEL <= regexp_count(comm0, '[^\[] ')
)
SELECT listagg(comm1, '[') WITHIN GROUP (ORDER BY lvl) ||
LPAD(']', regexp_count(listagg(comm1, '[')WITHIN GROUP (ORDER BY lvl), '\['), ']') comm2
FROM v_temp2
WHERE rnb = 1
}'
;
--dbms_output.put_line(v_sql); --test
execute immediate v_sql into v_comment2
;
dbms_output.put_line('input v_comment : ' ||v_comment ); --test
dbms_output.put_line('output v_comment2 : '||v_comment2); --test
END;
/
演示:pl/sql
這是我的解決方案的 sql 部分:
with v_temp1 as (
select 'process 578 [process 456 [process 123]]' comm0 from dual union all
select 'process 123 [process 123 [process 000]]' from dual union ALL
select 'process 456 [process 123 [process 123 [process 456 ]]]' from dual
)
, v_temp2 AS (
select comm0
, LEVEL lvl
, regexp_count(comm0, '[^\[] ') cnt
, trim(rtrim( regexp_substr(comm0, '[^\[] ', 1, LEVEL), ']' )) AS comm1
, row_number()OVER(PARTITION BY comm0, trim(rtrim( regexp_substr(comm0, '[^\[] ', 1, LEVEL), ']' )) ORDER BY LEVEL) rnb
from v_temp1
CONNECT BY LEVEL <= regexp_count(comm0, '[^\[] ')
/*You need to add the following two conditions when processing more than one row*/
AND PRIOR comm0 = comm0
AND PRIOR sys_guid() IS NOT NULL
)
SELECT comm0, listagg(comm1, '[') WITHIN GROUP (ORDER BY lvl) ||
LPAD(']', regexp_count(listagg(comm1, '[')WITHIN GROUP (ORDER BY lvl), '\['), ']') comm2
FROM v_temp2
WHERE rnb = 1
GROUP BY comm0
;
演示:sql
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/508626.html
