我有这个疑问
;WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/DMF/2007/08'AS DMF)
SELECT
Res.Expr.value('(../DMF:ResultDetail/Operator/Attribute/@ResultValue)[1]', 'nvarchar(150)') AS [Trying_to_Show_ResultValue_from_XML_Below]
,CAST(Expr.value('(../DMF:ResultDetail)[1]', 'nvarchar(max)')AS XML) AS ResultDetail
FROM [SomeTable_With_EvaluationResults_XML_Column] AS PH
CROSS APPLY EvaluationResults.nodes('
declare default element namespace "http://schemas.microsoft.com/sqlserver/DMF/2007/08";
//TargetQueryExpression'
) AS Res(Expr)
生成 XML 类型的第二列 ResultDetail
<Operator>
<Attribute>
<?char 13?>
<TypeClass>DateTime</TypeClass>
<?char 13?>
<Name>LastBackupDate</Name>
<?char 13?>
<ResultObjType>System.DateTime</ResultObjType>
<?char 13?>
<ResultValue>638320511970000000</ResultValue>
<?char 13?>
</Attribute>
</Operator>
我试图在上面的 SELECT 子句的第一列中显示 ResultValue 属性。 有什么帮助如何建立这条线吗?谢谢你。
未提供最小可重现示例。所以,我是从臀部开始拍摄的。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, EvaluationResults XML);
INSERT @tbl (EvaluationResults) VALUES
(N'<DMF:ResultDetail xmlns:DMF="http://schemas.microsoft.com/sqlserver/DMF/2007/08">
<Operator>
<Attribute>
<?char 13?>
<TypeClass>DateTime</TypeClass>
<?char 13?>
<Name>LastBackupDate</Name>
<?char 13?>
<ResultObjType>System.DateTime</ResultObjType>
<?char 13?>
<ResultValue>638320511970000000</ResultValue>
<?char 13?>
</Attribute>
</Operator>
</DMF:ResultDetail>');
-- DDL and sample data population, end
WITH XMLNAMESPACES ('http://schemas.microsoft.com/sqlserver/DMF/2007/08'AS DMF)
SELECT t.ID
, c.value('(Attribute/ResultValue/text())[1]', 'VARCHAR(50)') AS ResultValue
, c.query('.') AS ResultDetail
FROM @tbl AS t
CROSS APPLY EvaluationResults.nodes('/DMF:ResultDetail/Operator') AS t1(c);