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

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

问题描述

我有一个SSIS软件包,其中有两条记录。我需要在表格中插入额外的一列(例如序列)。如果有两个记录,则序列列的值应为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目的地中,映射 NewID 列到目标身份列

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



    (2 )使用登台表



    类似于我在


    1. 使用身份列创建临时表

    2. 每次截断表(这将重新启动身份),然后将数据插入到临时表中

    3. 使用<$ c从临时表中插入数据$ c> DataFlow任务或执行SQL任务

    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天全站免登陆