使用 XML 进行批量插入

发布于 2024-11-30 12:50:57 字数 7907 浏览 0 评论 0原文

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

我想将 列表插入到 [tbl_ReleaseHistory] 中,保持 为空。

此外,在将 插入同一表时, 应保持为空。

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

现在如何迭代 列表进行插入?

那么还有其他方法吗?

如何使用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  

谢谢

I am unable to write a stored procedure for bulk insertion into my table.

I want to insert list of <ObjectID> into [tbl_ReleaseHistory] keeping <FeatureID> null.

Also while inserting <FeatureID> into the same table <ObjectID> should remain null.

I am able to accomplish only the insertion of first <ObjectID> and <FeatureID> i.e 2218 and 67 from the xml passed for dummy execution.

Now how to iterate list of <objectID> for insertion?

So Is there any other method?

How to perform Bulk insert using 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  

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流云如水 2024-12-07 12:50:57

不能 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)

对于功能也类似。

Not 100% sure that I understand what you're trying to do.... but assuming this is your XML file:

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>'

you could easily select all ObjectID values using the SQL Server 2005 XQuery features (much easier to use than the old openxml stuff):

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

or the list of the feature ID's :

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

Now what do you want to do with those values??

Update: OK, so to insert the values, use this statement:

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)

and similarly for the features, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文