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.
The problem is even when there is 0 errors recorded, it always writes an log flatfile.
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.
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.
Scenario:
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\
Step by step flow:
- 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.
Hope that helps.
Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8:
Screenshot #9:
Screenshot #10:
Screenshot #11:
Screenshot #12:
You could try this..put a script before the task is executed to check the file size, and the add a "Precedence Constraint"...when TRUE, then proceed. with
Dim FileInfo As System.IO.FileInfo
you can obtain the file length like...FileInfo.Length.
精彩评论