简体   繁体   English

如何将相似的数据批量复制到SQL Server 2005中的两个表?

[英]How to bulk copy similar data to two tables in SQL Server 2005?

I've 'inherited' some code that I am having a hard time working with. 我已经“继承”了一些我在工作中遇到的困难的代码。 The application has an excel import function to import members to a members table, and SqlBulkCopy is used. 该应用程序具有excel导入功能,可将成员导入到成员表中,并使用SqlBulkCopy

A few months ago a junction table was added and two of the attributes in the members table has to be added there as well for the application to work properly. 几个月前,添加了一个联结表,并且必须在该表中添加成员表中的两个属性,以使应用程序正常工作。 There are no hard coupling (no PK, FK - not my choice!). 没有硬耦合(没有PK,FK-不是我的选择!)。

I am not sure how to solve this, because as far as I know you can't bulkcopy to two tables, you will have to do it separately. 我不确定如何解决此问题,因为据我所知您不能批量复制到两个表,您将不得不分别进行操作。 But how can I retrieve the GUID attribute of the newly imported members as well as the other attribute values (groupId) in the best way (low impact on performance)? 但是,如何以最佳方式(对性能的影响较小)检索新导入的成员的GUID属性以及其他属性值(groupId)?

Example: 例:

Excel-import: Excel导入:

Name
Email
plus more

Table 1 表格1

name
personID (GUID)
groupID (same for all imported members)
+ other attributes

Table2 表2

personID (GUID)
groupID (same for all imported members)
+ other 'new' attributes

Sorry I can't provide any code this time :/ Really hope somebody can give me any advice! 抱歉,这次我无法提供任何代码:/真希望有人能给我任何建议!

It's not quite clear what exactly and how exactly you're trying to store the data from your Excel import into these two tables, and where that groupID comes from.... 还不清楚您试图将Excel数据存储到这两个表中的确切方式和确切方式,以及groupID来源。

Basically - you're right - you cannot bulk insert into multiple tables. 基本上-您是对的-您不能批量插入多个表。 So my take would be: 所以我的看法是:

1) Do the bulk insert from Excel into a Staging table as today 1)像今天一样,将Excel中的批量插入到Staging表中

2) Then insert those pieces of information you need to store in Table1 and output the necessary info that you need to "connect" the bits for Table1 to the bits for Table2 - something along the lines of: 2)然后插入需要存储在Table1那些信息,并输出将Table1的位“连接”到Table1 Table2的位所需的必要信息-类似以下内容:

DECLARE @Connection TABLE (GroupID INT, PersonID UNIQUEIDENTIFIER, EMail VARCHAR(500))

INSERT INTO dbo.Table1 (list of columns here)
    OUTPUT Inserted.GroupID, Inserted.PersonID, Inserted.EMail 
        INTO @Connection(GroupID, PersonID, EMail)
    SELECT
       (list of columns here)
    FROM
       dbo.Staging
    WHERE 
       (possibly a condition here???)

This would insert the rows from Staging into your Table1 , and while doing so, it will write out some information for each row inserted - the GroupID , PersonID and EMail . 这从插入的行Staging到您的Table1 ,虽然这样做,会写出来供插入每一行的一些信息-该GroupIDPersonIDEMail

With this information, you should be able to also insert your values into Table2 : 有了这些信息,您还应该能够将值插入到Table2

INSERT INTO dbo.Table2 (GroupID, PersonID, EMail, list of other columns here)
    SELECT
       c.GroupID, c.PersonID, c.EMail,
       (list of other columns from Staging table here)
    FROM @Connection c
    INNER JOIN dbo.Staging s ON c.EMail = s.EMail   -- or use whatever you can use to 
                                                    -- connect the two sets of data
    WHERE (condition) ......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM