0

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

样本 FF 来源:
Col1,StartDate,EndDate,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 |
+--------+------------+--------+

4

2 回答 2

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

于 2018-10-11T15:47:12.193 回答
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 或“日历表”,其中包含字段 Date(表示日历日期)和 Weekday(表示星期几的数值)。

于 2018-10-10T13:53:01.337 回答