如何将 SQL 语句输出写入 CSV 文件?
我们有一个脚本任务来处理 flatflat,将数据插入数据库,然后将任何重复项(通过存储过程)记录到 SQL 表中,然后将其传递给查找该表的工作流任务 &将所有数据写入文件,然后截断表.
We have a script task that processes a flatflat, inserts data into the database, then records any duplicates (via a stored proccedure) into a SQL table, which then passes it to a work flow task that looks up that table & writes all data into a file then trucates the table.
问题是即使记录了 0 个错误,它也总是写入日志平面文件.
The problem is even when there is 0 errors recorded, it always writes an log flatfile.
有没有办法在重复日志表中有 > 0 条记录的地方写一个平面文件?
Is there a way to write a flatfile where there is > 0 records in the duplicate log table?
这里有一个可能的选项,可以让您摆脱没有记录的错误文件.
Here is a possible option that might give you an idea of getting rid of Error file that have no records.
以下是有关如何执行此操作的分步过程.在此示例中,我使用了一个名为 Country_State.csv
的 csv 文件,其中包含国家和州作为源文件.
Here is step-by-step process on how to do this. In this example, I have used a csv file named Country_State.csv
containing countries and states as the source file.
场景:
示例包将读取该文件,然后写入名为 Destination.txt
的文本文件.在这种情况下,错误文件 Error.txt
将被创建,但如果没有错误,稍后将被删除.在这里,我将文件存储在路径 c:\temp\
The sample package will read the file and then write to a text file named Destination.txt
. In this scenario, the error file Error.txt
will be created but later deleted if there are no errors. Here, I have the files stored in the path c:\temp\
分步流程:
- 在连接管理器部分,创建三个平面文件连接,即源、目标和错误.请参阅屏幕截图 #1.
-
Source
连接应指向 csv 文件路径c:\temp\Country_State.csv
.有关此文件的内容,请参阅屏幕截图 #2. -
Destination
连接应该指向一个名为c:\temp\Destination.txt
的文本文件. -
Error
连接应该指向一个名为c:\temp\Error.txt
的文本文件. - 创建一个名为
ErrorCount
的数据类型为 Int32 的变量. - 在控制流选项卡上,放置一个
数据流任务
,然后放置一个文件系统任务
. - 将数据流任务连接到文件系统任务.
- 右键单击数据流任务和文件系统任务之间的连接器.
- 在优先约束编辑器上,将Evaluation operation更改为
Expression
并粘贴值@ErrorCount == 0表达式 文本框中的代码>.
- 您的控制流应该如屏幕截图 #3 所示.
- 在数据流选项卡上的数据流任务中,拖放一个平面文件源并将其配置为使用
Source
连接管理器. - 放置一个平面文件目标并将其配置为使用
目标
连接管理器. - 将绿色输出箭头从平面文件源连接到平面文件目标.
- 在数据流选项卡上放置一个
Row Count
转换并将其配置为使用变量User:ErrorCount
. - 将红色输出箭头从平面文件源连接到行计数转换.
- 放置一个平面文件目标并将其配置为使用
Error
连接管理器. - 使用
Error
连接将 Row Count 的输出连接到平面文件目标. - 您的数据流任务应如屏幕截图 #4 所示.
- 在控制流选项卡上,双击文件系统任务.
- 在文件系统任务编辑器上,将 Operation 设置为
Delete file
并将 SourceConnection 设置为Error
.请参阅屏幕截图 #5. - 包执行前文件夹路径
C:\temp
的内容显示在屏幕截图 #6 中. - 数据流选项卡执行如屏幕截图 #7 所示.
- 控制流执行如屏幕截图 #8 所示.
- 包执行后文件夹路径
C:\temp
的内容如截图#9所示. - 为了显示这确实有效,我将
Source
连接管理器上的第二列更改为整数(即使状态名称是字符串),以便数据流任务重定向到Error
输出. - 场景 2 数据流选项卡执行如屏幕截图 #10 所示.
- 场景2 控制流执行如屏幕截图 #11 所示.请注意,文件系统任务没有执行,因为错误文件不为空.
- 场景2包执行后文件夹路径
C:\temp
的内容如截图#12所示.请注意,即使没有成功的行,文件 Destination.txt 也存在.这是因为该示例仅删除空的错误文件.
- On the connection manager section, create three flat file connections namely Source, Destination and Error. Refer screenshot #1.
-
Source
connection should point to the csv file pathc:\temp\Country_State.csv
. Refer screenshot #2 for the contents of this file. -
Destination
connection should point to a text file namedc:\temp\Destination.txt
. -
Error
connection should point to a text file namedc:\temp\Error.txt
. - Create a variable of data type Int32 named
ErrorCount
. - On the Control Flow tab, place a
Data Flow Task
and then place aFile System Task
. - Connect the Data Flow Task to File System Task.
- Right click on the connector between Data Flow Task and File System Task.
- On the Precedence Constraint Editor, change the Evaluation operation to
Expression
and paste the value@ErrorCount == 0
in the Expression textbox. - Your control flow should look like as shown in screenshot #3.
- Inside the data flow task on the data flow tab, drag and drop a Flat File Source and configure it to use
Source
connection manager. - Place a Flat File Destination and configure it to use
Destination
connection manager. - Connect the green output arrow from the Flat File Source to the Flat File Destination.
- Place a
Row Count
transformation on the data flow tab and configure it to use the VariableUser:ErrorCount
. - Connect the red output arrow from the Flat File Source to the Row Count transformation.
- Place a Flat File Destination and configure it to use
Error
connection manager. - Connect the output from Row Count to the Flat File Destination using
Error
connection. - Your data flow task should look like as shown in screenshot #4.
- On the Control Flow tab, double-click on the File System Task.
- On the File System Task Editor, set the Operation to
Delete file
and set the SourceConnection toError
. Refer screenshot #5. - Contents of the folder path
C:\temp
before package execution are shown in screenshot #6. - Data flow tab execution is shown in screenshot #7.
- Control flow execution is shown in screenshot #8.
- Contents of the folder path
C:\temp
after package execution are shown in screenshot #9. - To show this actually works, I changed the second column on the
Source
connection manager to integer (even though state names are strings) so that the data flow task redirects to theError
output. - Scenario 2 Data flow tab execution is shown in screenshot #10.
- Scenario 2 Control flow execution is shown in screenshot #11. Notice that the File System Task is not executed because the error file is not empty.
- Contents of the folder path
C:\temp
after Scenario 2 package execution are shown in screenshot #12. Notice that the file Destination.txt is present even though there were no successful rows. This is because the example deletes only the Error file if it is empty.
类似的逻辑可用于删除一个空的目标文件.
Similar logic can be used to delete a empty Destination file.
希望有所帮助.
屏幕截图 #1:
屏幕截图 #2:
屏幕截图 #3:
屏幕截图 #4:
屏幕截图 #5:
屏幕截图 #6:
屏幕截图 #7:
截图 #8:
屏幕截图 #9:
屏幕截图 #10:
屏幕截图 #11:
屏幕截图 #12: