[英]SQL Server 2008 find and replace elements in XML column
在下表中:
CREATE TABLE [dbo].[GDB_ITEMS](
[ObjectID] [int] NOT NULL,
[UUID] [uniqueidentifier] NOT NULL,
[Type] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](226) NULL,
[PhysicalName] [nvarchar](226) NULL,
[Path] [nvarchar](512) NULL,
[Url] [nvarchar](255) NULL,
[Properties] [int] NULL,
[Defaults] [varbinary](max) NULL,
[DatasetSubtype1] [int] NULL,
[DatasetSubtype2] [int] NULL,
[DatasetInfo1] [nvarchar](255) NULL,
[DatasetInfo2] [nvarchar](255) NULL,
[Definition] [xml] NULL,
[Documentation] [xml] NULL,
[ItemInfo] [xml] NULL,
[Shape] [geometry] NULL,
CONSTRAINT [R2_pk] PRIMARY KEY CLUSTERED
(
[ObjectID] ASC
)
ALTER TABLE [dbo].[GDB_ITEMS] WITH CHECK ADD CONSTRAINT [g1_ck] CHECK (([Shape].[STSrid]=(4326)))
GO
[Documentation]
列包含數百個xml項和元素。 我試圖用T-SQL來替換一系列元素:
<NPS_Info>
<MetaPurp>NPS</MetaPurp>
<NPS_Unit>
<UnitCode>MANDATORY for Data Store: NPS Alpha Unit Code (ACAD)</UnitCode>
<UnitType>MANDATORY for Data Store: NPS Unit Type (National Park, National Monument, etc)</UnitType>
</NPS_Unit>
</NPS_Info>
有了這個:
<NPS_Info>
<MetaPurp>NPS</MetaPurp>
<MetaPurp>CSDGM</MetaPurp>
<MetaPurp>OnlineData</MetaPurp>
<NPS_Unit>
<UnitCode>ABCD</UnitCode>
<UnitType>Park</UnitType>
</NPS_Unit>
<DatStore>
<Category>Landscape</Category>
<Category>Monitoring</Category>
<Category>Resource Management</Category>
<DSteward>
<cntinfo>
<cntperp>
<cntper>Something</cntper>
</cntperp>
<cntaddr>
<addrtype>mailing and physical</addrtype>
<address>1 Smith Lane</address>
<address></address>
<city>Anywhere</city>
<state>ST</state>
<postal>12345</postal>
</cntaddr>
<cntemail>email@email.com</cntemail>
</cntinfo>
</DSteward>
</DatStore>
</NPS_Info>
請原諒我笨拙的切割貼。 這個表中有幾千行,但是,並非所有行都有第一個代碼塊中描述的xml元素(這是一個全局表,其中包含DB中所有表的描述,一些[Documentation]記錄將包含非相關的xml對此操作不感興趣)。
您可以使用XML數據修改語言(XML DML)
此代碼將使用變量@XML
的內容更改名為NPS_Info
的第一個節點的內容。
-- Delete everything in node NPS_Info
update YourTable
set XMLCol.modify('delete //NPS_Info[1]/*')
-- Insert @XML to node NPS_Info
update YourTable
set XMLCol.modify('insert sql:variable("@XML") into (//NPS_Info)[1]')
SE數據的工作樣本
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.