【发布时间】:2021-07-28 12:05:41
【问题描述】:
我需要将 XML 和 SQL Server 表中的数据结合起来。我正在尝试这样做并面临以下错误。
消息 102,第 15 级,状态 1,第 52 行 “响应”附近的语法不正确。
XML...
set @sqlxml = N'<response xmlns="http://xyz.in/twa/cmm/decl/v2">
<identification>88762431</identification>
<type>RESPONSE</type>
<submitter><identifier>40134916C</identifier></submitter>
<functionalReference>TSW07389555IM1</functionalReference>
<transactionType>24</transactionType>
<attachDocument>
<category>AAA</category>
<mimeCode>application/pdf</mimeCode>
<URI>16f15574-5d5a-4e83-b9ac-2151f10cf2eb</URI>
<filename>XYZ_B2021_199.pdf</filename>
</attachDocument>
<attachDocument>
<category>AAB</category>
<mimeCode>text/plain</mimeCode>
<URI>1511b476-a2be-4ae5-a54c-0a5dc14759b2</URI>
<filename>XYZ_B2021_199_xml.txt</filename>
</attachDocument>
<additionalInformationICN><text>Please refer to attached XYZ for Directions</text></additionalInformationICN>
<issueDate>20210331113355</issueDate>
<overallDeclaration>
<identification>88762431</identification>
<functionalReference>TSW07389555IM1</functionalReference>
<submitter>
<identifier>40134916C</identifier>
</submitter>
<responsibleGovernmentAgency>XYZ</responsibleGovernmentAgency>
</overallDeclaration>
<status>
<agency>XYZ</agency>
<effectiveDate>20210331113355</effectiveDate>
<name>B04</name>
<releaseDate>20210331113355</releaseDate>
</status>
</response>'
SQL 脚本...
;WITH XMLNAMESPACES ('http://xyz.in/twa/cmm/decl/v2' AS ns)
SELECT distinct lv.[VERSION] LocationVersion,
lv.CHANGE_REASON LocationVersionChangeReason,
lv.CREATED_TIMESTAMP LocationVersionCreatedTimestamp,
lrd.CATEGORY,
lrd.MIME_TYPE as mimecode,
coalesce(lrd.[OBJECT_ID], lrd.FILENET_ID) as URI,
lrd.DOCUMENT_NAME as [FileName],
cast(lrd.SEQUENCE as bigint) sequence
response.value('(/response/status/agency/text())[1]','varchar(100)') as ResponseAgency,
response.value('(/response/issueDate/text())[1]','varchar(50)') as ResponseIssueDateTime,
response.value('(/response/additionalInformationICN/text/text())[1]','varchar(1000)') as ResponseClearanceInstructions
FROM DB.Location l
INNER JOIN DB.Location_RESPONSE lr ON l.Location_ENTITY_KEY = lr.Location_ENTITY_KEY
INNER JOIN DB.Location_RESPONSE_DOCUMENT lrd ON lr.Location_RESPONSE_KEY = lrd.Location_RESPONSE_KEY
LEFT OUTER JOIN DB.Location_VERSION lv on lr.Location_VERSION_KEY = lv.Location_VERSION_KEY
INNER JOIN DB.Location_RESPONSE lr2 on l.Location_ENTITY_KEY = lr2.Location_ENTITY_KEY
CROSS APPLY @sqlxml.nodes('/response') AS xmltable(response)
WHERE l.Llocation_ENTITY_KEY = 123456789
Sql 脚本工作正常,从数据库返回数据,不包括如下 XML...
;WITH XMLNAMESPACES ('http://xyz.in/twa/cmm/decl/v2' AS ns)
SELECT distinct lv.[VERSION] LocationVersion,
lv.CHANGE_REASON LocationVersionChangeReason,
lv.CREATED_TIMESTAMP LocationVersionCreatedTimestamp,
lrd.Category,
lrd.MIME_TYPE as Mimecode,
coalesce(lrd.[OBJECT_ID], lrd.FILENET_ID) as URI,
lrd.DOCUMENT_NAME as [FileName],
cast(lrd.SEQUENCE as bigint) [Sequence]
--response.value('(/response/status/agency/text())[1]','varchar(100)') as ResponseAgency,
--response.value('(/response/issueDate/text())[1]','varchar(50)') as ResponseIssueDateTime,
--response.value('(/response/additionalInformationICN/text/text())[1]','varchar(1000)') as ResponseClearanceInstructions
FROM DB.Location l
INNER JOIN DB.Location_RESPONSE lr ON l.Location_ENTITY_KEY = lr.Location_ENTITY_KEY
INNER JOIN DB.Location_RESPONSE_DOCUMENT lrd ON lr.Location_RESPONSE_KEY = lrd.Location_RESPONSE_KEY
LEFT OUTER JOIN DB.Location_VERSION lv on lr.Location_VERSION_KEY = lv.Location_VERSION_KEY
INNER JOIN DB.Location_RESPONSE lr2 on l.Location_ENTITY_KEY = lr2.Location_ENTITY_KEY
--CROSS APPLY @sqlxml.nodes('/response') AS xmltable(response)
WHERE l.Llocation_ENTITY_KEY = 123456789
有人可以帮忙吗?
提前致谢
【问题讨论】:
-
你忘记了行尾的逗号 cast(lrd.SEQUENCE as bigint) 序列
-
Larnu 经常喜欢说...
;是语句终止符,而不是语句开始符。拥有;WITH任何东西都是不好的形式。 -
尝试改用
WITH XMLNAMESPACES (default 'http://xyz.in/twa/cmm/decl/v2')。 -
还可以尝试从您的
response.value()XPaths 中删除/response/...@sqlxml.nodes()已经选择了/response元素,因此/response/...是多余的。 -
不清楚为什么你有
.nodes,只有一个<response>节点
标签: sql sql-server xml