插入数据库,捕捉主键重复错误?create directory test as 'd:\test'; --把emp.xml 放到这个目录里
grant write ,read on directory test to martin;employee.xml文件的内容如下:
<?xml version="1.0" encoding="gb2312" ?>
<employees>
<employee>
<empid>e0001</empid>
<name>萧峰</name>
</employee>
<employee>
<empid>e0002</empid>
<name>段誉</name>
</employee>
<employee>
<empid>e0003</empid>
<name>王语嫣</name>
</employee>
<employee>
<empid>e0003</empid>
<name>张无忌</name>
</employee>
</employees>
drop table people2
create table people2
(
personid varchar2(10 primary key ,
name varchar2(20)
);
alter table people2
add constraint P_ID primary key (personid);create or replace procedure xml2table(dir varchar2,
inpfile varchar2 ) is
p xmlparser.parser;
doc xmldom.domdocument;
procedure readelements(doc xmldom.domdocument) is --转到表的子过程
nl xmldom.domnodelist;
len number;
n xmldom.domnode;
nl2 xmldom.domnodelist;
len2 number;
strsql varchar2(1000);
begin
nl := xmldom.getelementsbytagname(doc, 'employee');
len := xmldom.getlength(nl);
for i in 0 .. len - 1 loop
strsql := 'insert into people2 values (';
n := xmldom.item(nl, i);
nl2 := xmldom.getchildnodes(n);
len2 := xmldom.getlength(nl2);
--这个地方想半天: 规律是这样的. (字段+逗号 字段+逗号) 循环,但是第一个字段不能呀
for j in 0 .. len2 - 1 loop
n := xmldom.item(nl2, j);
strSQL := strSQL || '''' || xmldom.getNodeValue(xmldom.getFirstChild(n)) || ''|| ''',';
end loop;
strsql:=substr(strsql,0,length(strsql)-1); --少了一个L 害死我了.应该 细心编程.小心调试啊,
-- select substr(strsql,0,mun) into strsql from dual;
strsql := strsql || ')';
dbms_output.put_line(strsql);
execute immediate (strsql);
end loop;
commit;
dbms_output.put_line('');
end readelements;
begin
p := xmlparser.newparser;
xmlparser.setvalidationmode(p, false);
xmlparser.setbasedir(p, dir);
xmlparser.parse(p, dir ||'\' ||inpfile);
doc := xmlparser.getdocument(p);
xmlparser.freeparser(p);
dbms_output.put_line('存到表 people2 中.');
readelements(doc);
xmldom.freedocument(doc);
end xml2table;begin
xml2table('TEST', 'employee.xml');
end;select *from people2;
grant write ,read on directory test to martin;employee.xml文件的内容如下:
<?xml version="1.0" encoding="gb2312" ?>
<employees>
<employee>
<empid>e0001</empid>
<name>萧峰</name>
</employee>
<employee>
<empid>e0002</empid>
<name>段誉</name>
</employee>
<employee>
<empid>e0003</empid>
<name>王语嫣</name>
</employee>
<employee>
<empid>e0003</empid>
<name>张无忌</name>
</employee>
</employees>
drop table people2
create table people2
(
personid varchar2(10 primary key ,
name varchar2(20)
);
alter table people2
add constraint P_ID primary key (personid);create or replace procedure xml2table(dir varchar2,
inpfile varchar2 ) is
p xmlparser.parser;
doc xmldom.domdocument;
procedure readelements(doc xmldom.domdocument) is --转到表的子过程
nl xmldom.domnodelist;
len number;
n xmldom.domnode;
nl2 xmldom.domnodelist;
len2 number;
strsql varchar2(1000);
begin
nl := xmldom.getelementsbytagname(doc, 'employee');
len := xmldom.getlength(nl);
for i in 0 .. len - 1 loop
strsql := 'insert into people2 values (';
n := xmldom.item(nl, i);
nl2 := xmldom.getchildnodes(n);
len2 := xmldom.getlength(nl2);
--这个地方想半天: 规律是这样的. (字段+逗号 字段+逗号) 循环,但是第一个字段不能呀
for j in 0 .. len2 - 1 loop
n := xmldom.item(nl2, j);
strSQL := strSQL || '''' || xmldom.getNodeValue(xmldom.getFirstChild(n)) || ''|| ''',';
end loop;
strsql:=substr(strsql,0,length(strsql)-1); --少了一个L 害死我了.应该 细心编程.小心调试啊,
-- select substr(strsql,0,mun) into strsql from dual;
strsql := strsql || ')';
dbms_output.put_line(strsql);
execute immediate (strsql);
end loop;
commit;
dbms_output.put_line('');
end readelements;
begin
p := xmlparser.newparser;
xmlparser.setvalidationmode(p, false);
xmlparser.setbasedir(p, dir);
xmlparser.parse(p, dir ||'\' ||inpfile);
doc := xmlparser.getdocument(p);
xmlparser.freeparser(p);
dbms_output.put_line('存到表 people2 中.');
readelements(doc);
xmldom.freedocument(doc);
end xml2table;begin
xml2table('TEST', 'employee.xml');
end;select *from people2;
解决方案 »
- 求等价的插入语句,字符串非正常文字
- 怎样查询相同时间段内价格最大的记录
- oracle数据库安装错误是什么原因,在windows 7系统上
- oracle定时任务时间出错
- pl/sql删除数据后,通过PB还能查询到已经删除的数据
- 我装的是9i,开机时总是报日志文件错误,说什么至少一个或一个以上服务错误什么的,我查看了事件管理器和服务,是以下的两个没起来
- 关于 Oracle中 distinct 的问题 之二
- 高分急求。。。一个存储过程转化。。。。拜托了~~~~~~~~~~~~~~~~~~~~~~~~~~!!!
- 高分求教,怎样恢复数据?
- 新建用户后连接, 提示"没有 CREATE SESSION 权限;登录被拒绝",怎办?
- 如何用sql取二级域名?急100分
- 去掉变量最后一个字符
改成:
begin
execute immediate ('INSERT INTO t VALUES(1,2,2)');
exception when others then
IF instr(Sqlerrm,'RA-00001')>0 THEN
dbms_output.put_line('违反唯一约束!!');
END IF;
end;