I need to combine XML and data from tables of SQL Server. I am trying to do so and facing following error.
Msg 102, Level 15, State 1, Line 52 Incorrect syntax near 'response'.
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 Script...
;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 script is working fine return data from database without including XML as below...
;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
Can someone help pls?
Thanks in Advance

;is a statement terminator, not a statement beginninator. It's bad form to have;WITHanything.WITH XMLNAMESPACES (default 'http://xyz.in/twa/cmm/decl/v2')instead./response/from yourresponse.value()XPaths ...@sqlxml.nodes()has already selected the/responseelement so/response/...is redundant..nodesthere at all, there is only one<response>node