重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
创建与A相同结构的表A_temp
荆州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信: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
--只有一个值
insert into a_test_temp values(rec_dia.id,rec_dia.bm);
commit;
else
lstr := rec_dia.bm || ',';
loop
ln := instr(lstr, ',');--第一个逗号的索引 a,b,c
exit when(nvl(ln, 0) = 0);--没有逗号了,退出
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;
执行上面的存储过程,写入到A_TEMP表。记得到了你要的结果。
记得采纳!
/*
*根据某个字符拆分字符串
*/
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;
有什么规定吗?
“ABCDEFGH”或是“A,B,C,D,E,F,G,H"
按符号拆分?按位置拆分?
substr()函数:截取a-b之间的或a 。如:substr('Hello World',0,5)截取1-5或
substr('Hello World',0,instr('Hello World','o'))截取1-"o"的位置
1.如果是","等符号:
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)
--此方法只能使用英文半角逗号(,)
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函数:
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)存储过程中,使用类似
For T In ( select a,b,c,d from table (split('1,2,3,4',',')) ) Loop
--注意下面的inserti语句,varchar类型的值需要补充引号上去
Execute Immediate ' insert into tableName set fieldName = '||T.a ;
Execute Immediate 'commit';
End Loop;
的查询语句,把分开的结果拼成sql语句并写入到表中。
如果是固定的格式,那就好办,检测字段中的“,”分割成四段,然后按要求接起来。
使用函数好些,可以直接调用。
函数写好:已经测试过。
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}
期望转换后的:
{"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;
/