在SQL Server中使用XML类型字段获取值

时间:2022-07-08 23:49:56

I have MS SQL table which contains a XML type field. This field has data in the format below:

我有MS SQL表,其中包含XML类型字段。此字段包含以下格式的数据:

<doc>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>

The quotes can be in different orders. I need to see the data in the below format which shows which quote came first second and third for each document.

报价可以按不同的顺序排列。我需要以下面的格式查看数据,该数据显示每个文档的第一个第二个和第三个。

Code 1       Code 2        Code 3
--------------------------------
   AA         BB          CC
   BB         AA          CC

2 个解决方案

#1


1  

Try this:

DECLARE @test TABLE(ID INT, XmlCol XML)

INSERT INTO @test VALUES(1, '<doc>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>')

INSERT INTO @test VALUES(2, '<doc>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>')

SELECT
    ID,
    X.Doc.value('(quote/code)[1]', 'varchar(20)') AS 'Code1',
    X.Doc.value('(quote/code)[2]', 'varchar(20)') AS 'Code2',
    X.Doc.value('(quote/code)[3]', 'varchar(20)') AS 'Code3'
FROM @test
CROSS APPLY xmlcol.nodes('doc') AS X(Doc)

Gives you an output of:

给你一个输出:

ID  Code1   Code2   Code3
1   AA  BB  CC
2   BB  AA  CC

#2


0  

declare @x xml = '<doc>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>';

select @x.value('(doc/quote/code)[1]', 'varchar(max)')
    , @x.value('(doc/quote/code)[2]', 'varchar(max)')
    , @x.value('(doc/quote/code)[3]', 'varchar(max)')

For @marc_s,

DECLARE @test TABLE(ID INT, XmlCol XML)

INSERT INTO @test VALUES(1, '<doc>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>')

INSERT INTO @test VALUES(2, '<doc>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>')




SELECT
    ID,
    XmlCol.value('(doc/quote/code)[1]', 'varchar(20)') AS 'Code1',
    XmlCol.value('(doc/quote/code)[2]', 'varchar(20)') AS 'Code2',
    XmlCol.value('(doc/quote/code)[3]', 'varchar(20)') AS 'Code3'
FROM @test

#1


1  

Try this:

DECLARE @test TABLE(ID INT, XmlCol XML)

INSERT INTO @test VALUES(1, '<doc>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>')

INSERT INTO @test VALUES(2, '<doc>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>')

SELECT
    ID,
    X.Doc.value('(quote/code)[1]', 'varchar(20)') AS 'Code1',
    X.Doc.value('(quote/code)[2]', 'varchar(20)') AS 'Code2',
    X.Doc.value('(quote/code)[3]', 'varchar(20)') AS 'Code3'
FROM @test
CROSS APPLY xmlcol.nodes('doc') AS X(Doc)

Gives you an output of:

给你一个输出:

ID  Code1   Code2   Code3
1   AA  BB  CC
2   BB  AA  CC

#2


0  

declare @x xml = '<doc>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>';

select @x.value('(doc/quote/code)[1]', 'varchar(max)')
    , @x.value('(doc/quote/code)[2]', 'varchar(max)')
    , @x.value('(doc/quote/code)[3]', 'varchar(max)')

For @marc_s,

DECLARE @test TABLE(ID INT, XmlCol XML)

INSERT INTO @test VALUES(1, '<doc>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>')

INSERT INTO @test VALUES(2, '<doc>
   <quote>
      <code>BB</code>
   </quote>
   <quote>
      <code>AA</code>
   </quote>
   <quote>
      <code>CC</code>
   </quote>
</doc>')




SELECT
    ID,
    XmlCol.value('(doc/quote/code)[1]', 'varchar(20)') AS 'Code1',
    XmlCol.value('(doc/quote/code)[2]', 'varchar(20)') AS 'Code2',
    XmlCol.value('(doc/quote/code)[3]', 'varchar(20)') AS 'Code3'
FROM @test