使用SQL Server XML Import添加索引

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用SQL Server XML Import添加索引相关的知识,希望对你有一定的参考价值。

在SQL Server中,我尝试使用XML函数导入文件,如下所示:

SELECT 
    file.value('@registrationnumber[1]', 'nvarchar(100)') AS [registrationnumber],
    report.value('@nihil[1]', 'nvarchar(100)') AS [nihil],
    report.value('@period[1]', 'nvarchar(100)') AS [period],
    report.value('@formulaid[1]', 'nvarchar(100)') AS [formulaid],
    report.value('@frequention[1]', 'nvarchar(100)') AS [frequention],
    report.value('@versieon[1]', 'nvarchar(100)') AS [version],
    variant.value('@type[1]', 'nvarchar(100)') AS [varianttype],
    variant.value('@value[1]', 'nvarchar(100)') AS [variantvalue],
    post.value('@cube[1]', 'nvarchar(100)') AS [cube],
    post.value('@row[1]', 'nvarchar(100)') AS [row],
    post.value('@rownr[1]', 'nvarchar(100)') AS [rownr],
INTO
    [#parsed]
FROM
    @xmlData.nodes('file') AS a(file)
CROSS APPLY
    file.nodes('report') AS b(report)
CROSS APPLY
    report.nodes ('post') AS c(post)
OUTER APPLY
    report.nodes ('variant') AS d(variant)

我想知道是否有可能为每个加载的报告自动添加一个带有唯一索引的附加列。

我无法在谷歌上找到解决方案,因为我认为我使用的是错误的搜索字词。

答案

使用ROW_NUMBER()

SELECT 
    file.value('@registrationnumber[1]', 'nvarchar(100)') AS [registrationnumber],
    report.value('@nihil[1]', 'nvarchar(100)') AS [nihil],
    report.value('@period[1]', 'nvarchar(100)') AS [period],
    report.value('@formulaid[1]', 'nvarchar(100)') AS [formulaid],
    report.value('@frequention[1]', 'nvarchar(100)') AS [frequention],
    report.value('@versieon[1]', 'nvarchar(100)') AS [version],
    variant.value('@type[1]', 'nvarchar(100)') AS [varianttype],
    variant.value('@value[1]', 'nvarchar(100)') AS [variantvalue],
    post.value('@cube[1]', 'nvarchar(100)') AS [cube],
    post.value('@row[1]', 'nvarchar(100)') AS [row],
    post.value('@rownr[1]', 'nvarchar(100)') AS [rownr],
    ROW_NUMBER() OVER (ORDER BY (SELECT 1)) YourSequentialIndex
INTO
    [#parsed]
FROM
    @xmlData.nodes('file') AS a(file)
        CROSS APPLY
    file.nodes('report') AS b(report)
        CROSS APPLY
    report.nodes ('post') AS c(post)
        OUTER APPLY
    report.nodes ('variant') AS d(variant)

以上是关于使用SQL Server XML Import添加索引的主要内容,如果未能解决你的问题,请参考以下文章

使用 SQL Server 创建 XML

sql 使用XQuery方法查询,值,存在,节点在SQL Server中查询XML

sql server profiler 是啥文件

如何使用 Spark 从 XML 复制到 SQL Server

SQL Server 使用通用字段元素生成 XML

使用使用 Excel 标记的 SQL Server 生成 XML