我试图解析SQL Server 2014存储过程中的XML文件。
XML文件如下所示:
<PROJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<APPLICATION_ID>9204797</APPLICATION_ID>
<ACTIVITY>R44</ACTIVITY>
<ADMINISTERING_IC>AI</ADMINISTERING_IC>
<APPLICATION_TYPE>5</APPLICATION_TYPE>
<ARRA_FUNDED>N</ARRA_FUNDED>
<AWARD_NOTICE_DATE>01/11/2017</AWARD_NOTICE_DATE>
<BUDGET_START>01/01/2017</BUDGET_START>
<BUDGET_END>12/31/2017</BUDGET_END>
</row>
</PROJECT>
我的代码是
SELECT
nref.value('@APPLICATION_ID[1]','varchar(max)') APPLICATION_ID,
nref.value('@ACTIVITY[1]','varchar(max)') ACTIVITY
FROM
[ADMIN_Grant_Exporter_Files_XML]
CROSS APPLY
XMLData.nodes('//PROJECT/row') as R(nref)
WHERE
APPLICATION_ID = '9204797'
APPLICATION_ID
作为单独的列存储在表中。
我试过了
XMLData.nodes('//PROJECT/row')
以及所有组合,例如
XMLData.nodes('//PROJECT[1]')
XMLData.nodes('//row[1]')
感谢任何帮助。即使我删除WHERE
,我得到的所有内容都是空的,因为此时我在表格中只有一条记录。
答案 0 :(得分:3)
PROJECTS
方法中的根标记(nodes
)拼写错误。然后要提取APPLICATION_ID
,您需要使用value
这样的方法。
nref.value('(APPLICATION_ID)[1]', 'Int')
@
方法中使用 value
来读取属性,但APPLICATION_ID
是xml中的元素
您也不能在相同的选择查询Where
子句
DECLARE @xml XML ='<PROJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<APPLICATION_ID>9204797</APPLICATION_ID>
<ACTIVITY>R44</ACTIVITY>
<ADMINISTERING_IC>AI</ADMINISTERING_IC>
<APPLICATION_TYPE>5</APPLICATION_TYPE>
<ARRA_FUNDED>N</ARRA_FUNDED>
<AWARD_NOTICE_DATE>01/11/2017</AWARD_NOTICE_DATE>
<BUDGET_START>01/01/2017</BUDGET_START>
<BUDGET_END>12/31/2017</BUDGET_END>
</row>
</PROJECTS>' -- Here PROJECT should be PROJECTS
SELECT nref.value('(APPLICATION_ID)[1]', 'Int') APPLICATION_ID,
nref.value('(ACTIVITY)[1]', 'varchar(max)') ACTIVITY
FROM @xml.nodes('//PROJECTS/row') AS R(nref) -- Here PROJECT should be PROJECTS
WHERE nref.value('(APPLICATION_ID)[1]', 'Int') = 9204797