在SSIS包中,如何插入主从记录?
我有一项数据流任务,从一个数据库中提取数据并将其插入到 SQL Server 数据库中。 在主行中插入每条记录后,我还需要将行插入到详细表中。 明细表的数据非常简单,可以计算。
- 插入后如何检索标识列的值?
- 如何生成必须插入第二个表中的行? 我可以在脚本中执行此操作吗?
我是否需要在控制流级别使用 Foreach 循环来传输数据流任务中的父行,然后再使用另一个 Foreach 循环来插入详细记录?
我可以在脚本中执行所有详细信息行插入吗? 这可能比放入 Foreach 循环更容易。
I have a data flow task that extracts data from one database and inserts into a SQL Server database. After each record is inserted in the master row I also need to insert rows into a detail table. The data for the detail table is pretty simple and can be calculated.
- How do I retrieve the value of the identity column after inserting?
- How can I produce the rows that must be inserted in the second table? Can I do this in a script?
Do I need to use a Foreach loop at the control flow level which transfers the parent row in a data flow task, then have another Foreach loop which inserts the detail records?
Can I just perform all the detail row inserts in a script? That would probably be easier than putting in the Foreach loops.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种方法......
创建一个对象类型的变量。
创建一个“执行 SQL 任务”来获取源数据并将其加载到变量 (ADO.NET) 中。
创建一个“ForEach 循环容器”。
将成功连接器(绿色)从“执行 SQL 任务”拖到“ForEach 循环容器”。将循环容器上的枚举器更改为“foreach ADO 枚举器”,然后从“ADO 对象”中选择变量源变量”。
在循环中,您应该能够添加一个可以使用的“执行 SQL 任务”。
您应该能够在插入主表后使用 SCOPE_IDENTITY() 获取每个 ID 并使用它插入到明细表中。
Here's one approach..
Create a variable of type object.
Create a "Execute SQL Task" that grabs your source data and loads it into the variable (ADO.NET).
Create a "ForEach Loop Container.
Drag the Success connector (green) from the "Execute SQL Task" to the "ForEach Loop Container". Change Enumerator on the loop container to "foreach ADO Enumerator" and pick your variable from the "ADO object source variable".
Within your loop, you should be alble to add an "Execute SQL Task" that you can work with..
You should be able to use the SCOPE_IDENTITY() to get each ID after inserting into the master table and use that to insert into the detail table.
我遇到过这个问题,规模更大一些(导入深度嵌套的 XML)。 我能够利用 XML 源的一项功能,即在主“表”中创建一个代理键,然后在“子”表中作为“外键”重复该代理键。
这个想法是允许每个“表”到达数据库中的单独的临时表。 处理完所有行后,您可以使用这些“外键”进行任何需要同时创建主行和详细信息行的最终处理 - 即使在事务中(如果需要)也是如此。
这允许数据流任务的性能与数据的主从性质相结合。
I've had this issue, on a somewhat more massive scale (importing deeply-nested XML). I was able to take advantage of a feature of the XML Source, that creates a surrogate key in the master "table" that is then repeated in the "child" table as a "foreign key".
The idea is to allow each "table" to reach a separate staging table in the database. After all the rows have been processed, you can then use these "foreign keys" to do any final processing that requires both the master and detail rows to be created at the same time - even in a transaction if necessary.
This allows the performance of a data flow task, combined with the master-detail nature of your data.
当我迁移源中具有 ID 字段的数据时,我经常使用的一项技术是在目标主表中拥有一个名为“OldID”的列。 使用 SSIS 控件填充它,将原始 id 字段放入 OldID 中。 然后,后续的 SSIS 控件对已填充的主记录执行查找,其中 OldID 与源 ID 匹配,并以这种方式获取新创建的 ID。
One technique I use frequently when I'm migrating data that has an ID field in the source is to have a column called "OldID" in the destination master table. Populate it with a SSIS control putting the original id field into OldID. Then is a subsequent SSIS control perform a lookup against the already populated master records where the OldID matches the source ID and get the newly created ID that way.