BULK INSERT使用XML

时间:2011-08-22 10:41:48

标签: c# xml sql-server-2005 stored-procedures sql-server-openxml

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

我想在<ObjectID>中插入[tbl_ReleaseHistory]列表,保持<FeatureID> null。

同时,将<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  

由于

1 个答案:

答案 0 :(得分:3)

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

并且功能类似。