使用 SSIS 插入记录时添加增量编号 [英] Add incremental number while inserting records using SSIS

查看:42
本文介绍了使用 SSIS 插入记录时添加增量编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 SSIS 包,其中有两条记录.我需要在表中插入一个额外的列(假设是序列)的记录.如果有两条记录,Sequence 列的值应为 1(对于第一条记录)和 2(对于第二条记录).再次,下一次,我得到三个记录,然后序列再次从 1,2 和 3 开始.

I have a SSIS package in which, two records are coming. I need to insert the records in the table with an extra column (let's say Sequence). If there are two records, Sequence column should have the value 1(for the first record) and 2(for the second record). Again, next time, I'm getting three records, then again sequence starts from 1,2 and 3.

有没有办法在不使用脚本或存储过程的情况下做到这一点?

Is there anyway to do this without using script or stored procedure?

截图:

推荐答案

有两种方法可以实现:

我认为使用脚本组件更高效,更可控,你可以自己写逻辑

  1. 在 DataFlow 任务中添加脚本组件
  2. 添加 DT_I4 类型的输出列 (例如:NewID)
  3. 在脚本编辑器中使用以下代码 (我使用 Visual Basic 语言)

Imports System  
Imports System.Data  
Imports System.Math  
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper  
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper  

<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute> _  
<CLSCompliant(False)> _  
Public Class ScriptMain  
    Inherits UserComponent 

    Private CurrentID as Integer  = 0


    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)  

        CurrentID += 1

        Row.NewID = CurrentID


    End Sub 

End Class

  • OLEDB Destination中,将NewID列映射到目标标识列

  • In the OLEDB Destination,Map the NewID column to the destination identity column

    (2) 使用临时表

    类似于我在 这个答案:

    1. 创建一个带有标识列的临时表
    2. 每次截断表(这将重新启动身份),并将数据插入临时表
    3. 使用DataFlow TaskExecute SQL Task 从Staging 表插入数据
    1. Create a staging table with an identity column
    2. Each time Truncate the Table (this will restart identity), and Insert data into staging table
    3. Insert data from Staging table using a DataFlow Task or an Execute SQL Task

    这篇关于使用 SSIS 插入记录时添加增量编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆