从XML数据类型中选择一个值

时间:2013-06-20 19:16:09

标签: sql-server xml

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

想象一下以下示例:

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

如何编写查询以便选择持续时间属性?

非常感谢

1 个答案:

答案 0 :(得分:1)

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

select Configuration.value('(/Configuration/DoSRequestAnalysis/Windows/Window[@Name="Smallest"]/@Duration)[1]', 'smallint')