Can I do a find/replace in t-sql?

后端 未结 3 649
忘了有多久
忘了有多久 2020-12-24 08:24

I basically have an xml column, and I need to find and replace one tag value in each record.

3条回答
  •  一整个雨季
    2020-12-24 08:33

    To find a content in an XML column, look into the exist() method, as described in MSDN here.

    SELECT * FROM Table
    WHERE XMLColumn.exist('/Root/MyElement') = 1
    

    ...to replace, use the modify() method, as described here.

    SET XMLColumn.modify('
      replace value of (/Root/MyElement/text())[1]
      with "new value"
    ')
    

    ..all assuming SqlServer 2005 or 2008. This is based on XPath, which you'll need to know.

提交回复
热议问题