gpt4 book ai didi

c# - 使用 XML 批量插入

转载 作者:行者123 更新时间:2023-11-30 22:36:09 24 4
gpt4 key购买 nike

我无法编写用于批量插入到我的表中的存储过程。

我想插入 <ObjectID> 的列表进入[tbl_ReleaseHistory]保持<FeatureID>空。

同时插入 <FeatureID>进入同一张表<ObjectID>应保持为空。

我只能完成第一个 <ObjectID> 的插入和 <FeatureID>即来自为虚拟执行传递的 xml 的 2218 和 67。

现在如何迭代 <objectID> 的列表插入?

那么还有其他方法吗?

如何使用 XML 执行批量插入?

CREATE PROCEDURE [dbo].[spVersionAddReleaseHistory] @ApplicationMaster NTEXT
AS
/****************************************************************************
DESCRIPTION:
------------
This script is used to insert new record to the table tbl_VersionMaster.
MAINTENANCE LOG:
DATE AUTHOR DESCRIPTION
---- ------ -----------
01/07/2011 Isha Initial Creation

/*DUMMY EXECUTION : */
DECLARE @return_value int
EXEC @return_value = [dbo].[spVersionAddReleaseHistory]
@ApplicationMaster = N'<Root><ApplicationEntity>
<Id>0</Id>
<versionId>0</versionId>
<Versions>
<Version>
<Application>1111</Application>
<Version>11.11.123.123</Version>
<VersionMajor>11</VersionMajor>
<VersionMinor>11</VersionMinor>
<VersionBuild>123</VersionBuild>
<VersionRevision>123</VersionRevision>
<VersionFeatureIdList>
<FeatureID>67</FeatureID>
<FeatureID>68</FeatureID>
<FeatureID>69</FeatureID>
</VersionFeatureIdList>
<VersionObjectIdList>
<ObjectID>2218</ObjectID>
<ObjectID>2219</ObjectID>
<ObjectID>2220</ObjectID>
<ObjectID>2221</ObjectID>
<ObjectID>2222</ObjectID>
<ObjectID>2223</ObjectID>
<ObjectID>2224</ObjectID>
<ObjectID>2225</ObjectID>
<ObjectID>2226</ObjectID>
<ObjectID>2227</ObjectID>
<ObjectID>2228</ObjectID>
<ObjectID>2229</ObjectID>
</VersionObjectIdList>
<Components>
<Component>2218-Cmpjhhghghjghjg</Component>
<Component>2219-NEW </Component>
<Component>2220-OLD</Component>
</Components>
<Tables>
<Table>2221-t</Table>
<Table>2223-ty</Table>
</Tables>
<StoredProcedures>
<StoredProcedure>2226-tr</StoredProcedure>
<StoredProcedure>2227-trigr</StoredProcedure>
</StoredProcedures>
<Triggers>
<Trigger>2226-tr</Trigger>
<Trigger>2227-trigr</Trigger>
</Triggers>
<Features>
<Feature>2224-ffu</Feature>
<Feature>2225-ffffu</Feature>
</Features>
</Version>
</Versions>
</ApplicationEntity></Root>'
SELECT 'Return Value' = @return_value

****************************************************************************/
DECLARE @hDoc INT
EXEC Sp_xml_preparedocument
@hDoc OUTPUT,
@ApplicationMaster

-- SET identity_insert tbl_versionmaster ON
DECLARE @mylastident AS int

SET @mylastident = (SELECT max(VM_VersionId) from [tbl_VersionMaster])
BEGIN
INSERT INTO [tbl_ReleaseHistory]
( [RL_VersionId] ,
[RL_ObjectId] ,
[RL_FeatureId]
)
SELECT
@mylastident ,
ObjectID ,
NULL
FROM OPENXML(@hDoc,'Root/ApplicationEntity/Versions/Version/VersionObjectIdList',2)
WITH(
ObjectID INT ,
FeatureID INT
) xmlitems
END

BEGIN
INSERT INTO [tbl_ReleaseHistory]
( [RL_VersionId] ,
[RL_ObjectId] ,
[RL_FeatureId]
)
SELECT
@mylastident ,
NULL ,
FeatureID
FROM OPENXML (@hDoc,'Root/ApplicationEntity/Versions/Version/VersionFeatureIdList',2)
WITH (
ObjectID INT ,
FeatureID INT
) xmlitems
END
SET NOCOUNT OFF;
EXEC Sp_xml_removedocument @hDoc

谢谢

最佳答案

不能 100% 确定我理解您要做什么....但假设这是您的 XML 文件:

DECLARE @Input XML 
SET @Input = '<Root>
<ApplicationEntity>
<Id>0</Id>
<versionId>0</versionId>
<Versions>
<Version>
<Application>1111</Application>
<Version>11.11.123.123</Version>
<VersionMajor>11</VersionMajor>
<VersionMinor>11</VersionMinor>
<VersionBuild>123</VersionBuild>
<VersionRevision>123</VersionRevision>
<VersionFeatureIdList>
<FeatureID>67</FeatureID>
<FeatureID>68</FeatureID>
<FeatureID>69</FeatureID>
</VersionFeatureIdList>
<VersionObjectIdList>
<ObjectID>2218</ObjectID>
<ObjectID>2219</ObjectID>
<ObjectID>2220</ObjectID>
<ObjectID>2221</ObjectID>
<ObjectID>2222</ObjectID>
<ObjectID>2223</ObjectID>
<ObjectID>2224</ObjectID>
<ObjectID>2225</ObjectID>
<ObjectID>2226</ObjectID>
<ObjectID>2227</ObjectID>
<ObjectID>2228</ObjectID>
<ObjectID>2229</ObjectID>
</VersionObjectIdList>
.... (more stuff - not relevant here) ....
</Version>
</Versions>
</ApplicationEntity>
</Root>'

您可以使用 SQL Server 2005 XQuery 功能轻松选择所有 ObjectID 值(比旧的 openxml 东西更容易使用):

SELECT
V.VOI.value('(.)[1]', 'int')
FROM
@Input.nodes('/Root/ApplicationEntity/Versions/Version/VersionObjectIdList/ObjectID') V(VOI)

或功能 ID 的列表:

SELECT
F.FID.value('(.)[1]', 'int')
FROM
@Input.nodes('/Root/ApplicationEntity/Versions/Version/VersionFeatureIdList/FeatureID') F(FID)

现在你想用这些值做什么?

更新:好的,所以要插入值,请使用以下语句:

INSERT INTO dbo.tbl_ReleaseHistory(RL_VersionId, RL_ObjectId, RL_FeatureId) 
SELECT
V.VOI.value('(.)[1]', 'int'),
(some value for RL_ObjectId),
(some value for RL_FeatureId)
FROM
@Input.nodes('/Root/ApplicationEntity/Versions/Version/VersionObjectIdList/ObjectID') V(VOI)

特征也类似。

关于c# - 使用 XML 批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7146265/

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