我在六列的表格中有一行。其中一列称为MessageContent,是包含XML文档的nvarchar(max)。我想从表中检索所有六列:
SELECT col1
,col2
,col3
,MessageContent (element: ErrorMessage)
,col5
,col6
from tablea;
这甚至可能吗?我看过的所有示例都涉及返回XML,而我只想要元素的值:ErrorMessage。
这里是一个解决方案。需要注意两点:(1)命名空间处理(2)TRY_CAST()
处理数据库表中的“不良” XML。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY(1,1) PRIMARY KEY, MessageContent NVARCHAR(MAX));
INSERT INTO @tbl
VALUES (N'<?xml version="1.0"?>
<Acknowledgement xmlns="http://localhost/MeetingBrokerServices">
<DocumentId>60051951-8f28-47d3-8fd8-0ba89b537c87</DocumentId>
<TransactionId>62820a70-97f5-42b0-922e-a5f0908b9c8f</TransactionId>
<DocumentDate>2019-10-10T04:00:00.7475266Z</DocumentDate>
<StatusCode>NPE</StatusCode>
<Success>false</Success>
<Errors>
<Error>
<Code>301</Code>
<ErrorText>Invalid LocationIdentifier was received in the request. Valid LocationIdentifier must be populated and sent for this request to succeed. Request Details: ExternalRfpId: SecondaryExternalRfpId: RfpId: 12499772 SiteId: LocationIdentifierType: MeetingBroker LocationId: ExternalBookingId: 111.11 MbUserGuid: 625bb5f9-0bc7-4c7f-900a-a6436555ea19 RequestMetaDataGuid: BizTalk MessageId: c6e05156-4a35-4be4-b9fe-209173049539 Please see WebServiceTransactionLog and RequestMetaData for details.</ErrorText>
<ErrorType>Critical</ErrorType>
</Error>
</Errors>
</Acknowledgement>');
-- DDL and sample data population, end
;WITH XMLNAMESPACES(DEFAULT 'http://localhost/MeetingBrokerServices'), rs AS
(
SELECT *
, TRY_CAST(MessageContent AS XML) AS [config]
FROM @tbl
)
SELECT ID
, col.value('(ErrorText/text())[1]','VARCHAR(4000)') AS ErrorText
FROM rs AS tbl
CROSS APPLY tbl.[config].nodes('/Acknowledgement/Errors/Error') AS tab(col);