将平面文件导入 SQL 服务器 table 时插入日期

Insert date while importing a flat file into SQL Server table

我正在尝试使用 SSIS 包将平面文件导入 SQL 服务器 table。此包裹计划每天 运行。我在导入文件之前已经创建了一个额外的列。

例如:

2019 年 1 月 8 日的平面文件:

001,Tony Stark,Ironman
014,Steve Rodgers, Captain America

2019 年 1 月 9 日的平面文件:

414,Peter Parker, Spiderman
007,Clark Kent, Superman  

预期结果:

导入后,table 在 01/08/2019 应该是这样的

ID      Name              Alias                 Date
````    ``````            ``````                `````
001     Tony Stark        Ironman               2019-01-08  
014     Steve Rodgers     Captain America       2019-01-08  

在 2019 年 1 月 9 日点赞

ID      Name              Alias                 Date
````    ``````            ``````                `````
001     Tony Stark        Ironman               2019-01-08  
014     Steve Rodgers     Captain America       2019-01-08 
414     Peter Parker      Spiderman             2019-01-09
007     Clark Kent        Superman              2019-01-09 

当你去加载csv文件到mysql,load it into a new mysql table

然后,使用 table alter to create a new date column. Update 新的 table,将列设置为所需的日期。

然后,使用 mysql insert-select 语法将新 table 中的值插入最终目标 table。

最后,删除 new/temporary table。

您可以通过多种方式实现此目的:

1- 将派生列添加到包中

您可以在数据流任务中添加派生列转换,并使用包含包开始时间的 [System::StartTime] 变量到 运行。或者您可以使用 GETDATE() 函数 returns 当前日期。

2- 为您添加的列添加默认值

您可以添加 GETDATE() 函数作为列的默认值,因此对于插入的每一行,都会分配当前日期的值。

ALTER TABLE DestTable ADD CONSTRAINT DF_SomeName DEFAULT GETDATE() FOR [Date];

参考资料