从SQL Server中的XML数据类型读取标头标记值

时间:2015-05-19 23:52:38

标签: sql sql-server xml

如何在T-SQL中读取此XML对象的时间戳值

 <event name="rpc_starting" package="sqlserver" timestamp="2015-05-19T22:56:21.969Z">
    <data name="connection_reset_option">
    <value>1</value>
    <text>KeepDTCXact</text>
    </data>
</event> 

1 个答案:

答案 0 :(得分:2)

使用.value()这样的方法:

DECLARE @xml XML = '
<event name="rpc_starting" package="sqlserver" timestamp="2015-05-19T22:56:21.969Z">
    <data name="connection_reset_option">
    <value>1</value>
    <text>KeepDTCXact</text>
    </data>
</event>'

SELECT @xml.value('(/event/@timestamp)[1]', 'nvarchar(max)')

SELECT @xml.value('(/event/@timestamp)[1]', 'datetime')