2019独角兽企业重金招聘Python工程师标准>>>
刚才研究了一下XMLType字段使用方法 ,现在给大家介绍一下。 主要是新增、查询、修改XMLType字段
表结构:
建表sql:
-- Create table
create table T_BOOK
(
ID VARCHAR2(32) not null,
SYS_ID VARCHAR2(32),
TYPE VARCHAR2(200),
LIBRARY VARCHAR2(32),
TITLE VARCHAR2(200),
CREATE_USER VARCHAR2(64),
SUBJECT VARCHAR2(200),
DESCRIPTION VARCHAR2(200),
PUBLISHER VARCHAR2(200),
CONTRIBUTOR VARCHAR2(200),
CREATE_DATE DATE,
FORMAT VARCHAR2(200),
INENTIFIER VARCHAR2(200),
SOURCE VARCHAR2(200),
LANGUAGE VARCHAR2(200),
OTHER_DESC XMLTYPE
)
tablespace DCP
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table T_BOOK
is '数字资源结构表';
-- Add comments to the columns
comment on column T_BOOK.LIBRARY
is '分区键';
-- Create/Recreate primary, unique and foreign key constraints
alter table T_BOOK
add constraint PK_T_BOOK primary key (ID)
using index
tablespace DCP
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
新增记录:
insert into t_book
(id,other_desc)
values (1 , sys.xmlType.createXML('<name><a id="1" value="some values">abc</a></name>') );
insert into t_book
(id,other_desc)
values (2 , sys.xmlType.createXML('<name><a id="1" value="some values">abc</a><b id="2" value="some2 values">abc2</b></name> ') );
查询记录:
查询一个字段中不存在的元素时,返回null值
得到id=1的value变量的值
select i.other_desc.extract('//name/a[@id=1]/@value ').getStringVal() as ennames, id from t_book i
rowid ennames id
1 some values 1
2 some values 2
得到a节点的值
select id,i.other_desc.getclobval(), i.other_desc.extract('//name/a/text()').getStringVal() as truename from t_book i
添加节点
update ris_u_res.t_data_402 t
set t.other_description=insertchildxml(other_description,
'//xmlType','COPYRIGHT',xmltype('<COPYRIGHT>1</COPYRIGHT>'))
where t.id='721577'
得到b节点id属性的值
Select i.other_desc.extract('/name/b/@id').getStringVal() As Name FROM t_book i where i.id='1'
修改记录:
修改一个字段中不存在的元素时,不会对数据有修改,但是1rows updated,所以需要提交事务。
更新id为1的记录的XMLType字段的a元素的id为1的value的值为some new value
update t_book set other_desc=updateXML(other_desc,'//name/a[@id=1]/@value ','some new value') where id=1
更新节点里面的数据
update T_DATA_892 t
set t.other_description = updateXML(t.other_description,
'//xmlType/SERIALNUM_905S',
'<SERIALNUM_905S>12072</SERIALNUM_905S>')
where t.id='1591083'