1

如何编写一个 SQL Server 查询,该查询从 xml 数据类型中进行选择,并在基于其他属性匹配的元素中选择属性。

想象以下示例:

create table dbo.Configuration(Configuration xml not null);

insert into dbo.Configuration(Configuration) values(convert(xml, '<?xml version="1.0" standalone="yes"?>
<Configuration>
    <DoSRequestAnalysis>
        <Windows>
            <Window Name="Smallest" Duration="15">
                <ThresholdsToRemoveRequests NoofRequests="25" />
            </Window>
        </Windows>
    </DoSRequestAnalysis>
</Configuration>
'));

select Configuration.value('(/Configuration/DoSRequestAnalysis/Windows/Window[@Name="Smallest"]/@Duration)[0]', 'smallint') from dbo.Configuration; -- I want to select the value of the attribute Duration i.e. 15 but this select returns null

如何编写查询以选择 Duration 属性?

非常感谢

4

1 回答 1

1

你很接近,只需将 [0] 更改为 [1]

select Configuration.value('(/Configuration/DoSRequestAnalysis/Windows/Window[@Name="Smallest"]/@Duration)[1]', 'smallint') 
于 2013-06-20T19:19:22.303 回答