SSIS-如何从平面文件插入到具有日期范围的OLE DB?

时间:2018-10-10 02:58:45

标签: while-loop ssis dataflowtask flatfilesource oledbdestination

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

样本FF来源:
Col1,StartDate,EndDate,Col4
1234,7 / 10 / 2018,28 / 10 / 2018,1.000

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

2 个答案:

答案 0 :(得分:0)

这是您采取Nicks建议并实施的方式:

--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或“日历表”,其中包含日期(代表日历日期)和工作日(代表星期几的数值)字段。

答案 1 :(得分:0)

以下是有关如何使用脚本任务在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”的新输出,该输出将单行转换为开始日期和结束日期之间的每周行。

相关问题