Oracle 字符串轉多行(REGEXP_SUBSTR)



方案一:

SQL

1.一個數據表(TABLE1_ZK)中存在一個字段(STRS)(存儲格式是以【,】隔開的字符串)

2.現需要將其查分為多行數據(每行為其中一個字符串)

3.sql

SELECT t.id,
           REGEXP_SUBSTR(t.STRS, '[^,]+', 1, LEVEL) AS mat  
      FROM (
           select bds.id,bds.STRS 
           from  TABLE1_ZK bds 
           where bds.STRS is not null 
       ) t
    CONNECT BY LEVEL <=  regexp_count(t.STRS, ',')+1 
    and t.id = prior t.id  and prior dbms_random.value is not null

4.查詢結果

 


可能遇見的問題:

SpringBoot   druid 可能會報SQL注入失敗:java.sql.SQLException: sql injection violation, deny object : dbms_random

1.原因:

yml中進行了wall(防火牆)配置;wall攔截了dbms_random

2.解決:

去掉其中wall配置即可


方案二:

若業務系統不允許去除wall

更換SQL:

select distinct bds.id, regexp_substr(bds.STRS, '[^,]+', 1, Level,'i') str
from TABLE1_ZK bds 
where bds.STRS is not null 
connect by Level <= LENGTH(bds.STRS) - LENGTH(REGEXP_REPLACE(bds.STRS, ',', '')) + 1

查詢結果:


注:大數據量(多行且需字段轉行數量較大)可能會出現死循環;

建議程序中循環處理,查詢單條並轉換;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM