开发者

How do I insert a new node into a series of xml docs (varchar column type) in sql server

I've got a few thousand rows stored as xml in a varchar column, and I need to add a new node to each of them. I've read that there is no update mechanism and pretty much the only way I can do this is to read out the rows, update them locally, then write them back in.

If this were a small number of rows I'd do it manually, but I can't go through several thousand of them or my brain will melt (I don't want that).

Are there any external tools that could help or even just good techniques for doing updates of this kind/volume?

I'm not 100% sure if we've got an xsd defined but I imagine we do (I can create one, in any case).

Clarification: I did not design the table and it's unlikely I'll be a开发者_如何学运维ble to change it to an actual xml type column


My suggestion would be:

  • add a new column of type XML to your table

    ALTER TABLE dbo.YourTable
      ADD XmlColumn XML
    
  • fill that column with the values from that other column

    UPDATE dbo.YourTable
    SET XmlColumn = CAST(YourVarcharColumn AS XML)
    
  • in your XML column, add that tag you need

    UPDATE dbo.YourTable
    SET XmlColumn.modify(.....)
    
  • if you really need it, update the varchar column from the XML column again (but WHY is it varchar in the first place, when it really contains XML????)

    UPDATE dbo.YourTable
    SET YourVarcharColumn = CAST(XmlColumn AS VARCHAR(xx))
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜