gpt4 book ai didi

sql-server - 如何将xml数据插入到SQL Server表中?

转载 作者:行者123 更新时间:2023-12-02 03:42:16 25 4
gpt4 key购买 nike

如何将以下 XML 数据导入到具有三列的 SQL Server 表中?

<dataset> 
<metadata>
<item name="NAME_LAST" type="xs:string" length="62" />
<item name="NAME_FIRST" type="xs:string" length="62" />
<item name="NAME_MIDDLE" type="xs:string" length="32" />
</metadata>
<data>
<row>
<value>SMITH</value>
<value>MARY</value>
<value>N</value>
</row>
<row>
<value>SMITH2</value>
<value>MARY2</value>
<value>N2</value>
</row>
</data>
</dataset>

最佳答案

试试这个:

DECLARE @input XML = '<dataset> 
<metadata>
<item name="NAME_LAST" type="xs:string" length="62" />
<item name="NAME_FIRST" type="xs:string" length="62" />
<item name="NAME_MIDDLE" type="xs:string" length="32" />
</metadata>
<data>
<row>
<value>SMITH</value>
<value>MARY</value>
<value>N</value>
</row>
<row>
<value>SMITH2</value>
<value>MARY2</value>
<value>N2</value>
</row>
</data>
</dataset>'

INSERT INTO dbo.YourTable(ColName, ColFirstName, ColOther)
SELECT
Name = XCol.value('(value)[1]','varchar(25)'),
FirstName = XCol.value('(value)[2]','varchar(25)'),
OtherValue = XCol.value('(value)[3]','varchar(25)')
FROM
@input.nodes('/dataset/data/row') AS XTbl(XCol)

关于sql-server - 如何将xml数据插入到SQL Server表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21378729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com