創(chuàng)建與A相同結(jié)構(gòu)的表A_temp
荊州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
create?or?replace?procedure?busi_bm?is
lstr?varchar2(1000);
ln???number;
mid?a_test.id%type;
m_bm?a_test.bm%type;
begin
for?rec_dia?in?(select?*
from?a_test?t)?loop
if?rec_dia.bm?=?replace(rec_dia.bm,?',')?then
--只有一個(gè)值
insert?into?a_test_temp?values(rec_dia.id,rec_dia.bm);
commit;
else
lstr????:=?rec_dia.bm?||?',';
loop
ln?:=?instr(lstr,?',');--第一個(gè)逗號(hào)的索引??a,b,c
exit?when(nvl(ln,?0)?=?0);--沒有逗號(hào)了,退出
m_bm?:=?ltrim(rtrim(substr(lstr,?1,?ln?-?1)));--獲得a
lstr???????:=?substr(lstr,?ln?+?1);--b,c
insert?into?a_test_temp?values(rec_dia.id,m_bm);
commit;
end?loop;
end?if;
end?loop;
end?busi_bm;
執(zhí)行上面的存儲(chǔ)過程,寫入到A_TEMP表。記得到了你要的結(jié)果。
記得采納!
/*
*根據(jù)某個(gè)字符拆分字符串
*/
CREATE OR REPLACE TYPE str_split IS TABLE OF VARCHAR2 (4000);
CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2)
RETURN str_split
PIPELINED
AS
v_length NUMBER := LENGTH(p_string);
v_start NUMBER := 1;
v_index NUMBER;
BEGIN
WHILE(v_start = v_length)
LOOP
v_index := INSTR(p_string, p_delimiter, v_start);
/ 示范 /
SELECT * from table(splitstr('Hello,Cnblogs!',','));
拆分sql后:
select?
substr('Hello World',0,instr('Hello World','o')) as 首,
substr('Hello World',instr('Hello World','o')+1) as 尾
from dual;
有什么規(guī)定嗎?
“ABCDEFGH”或是“A,B,C,D,E,F(xiàn),G,H"
按符號(hào)拆分?按位置拆分?
substr()函數(shù):截取a-b之間的或a 。如:substr('Hello World',0,5)截取1-5或
substr('Hello World',0,instr('Hello World','o'))截取1-"o"的位置
1.如果是","等符號(hào):
select substr('Hello World',0,instr('Hello World','o')) ?from dual;
select substr('Hello World',instr('Hello World','o')+1) ?from dual;
或
select substr('Hello World',instr('Hello World','o')+1,length('Hello World')) ?from dual;
create or replace type strArray as table of varchar2(4020)
--此方法只能使用英文半角逗號(hào)(,)
create or replace function f_split(instr IN VARCHAR2) return strArray
as
l_tablen BINARY_INTEGER;
l_tab? ? DBMS_UTILITY.uncl_array;
l_ret strArray:=strArray();
BEGIN
DBMS_UTILITY.comma_to_table(instr, l_tablen, l_tab);
FOR i IN 1 .. l_tablen LOOP
l_ret.EXTEND(1);
l_ret(i):=l_tab(i);
END LOOP;
return l_ret;
END;
select t.column_value as value from table(f_split('a,b,付')) t;
create or replace type strArray as table of varchar2(4020)
CREATE OR REPLACE FUNCTION f_split(p_str IN VARCHAR2, p_delimiter IN VARCHAR2)
? RETURN strArray IS
? TYPE ret_cur IS REF CURSOR;
? ret_cur1 ret_cur;
? g_str strarray:=strarray();
? l_sql varchar2(500):='';
BEGIN
? l_sql :='SELECT REGEXP_SUBSTR ('''||p_str||''',''[^'||p_delimiter||']+'',1,LEVEL) as valuesa
? ? ? ? ? ? FROM DUAL
? ? ? ? ? ? CONNECT BY REGEXP_SUBSTR ('''||p_str||''',''[^'||p_delimiter||']+'',1,LEVEL) IS NOT NULL';
OPEN ret_cur1 FOR l_sql ;
? FETCH ret_cur1 BULK COLLECT INTO g_str;
? CLOSE ret_cur1;
? RETURN g_str;
END f_split;
select * from table(f_split('abc$d$e$','$') );
(1)定義split_type類型:
CREATE OR REPLACE TYPE split_type IS TABLE OF VARCHAR2 (4000) /
(2)定義split函數(shù):
CREATE OR REPLACE FUNCTION split (p_str IN VARCHAR2, p_delimiter IN VARCHAR2) RETURN split_type IS j INT := 0; i INT := 1; len INT := 0; len1 INT := 0; str VARCHAR2 (4000); my_split split_type := split_type (); BEGIN len := LENGTH (p_str); len1 := LENGTH (p_delimiter); WHILE j len LOOP j := INSTR (p_str, p_delimiter, i); IF j = 0 THEN j := len; str := SUBSTR (p_str, i); my_split.EXTEND; my_split (my_split.COUNT) := str; IF i = len THEN EXIT; END IF; ELSE str := SUBSTR (p_str, i, j - i); i := j + len1; my_split.EXTEND; my_split (my_split.COUNT) := str; END IF; END LOOP; RETURN my_split; END split; /
(3)存儲(chǔ)過程中,使用類似
For T In ( select a,b,c,d from table (split('1,2,3,4',',')) ) Loop
--注意下面的inserti語句,varchar類型的值需要補(bǔ)充引號(hào)上去
Execute Immediate ' insert into tableName set fieldName = '||T.a ;
Execute Immediate 'commit';
End Loop;
的查詢語句,把分開的結(jié)果拼成sql語句并寫入到表中。
如果是固定的格式,那就好辦,檢測(cè)字段中的“,”分割成四段,然后按要求接起來。
使用函數(shù)好些,可以直接調(diào)用。
函數(shù)寫好:已經(jīng)測(cè)試過。
CREATE?OR?REPLACE?FUNCTION?json_montage?(JSON?IN?VARCHAR2)?RETURN?VARCHAR2?IS
In_JSON??VARCHAR2(4096);
v_JSON??VARCHAR2(4096)?DEFAULT?'{"comicPkgList":[{J1,J2,J4}],J3}';
v_J1??VARCHAR2(4096);
v_J2??VARCHAR2(4096);
v_J3??VARCHAR2(4096);
v_J4??VARCHAR2(4096);
v_split?VARCHAR2(10)?DEFAULT?',';
len?NUMBER(10)?default?0;
place?NUMBER(10)?default?2;
BEGIN
In_JSON?:=JSON;
/*
原字符串:
{"IsSmsConfirm":false,"productID":"MDSP3331111","autoReceive":false,"supCycle":1}
期望轉(zhuǎn)換后的:
{"comicPkgList":[{"IsSmsConfirm":false,"productID":"MDSP3331111","supCycle":1}],"autoReceive":false}
*/
In_JSON?:=?RTRIM(LTRIM(In_JSON));
place?:=?INSTR(In_JSON,v_split,2,1);
len?:=?place?-1?-1;
v_J1?:=?SUBSTR(In_JSON,2,len);
len?:=?INSTR(In_JSON,v_split,2,2)?-?place?-1-0;
v_J2?:=?SUBSTR(In_JSON,place+1,len);
place?:=?INSTR(In_JSON,v_split,2,2);
len?:=?INSTR(In_JSON,v_split,2,3)?-?place?-1-0;
v_J3?:=?SUBSTR(In_JSON,place+1,len);
place?:=?INSTR(In_JSON,v_split,2,3);
len?:=?LENGTH(In_JSON)?-?place?-1;
v_J4?:=?SUBSTR(In_JSON,place+1,len);
v_JSON?:=?'{"comicPkgList":[{'?||?v_J1?||?','?||?v_J2?||?','?||?v_J4?||?'}],'?||?v_J3?||?'}';
IF?v_JSON?=?'{"comicPkgList":[{J1,J2,J4}],J3}'?THEN
v_JSON?:='ERROR';
END?IF;
RETURN?v_JSON;
EXCEPTION
WHEN?NO_DATA_FOUND?THEN
NULL;
WHEN?OTHERS?THEN
RAISE;
END?json_montage;
/