如何从表中的xml列获取列值

时间:2013-07-29 10:43:35

标签: sql sql-server sql-server-2008 xml-parsing sql-server-2008-r2

<value1>

<name>
 <account id="4567890" firstname="abc">
</account>
</name> 

</value1>

我需要在xml

中的帐户标记中取出id
SELECT 
       T.id
     , T.request_xml.value(
          '(Value1/name/account/id)[1]', 
          'varchar(50)'
       ) AS id
FROM [table] AS T

1 个答案:

答案 0 :(得分:1)

尝试使用@ -

DECLARE @XML XML
SELECT @XML = '
<value1>
<name>
 <account id="4567890" firstname="abc">
</account>
</name> 
</value1>'

SELECT id = @XML.value('(value1/name/account/@id)[1]', 'VARCHAR(50)') 

您的查询 -

SELECT 
       T.id
     , T.request_xml.value(
          '(Value1/name/account/@id)[1]', --<-- @
          'varchar(50)'
       ) AS id
FROM [table] AS T