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

问题描述 投票:0回答:2

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

样本FF来源: COL1,起始日期,结束日期,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 | 21/10/2018 | 1.000 | | 1234 | 28/10/2018 | 1.000 | + ------ + ------------ + ------- +

while-loop ssis dataflowtask flatfilesource oledbdestination
2个回答
0
投票

这是你如何采纳尼克斯的建议和实施:

--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或“Calendar Table”,其中包含Date(表示日历日期)和Weekday(表示星期几的数值)字段。


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”的新输出,它将您的单行转换为开始日期和结束日期之间的每周行。

© www.soinside.com 2019 - 2024. All rights reserved.