【发布时间】:2016-01-31 20:34:19
【问题描述】:
我有一个 SOAP 响应,需要将 soapenv:Body 的几乎所有字段导入 SQL Server 谷歌搜索和测试我构建了这个似乎有效的查询 但这并不是我想要的:
declare
@Root varchar(50)='/soap:Envelope/soap:Body/GetFeedbackResponse/',
@xDoc XML = '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ebl:RequesterCredentials soapenv:mustUnderstand="0" xmlns:ns="urn:ebay:apis:eBLBaseComponents" xmlns:ebl="urn:ebay:apis:eBLBaseComponents">
<ebl:NotificationSignature xmlns:ebl="urn:ebay:apis:eBLBaseComponents">r6revSiTXCP9SBBFUtpDAQ==</ebl:NotificationSignature>
</ebl:RequesterCredentials>
</soapenv:Header>
<soapenv:Body>
<GetFeedbackResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2015-09-06T11:20:48.528Z</Timestamp>
<Ack>Success</Ack>
<CorrelationID>428163922470</CorrelationID>
<Version>899</Version>
<Build>E899_INTL_APIFEEDBACK_17278558_R1</Build>
<NotificationEventName>Feedback</NotificationEventName>
<RecipientUserID>ebay_bestseller</RecipientUserID>
<EIASToken>nY+sHZ2PrBmdj6wVnY+sEWDETj2dj6AFlIajDpaEpAydj6x9nY+seQ==</EIASToken>
<FeedbackDetailArray>
<FeedbackDetail>
<CommentingUser>ebay_bestseller</CommentingUser>
<CommentingUserScore>42425</CommentingUserScore>
<CommentText>Great buyer - We Would Appreciate 5 STARS for Our Feedback!</CommentText>
<CommentTime>2015-09-06T11:20:45.000Z</CommentTime>
<CommentType>Positive</CommentType>
<ItemID>310541589307</ItemID>
<Role>Buyer</Role>
<FeedbackID>1064451206013</FeedbackID>
<TransactionID>549674542021</TransactionID>
<OrderLineItemID>310541589307-549674542021</OrderLineItemID>
</FeedbackDetail>
</FeedbackDetailArray>
<FeedbackDetailItemTotal>1</FeedbackDetailItemTotal>
<FeedbackScore>126</FeedbackScore>
<PaginationResult>
<TotalNumberOfPages>1</TotalNumberOfPages>
<TotalNumberOfEntries>1</TotalNumberOfEntries>
</PaginationResult>
<EntriesPerPage>25</EntriesPerPage>
<PageNumber>1</PageNumber>
</GetFeedbackResponse>
</soapenv:Body>
</soapenv:Envelope>'
;with xmlnamespaces('http://schemas.xmlsoap.org/soap/envelope/' as [soap], default 'urn:ebay:apis:eBLBaseComponents')
insert into Test (TS,Comment)
select
@xDoc.value('(/soap:Envelope/soap:Body/GetFeedbackResponse/Timestamp)[1]', 'nvarchar(max)'),
@xDoc.value('(/soap:Envelope/soap:Body/GetFeedbackResponse/FeedbackDetailArray/FeedbackDetail/CommentText)[1]', 'nvarchar(max)'),
........
第一个问题:由于有大约 50 个不同的 SOAP 响应,有些确实有多达 120 个字段,我一直在寻找一种解决方案,它可以展平所有字段并在相关表中自动导入 无需指示所有行:考虑到我使用的是 sqlServer 2012/2014 是否有解决方案,或者如果没有,是否有比我建议的更好/更快的方法?
第二:(如果没有更好的解决方案) 因为我有很多字段要导入,对于某些 SOAP,最多 120 个,并且由于 root 总是相同的,所以我想减少将变量放入路径第一部分的长度,以便拥有 例如
@xDoc.value('('+@Root+'Timestamp)[1]', 'nvarchar(max)')
而不是
@xDoc.value('(/soap:Envelope/soap:Body/GetFeedbackResponse/Timestamp)[1]', 'nvarchar(max)')
但我收到一个错误,例如第一个值必须是字面值:是否有任何周转?
第三:从文档中...需要查询
@xDoc.value('(/soap:Envelope/soap:Body/GetFeedbackResponse/Timestamp)[1]', 'nvarchar(max)')
返回一个值,否则引发错误 但在某些 SOAP 中,某些字段是可选的,因此并不总是存在: 我试过了
;with xmlnamespaces('http://schemas.xmlsoap.org/soap/envelope/' as [soap], default 'urn:ebay:apis:eBLBaseComponents')
insert into Test (TS,Comment)
select if (exists @xDoc.value('(/soap:Envelope/soap:Body/GetFeedbackResponse/Timestamp)')) then @xDoc.value('(/soap:Envelope/soap:Body/GetFeedbackResponse/Timestamp)[1]', 'nvarchar(max)')
....
但是会报错:哪个是正确的查询形式?
非常感谢
乔
【问题讨论】:
标签: tsql soap sql-server-2012 xquery