SQL2005中完全支持XML类型,我们可能使用XML来存储非关系型的数据.下面的例子演示了:
1.从Xml文件读出数据到xml变量中
2.将Xml变量中的数据转换成二维表输出
declare @xml as xml; -- --select @xml =bulkcolumn from openrowset(bulk 'c:\word.xml', single_blob) as x; -- -- ----select x.value('Word[1]', 'nvarchar(50)') as Word, ---- x.value('Meaning[1]', 'nvarchar(500)') as Meanning ----from @xml.nodes('/Daily-Words/WordItem') tab(x); -- --select x.value('Word[1]', 'nvarchar(50)'), -- x.value('Meaning[1]', 'nvarchar(500)') --from @xml.nodes('/Daily-Words/WordItem') as tab(x); set @xml='<masterx><row><id>1</id><name>Rock</name></row> <row><id>2</id><name>ATZ</name></row></masterx>'; select x.value('id[1]', 'bigint') as id, x.value('name[1]', 'nvarchar(255)') as name from @xml.nodes('/masterx/row') as tab(x)
c:\word.xml内容:
<?xml version="1.0" encoding="utf-8"?> <Daily-Words xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <WordItem> <Word>surprise</Word> <Meaning>[n]惊?奇?, 惊?讶?令?人?吃?惊?的?事?物?; [vt]使?惊?奇?, 使?诧?异?意?外?发?现?[撞?见?], 出?其?不?意?获?得?</Meaning> <Sample1 /> <Sample2 /> </WordItem> <WordItem> <Word>simultaneous</Word> <Meaning>[adj]同?时?发?生?的?; 同?时?存?在?的?</Meaning> <Sample1 /> <Sample2 /> </WordItem> </Daily-Words>
能使用Xml的地方:
1.存储过程传多行数据为参数
2.表中存放Xml序列化的对象.