3

我有一份定期运行的报告,并将其放入暂存数据库进行审查。它通过 Microsoft 的 SQL Server 集成服务进行处理,用于转换数据;但是,我想添加一个“ID”列,每次运行趋势报告时都会以整数形式递增,但到目前为止还不能真正弄清楚如何做到这一点。

目标不是为每一行分配一个新编号,而是为一次运行的所有行分配相同的编号

理想情况下,通过 SSIS 中的派生列比更改 SQL 代码更容易,但我会考虑这两个选项。

RunID   Name
----- |----------------
 1    |  A
 1    |  B
 1    |  C
 1    |  D
 2    |  A
 2    |  B
 2    |  C
 3    |  A
4

3 回答 3

2

如果运行 ID 绑定到 SSIS 包的执行,则可以使用 ExecutionInstanceGUID。https://docs.microsoft.com/en-us/sql/integration-services/system-variables

您确定需要 SSIS 包来转换数据吗?大多数事情都可以在 T-SQL 中完成

于 2017-06-12T23:13:16.793 回答
2

Create a table with an IDENTITY column for the RunID. The table can hold whatever other information about the run you want, but at the very least I would include the run datetime.

Let the first step in your SSIS package be to insert a new row into the table and get the IDENTITY, and populate a package variable with the IDENTITY of the new row. That is your RunID which you can then do whatever you want with.

As a bonus, you'll have a permanent history record of your Runs, when they occurred, and whatever other meta data you choose to store about them.

于 2017-06-12T17:14:00.897 回答
1

您可以使用包变量来实现这一点

  1. 您可以添加一个存储整数的包变量 @[User::RunID]
  2. Execute SQL Task在每次执行包时,使用类似的查询从目标表中获取最大 RunID :

    select ISNULL(max(RunID),0) + 1 from stg_table
    
  3. 将此值添加到变量中ResultSet

这样,每次执行包时该值将增加 1

以下链接中有关 mappinf 结果到变量的更多详细信息:

于 2017-06-12T22:12:53.603 回答