大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
/*
创新互联建站是一家集网站建设,阿坝州企业网站建设,阿坝州品牌网站建设,网站定制,阿坝州网站建设报价,网络营销,网络优化,阿坝州网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
*根据某个字符拆分字符串
*/
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$','$') );
创建与A相同结构的表A_temp
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表。记得到了你要的结果。
记得采纳!
(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语句并写入到表中。