SQL Xquery 从属性获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2912062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xquery get value from attribute
提问by Steven
I have some xml and need to extract values using sql
我有一些 xml,需要使用 sql 提取值
<?xml version="1.0" ?>
<fields>
<field name="fld_AccomAttic">
<value>0</value>
</field>
<field name="fld_AccomBathroom">
<value>1</value>
</field>
</fields>
</xml>
I need to get
column name
fld_AccomAttic
Value
1
我需要获取列名 fld_AccomAttic
值 1
The xml is held in a sql server 2005 db
xml 保存在 sql server 2005 db 中
I have used xquery before and it has worked.
我以前使用过 xquery 并且它有效。
How to extract these values?
如何提取这些值?
回答by Paul Talbot
SELECT <xmlfield>.value('(/xml/fields/field/@name)[1]', 'varchar(60)')
FROM <table>
WHERE <xmlfield>.value('(/xml/fields/field/value/)[1], 'int') = 1
Replace with your table and field names.
替换为您的表和字段名称。
回答by Steven
Figured it matey
想通了伙计
XMLData.value('(/xml/fields/field[@name = "fld_AccomAttic"]/value)[1]','varchar(50)')