Oracle 批量插入记录
这里使用了PL/SQL的联合数组.
我要将一组用'_'分隔的记录插入数据库,当然,这样做只是为了方便从java调用存储过程进行批量插入.
先定义数组类型和一个工具函数(用来分解字符串):
1 CREATE OR REPLACE PACKAGE qtone IS
2 TYPE SUBSTR_ARRAY IS TABLE OF VARCHAR2(100 CHAR) INDEX BY BINARY_INTEGER;
3 END;
4
5 create or replace FUNCTION split_str(str in varchar2, delimiter in varchar2) RETURN qtone.SUBSTR_ARRAY is
6 src_str varchar2(300 char) := '';
7 prev_index integer := 1;
8 next_index integer := 1;
9
10 retval qtone.substr_array ;
11 cnt INTEGER := 1;
12 BEGIN
13 --去掉开头和结束的分隔符
14 src_str := trim(both delimiter from str);
15 while next_index != 0 loop
16 next_index := instr(src_str,delimiter,prev_index,1);
17
18 if(next_index = 0) then
19 retval(cnt) := substr(src_str,prev_index);
20 cnt := cnt + 1;
21 else
22 retval(cnt) := substr(src_str,prev_index,next_index - prev_index);
23 cnt := cnt + 1;
24 end if;
25
26 prev_index := next_index + 1;
27 end loop;
28
29 RETURN retval;
30 end;
2 TYPE SUBSTR_ARRAY IS TABLE OF VARCHAR2(100 CHAR) INDEX BY BINARY_INTEGER;
3 END;
4
5 create or replace FUNCTION split_str(str in varchar2, delimiter in varchar2) RETURN qtone.SUBSTR_ARRAY is
6 src_str varchar2(300 char) := '';
7 prev_index integer := 1;
8 next_index integer := 1;
9
10 retval qtone.substr_array ;
11 cnt INTEGER := 1;
12 BEGIN
13 --去掉开头和结束的分隔符
14 src_str := trim(both delimiter from str);
15 while next_index != 0 loop
16 next_index := instr(src_str,delimiter,prev_index,1);
17
18 if(next_index = 0) then
19 retval(cnt) := substr(src_str,prev_index);
20 cnt := cnt + 1;
21 else
22 retval(cnt) := substr(src_str,prev_index,next_index - prev_index);
23 cnt := cnt + 1;
24 end if;
25
26 prev_index := next_index + 1;
27 end loop;
28
29 RETURN retval;
30 end;
然后,做个简单测试:
1 CREATE TABLE test(
2 name VARCHAR2(20 char),
3 address VARCHAR2(50 char)
4 );
5
6 DECLARE
7 names VARCHAR2(100 char) := 'ungshow_sweet_ting';
8 addrs VARCHAR2(100 char) := 'GuangDong_ZheJiang_HuBei';
9
10 addr_array qtone.SUBSTR_ARRAY;
11 name_array qtone.SUBSTR_ARRAY;
12
13 stmt VARCHAR2(100 char) := '';
14 BEGIN
15 name_array := split_str(names,'_');
16 addr_array := split_str(addrs,'_');
17
18 stmt := 'insert into test(name,address) values(:name,:addr)';
19 IF(name_array.COUNT <> 0 AND name_array.COUNT = addr_array.COUNT ) THEN
20 FOR i IN name_array.first..name_array.last LOOP
21 EXECUTE IMMEDIATE stmt USING name_array(i),addr_array(i);
22 END LOOP ;
23
24 commit;
25 END IF ;
26 END ;
2 name VARCHAR2(20 char),
3 address VARCHAR2(50 char)
4 );
5
6 DECLARE
7 names VARCHAR2(100 char) := 'ungshow_sweet_ting';
8 addrs VARCHAR2(100 char) := 'GuangDong_ZheJiang_HuBei';
9
10 addr_array qtone.SUBSTR_ARRAY;
11 name_array qtone.SUBSTR_ARRAY;
12
13 stmt VARCHAR2(100 char) := '';
14 BEGIN
15 name_array := split_str(names,'_');
16 addr_array := split_str(addrs,'_');
17
18 stmt := 'insert into test(name,address) values(:name,:addr)';
19 IF(name_array.COUNT <> 0 AND name_array.COUNT = addr_array.COUNT ) THEN
20 FOR i IN name_array.first..name_array.last LOOP
21 EXECUTE IMMEDIATE stmt USING name_array(i),addr_array(i);
22 END LOOP ;
23
24 commit;
25 END IF ;
26 END ;
posted on 2008-08-18 00:06 CodingME! 阅读(7621) 评论(2) 编辑 收藏 举报