【ORACLE】逗號分隔的字串的應用
阿新 • • 發佈:2019-02-01
【問題】遇到逗號分隔的字串轉為巢狀表,
如果採用自定義的方法,需要寫個迴圈,比較麻煩
【分析】
ORACLE提供了類似的過程來實現這個功能
【結論】 comma_to_table
table_to_comma 都是對應函式 字串與記憶體表互轉
dbms_utility.comma_to_table( list IN VARCHAR2, tablen OUT BINARY_INTEGER, tab OUT lname_array); |
CREATE TABLE c2t_test
( readline VARCHAR2(200)); INSERT INTO c2t_test VALUES ('"1","Mainframe","31-DEC-2001"'); INSERT INTO c2t_test VALUES ('"2","MPP","01-JAN-2002"'); INSERT INTO c2t_test INSERT INTO c2t_test VALUES ('"4","PC","03-MAR-2004"'); INSERT INTO c2t_test VALUES ('"5","Macintosh","04-APR-2005"'); COMMIT; SELECT * FROM c2t_test; CREATE TABLE test_import ( src_no NUMBER(5), src_desc VARCHAR2(20), load_date DATE); CREATE OR REPLACE PROCEDURE load_c2t_test IS c_string cnt BINARY_INTEGER; my_table dbms_utility.lname_array; BEGIN FOR t_rec IN (SELECT * FROM c2t_test) LOOP dbms_utility.comma_to_table(t_rec.readline, cnt, my_table); my_table(1) := TRANSLATE(my_table(1), 'A"','A'); my_table(2) := TRANSLATE(my_table(2), 'A"','A'); my_table(3) := INSERT INTO test_import (src_no, src_desc, load_date) VALUES (TO_NUMBER(my_table(1)), my_table(2),TO_DATE(my_table(3))); END LOOP; COMMIT; END load_c2t_test; / exec load_c2t_test; SELECT * FROM test_import; |
【結論】 comma_to_table
table_to_comma 都是對應函式 字串與記憶體表互轉