将xml有效负载按原样插入到列中

时间:2014-09-08 15:18:13

标签: sql xml

我想将以下xml有效内容插入表列中。

<PostTransAuctionBidsResponse>
  <AuctionBidsResponse>
    <AuctionBidOutput>
      <BidOfferRef>-1</BidOfferRef>
      <Reason>xxx : Provider/Auction/Customer is not valid'</Reason>
      <Provider>HGM</Provider>
    </AuctionBidOutput>
  </AuctionBidsResponse>
</PostTransAuctionBidsResponse>

当我尝试插入它时,我被迫将其转换为varchar值。但是当我转换它时,表格中的值如下所示。

<PostTransAuctionBidsResponse><AuctionBidsResponse><AuctionBidOutput><BidOfferRef>-1</BidOfferRef><Reason>xxx : Provider/Auction/Customer is not valid'</Reason><Provider>HGM</Provider></AuctionBidOutput></AuctionBidsResponse></PostTransAuctionBidsResponse>

是否可以将其更改为上面的有效负载?

我正在做的事情就像 -

declare @xml xml
set @xml = 
'
    <PostTransAuctionBidsResponse>
      <AuctionBidsResponse>
        <AuctionBidOutput>
          <BidOfferRef>-1</BidOfferRef>
          <Reason>xxx : Provider/Auction/Customer is not valid'</Reason>
          <Provider>HGM</Provider>
        </AuctionBidOutput>
      </AuctionBidsResponse>
    </PostTransAuctionBidsResponse>
' 

declare @Content varchar(max)
set @Content = convert(varchar(max),@xml)

insert into TES_Log(content)
values(@Content)

1 个答案:

答案 0 :(得分:1)

根据这篇文章(link),在将值赋给@xml变量时,还需要显式转换。在这两种情况下,您还需要包含convert()的第三个参数:

set @xml = convert(XML,
'
<PostTransAuctionBidsResponse>
  <AuctionBidsResponse>
    <AuctionBidOutput>
      <BidOfferRef>-1</BidOfferRef>
      <Reason>xxx : Provider/Auction/Customer is not valid'</Reason>
      <Provider>HGM</Provider>
    </AuctionBidOutput>
  </AuctionBidsResponse>
</PostTransAuctionBidsResponse>
', 1)

set @Content = convert(varchar(max),@xml, 1)