如何使用SQL查询编辑XML值?

时间:2017-08-10 06:05:55

标签: sql sql-server xml

我在SQL Server数据库中有XMLUserDetails,其值如下:

<fleet>
    <Setting name="city"/>
    <UserSettings name="details">
        <UserSettings name="A1">
            <setting type="1" name="email"/>
            <setting value="user"/>
        </UserSettings>
        <UserSettings name="A2">
            <setting type="2" name="phone"/>
            <setting value="technician"/>
        </UserSettings>
    </UserSettings>     
</fleet> 

我想从数据库中读取并在

中添加一个新设置
<UserSettings name="A2"> .

有人能指出我应该做什么吗?

2 个答案:

答案 0 :(得分:2)

您可以使用sql:variable()来介绍您在外部创建的值。您可以使用Xml.exist()查找以前没有获得此新条目的XML:

DECLARE @myTbl TABLE(Descr VARCHAR(100),TheXml XML);            
INSERT INTO @myTbl VALUES
(   
'Setting "A3" doesn''t exist','<fleet>
    <Setting name="city" />
    <UserSettings name="details">
        <UserSettings name="A1">
            <setting type="1" name="email" />
            <setting value="user" />
        </UserSettings>
        <UserSettings name="A2">
            <setting type="2" name="phone" />
            <setting value="technician" />
        </UserSettings>
    </UserSettings>
</fleet>'
)
,(   
'Setting "A3" exists already','<fleet>
    <Setting name="city" />
    <UserSettings name="details">
        <UserSettings name="A1">
            <setting type="1" name="email" />
            <setting value="user" />
        </UserSettings>
        <UserSettings name="A2">
            <setting type="2" name="phone" />
            <setting value="technician" />
        </UserSettings>
        <UserSettings name="A3">
            <setting type="3" name="A3 setting" />
            <setting value="this exists already" />
        </UserSettings>
    </UserSettings>
</fleet>'
);  

DECLARE @NewSetting XML=
      N'<UserSettings name="A3">
            <setting type="3" name="new A3" />
            <setting value="new data" />
        </UserSettings>';


UPDATE @myTbl
SET TheXml.modify('             
insert sql:variable("@NewSetting")
as last into (/fleet/UserSettings)[1]')           
WHERE TheXml.exist(N'/fleet/UserSettings[@name="details"]/UserSettings[@name="A3"]')=0;  

SELECT * FROM @myTbl      

答案 1 :(得分:1)

SQL SERVER为modify XML数据提供方法,并提供了一种语言(XML DML)。
您需要将modify方法与insert xml dml语句一起使用。

以下是完整的示例:

示例数据:

DECLARE @myDoc xml;            
SET @myDoc =   
'<fleet>
    <Setting name="city" />
    <UserSettings name="details">
        <UserSettings name="A1">
            <setting type="1" name="email" />
            <setting value="user" />
        </UserSettings>
        <UserSettings name="A2">
            <setting type="2" name="phone" />
            <setting value="technician" />
        </UserSettings>
    </UserSettings>
</fleet>';  

声明:

SET @myDoc.modify('             
insert <setting value="NEW SETTINGS" />
as first into (/fleet/UserSettings/UserSettings[@name="A2"])[1] ') ;           
SELECT @myDoc;        

结果:

<fleet>
    <Setting name="city" />
    <UserSettings name="details">
        <UserSettings name="A1">
            <setting type="1" name="email" />
            <setting value="user" />
        </UserSettings>
        <UserSettings name="A2">
            <setting value="NEW SETTINGS" />
            <setting type="2" name="phone" />
            <setting value="technician" />
        </UserSettings>
    </UserSettings>
</fleet>