2016-08-15 29 views
0

我有一個表格(myTable),其中有一列用於保存xml格式的配置。我需要向其中一個節點添加新的設置,但我找不到如何添加它,然後找到它並查看它是否正確添加。這個XML列非常大,所以很難手動添加它。任何幫助,將不勝感激。如何在sql 2008 xml列中添加新的xml

<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" IsTemplate="false"> <Adapter Name="[dbo].[Customer]"> <Col name="LastName" type="varchar" size="100" param="@P_1" /> <Col name="FirstName" type="varchar" size="100" null="true" param="@P_2" /> <Col name="MiddleInitial" type="nvarchar" size="1" null="true" param="@P_3" /> <Col name="Address" type="varchar" size="500" null="true" param="@P_4" /> <Col name="City" type="varchar" size="100" null="true" param="@P_5" /> <Col name="State" type="varchar" size="20" null="true" param="@P_6" /> <!--Add New Col here--> <Col name="PostalCode" type="varchar" size="20" null="true" param="@P_7" /> </Adapter> </Configuration>

回答

0

其他疑問採取件後,我想我終於得到它的工作。

DECLARE @table VARCHAR (max) 
SET @table = '[dbo].[Customer]' 
DECLARE @Column VARCHAR (max) 
SET @Column = 'test1' 

--insert記錄。

UPDATE Sync. scope_config 
SET config_data. modify('insert <Col name="test1" type="bit" null="true" param="@P_000" /> 
after 
(/Configuration/Adapter[@Name=sql:variable("@table")]/Col[@name=sql:variable("@Column")])[1]') 
WHERE config.config_data.exist('/Configuration/Adapter[@Name=sql:variable("@table")]') = 1 

--Find記錄。

SELECT s.config_data. query('(/Configuration/Adapter[@Name=sql:variable("@table")])') 
from dbo.config s 
WHERE s.config_data. exist('/Configuration/Adapter[@Name=sql:variable("@table")]/Col[@name=sql:variable("@Column")]') = 1