How do you read XML column in SQL Server 2008?

后端 未结 3 491
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 14:39

I have never used XML in SQL Server 2008, I need to extract a list of customers into a variable table how do you do it?

Given that I have a column called Custo

3条回答
  •  难免孤独
    2020-11-30 15:28

    You need to use CROSS APPLY from table to XML column

    create table sales (customerlist xml)
    insert sales select '
        
           
               1
               Mr Smith
           
           
              2
              Mr Bloggs
           
        '
    

    Your query:

    SELECT
       N.C.value('ItemId[1]', 'int') ItemId,
       N.C.value('Value[1]', 'varchar(100)') Value
    FROM dbo.Sales
    CROSS APPLY CustomerList.nodes('//Customer') N(C)
    

    EDIT - note
    The query above was written quickly to illustrate working with xml columns in a table (multi-row). For performance reasons, don't use '//Customer' but use an absolute path instead '/ArrayOfCustomers/Customer'. '//Customer' will go through the entire XML to find Customer nodes anywhere in the XML at any level.

提交回复
热议问题