2010-10-20 31 views
5

如何使用oracle XML數據類型?快速介紹如何使用oracle xml數據類型

這個問題旨在asked and answered由我,只是爲了與他人共享信息

+1

壞事。要麼獲得博客,要麼創建這個社區wiki! – 2010-10-20 05:36:40

+3

你如何使它成爲「社區wiki」?我的印象是,這樣的事情被鼓勵,根據[stackoverflow常見問題](http://stackoverflow.com/faq) – corydoras 2010-10-20 05:39:05

+0

它應該仍然是一個問題,但。 「我如何處理」有點寬泛。無論如何。 – Thilo 2010-10-20 06:08:51

回答

8

下面的示例SQL演示瞭如何插入,包含XMLTYPE數據類型的查詢和更新數據庫字段:

-- Create a table that can store XML 
create table sample_xml (id number, xml xmltype); 

-- Insert some XML into the table 
insert into sample_xml values (1, xmltype.createxml('<subject><name>test</name><list><li>a</li><li>b</li></list></subject>')); 
insert into sample_xml values (2, xmltype.createxml('<subject><name>test</name><list><li>a</li></list></subject>')); 

-- When doing a select, refer to table using the alias or getClobVal() will not work 
select t.id, t.xml.getClobVal() from sample_xml t; 

-- Update text of a single xml element 
UPDATE sample_xml SET xml = UPDATEXML(xml, '/subject/name/text()','happy') WHERE id = 2; 

-- Select out just the name for each row in the table using xpath 
select id, extractvalue(xml, '/subject/name/text()') from sample_xml; 

-- Doing an sample_xml, where the xpath string matches two elements, the text of both elements will be updated 
UPDATE sample_xml SET xml = UPDATEXML(xml, '/subject/list/li/text()','one');