【问题标题】:SSIS - How to Insert from Flat File to OLE DB with date range?SSIS - 如何从平面文件插入到具有日期范围的 OLE DB?
【发布时间】:2019-03-14 21:03:19
【问题描述】:

我有一个平面文件源,其中的列每行都有开始日期和结束日期,而我尝试插入的表只有一个日期列,这意味着我必须从开始日期到每周插入 1 行结束日期。

样本 FF 来源:
Col1,开始日期,结束日期,Col4
1234,7/10/2018,28/10/2018,1.000

要插入到表中的行:
+--------+------------+--------+
| Col1 |日期 | Col4 |
+--------+------------+--------+
|第1234章2018 年 7 月 10 日 | 1.000 |
|第1234章2018 年 10 月 14 日 | 1.000 |
|第1234章21/10/2018 | 1.000 |
|第1234章2018 年 10 月 28 日 | 1.000 |
+--------+------------+--------+

【问题讨论】:

  • 到目前为止你做了什么?我建议您将文件按原样加载到临时表中,然后加入日历表并插入到最终表中。
  • 感谢您的回复。现在我有一个 FF 源 > Data Conv > Lookup - 检查来自 ff 的行是否存在于数据库中,如果不存在,则转到 No Match Output,即:>(有问题的任务)

标签: while-loop ssis dataflowtask flatfilesource oledbdestination


【解决方案1】:

这是您采纳尼克斯建议并实施的方式:

--This just replicates your load to a staging table
declare @t table (Col1 int,StartDate date,EndDate date,Col4 money)
insert into @t
values
(1234,'10/7/2018','10/28/2018',1.000)

--This will be your insert into final
select Col1,EndDate as [Date],Col4
from @t 

union all

select t.Col1,a.Date,t.col4
from @t t
cross apply (select * 
             from dDate 
             where [Date] >= t.StartDate --This includes start date
                 and [Date] < t.EndDate --This does not include ED, I did this to avoid result not ending on same day of the week
                 and [WeekDay] = datepart(dw,t.StartDate) --Weekly records starting on SD and progressing weekly
            )a
order by 1,2 -- Just for your viewing pleasure

结果:

Col1    Date        Col4
1234    2018-10-07  1.00
1234    2018-10-14  1.00
1234    2018-10-21  1.00
1234    2018-10-28  1.00

这假设您有一个 DateDimension 或“日历表”,其中包含字段 Date(表示日历日期)和 Weekday(表示星期几的数值)。

【讨论】:

    【解决方案2】:

    以下是关于如何在 SSIS 中使用脚本任务执行此操作的单独答案:

    1. 在数据流中添加读取平面文件的源
    2. 添加脚本组件并选择转换(连接到源)
    3. 转到输入并选择所有输入为只读
    4. 转到输入/输出并添加新输出并将其命名为 New
    5. 添加新列 Col1,Date,Column(带有数据类型)
    6. 转到脚本并输入并粘贴以下代码

      public override void Input0_ProcessInputRow(Input0Buffer Row)
      {
          int daysToAdd = 0;
          DateTime SD = Row.StartDate;
      
          while (SD.AddDays(daysToAdd) < Row.EndDate)
          {
              NewBuffer.AddRow();
              NewBuffer.Col1 = Row.Col1;
              NewBuffer.Date = SD.AddDays(daysToAdd);
              NewBuffer.Column = Row.Col4;
      
              daysToAdd = daysToAdd + 7;
          }
      
          //Add the end date
          NewBuffer.AddRow();
          NewBuffer.Col1 = Row.Col1;
          NewBuffer.Date = Row.EndDate;
          NewBuffer.Column = Row.Col4;
      
      }
      

    您现在将有一个名为“New”的新输出,它将您的单行转换为开始日期和结束日期之间的每周行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-26
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      相关资源
      最近更新 更多