Configure the Flat File Source in SSIS 2012 to read CSV files

Problem

I am new to SSIS and need to know how to read a comma-separated value (CSV) file into an SQL Server Integration Services (SSIS) 2012 package?  What are the steps that you need to follow?  Check out this tip to learn more.

Solution

For this solution, we will use the CSV file shown below. The file is named tip.csv and it has five columns and a header row.

The file is named tip.csv

The first step is to drag a Data Flow Task onto the package palette as shown below.

drag a Data Flow Task onto the package palette

Double click on the Data Flow Task or click on the Data Flow tab.

Next, drag a Flat File Source from the SSIS Toolbox onto the Data Flow palette as shown below.

drag a Flat File Source from the SSIS Toolbox

If you see a white X in the red circle, hover over the object to show the help message. The message below states that we need to set up a connection to the flat file.

hover over the object to show the help message

At this point there are two ways to initiate the setup of the flat file connection. The first way is to right click in the Connection Manager window and select “New Flat File Connection…” as shown below.

right click in the Connection Manager window

The second way is to double click on the Flat File Source to bring up the Flat File Source Editor and then click on “New…”.

double click on the Flat File Source

Either of the above ways will initiate the Flat File Connection Manager Editor.

Flat File Connection Manager Editor

Complete the Flat File Connection Manager Editor:

  • For the “Connection manager name” on the “General” tab, I typically will use the actual file name or the type of file if there are multiple files of the same layout.
  • Just remember that what is entered for the name will be displayed in the Connection Manager window.
  • For the “File name”, click on “Browse…” to navigate to the file or enter the file name manually. When using the browse feature, there are predefined filters for *.txt and *.csv files.
  • Because the Description column in our CSV file uses double quotes to qualify text strings, we must place a double quote in the “Text qualifier:” box.
  • For our file, the “Header row delimiter:” is the default of {CR}{LF} (carriage return/line feed).
  • “Header rows to skip:” also remains at the default of 0 because we only have one header row in this example and we make sure that “Column names in the first data row” is checked.

    Flat File Connection Manager Editor

    Now, we click on “Columns” tab to review the Columns page. We leave the “Row delimiter” set to the default of {CR}{LF} (carriage return/line feed). We leave the “Column delimiter” set to the default of Comma {,}. The preview section on this page allows us to see that SSIS is reading the file properly according to the format. This preview page will also allow us to make changes as necessary to accommodate the file’s format.

    Figure9

    Next, we click on “Advanced” tab to review the Advanced page. By default, SSIS sets the “DataType” for each column to string [DT_STR], “OutputColumnWidth” to 50, and “TextQualified” to True. Setting each column to the proper data type as it is read in from its source eliminates the need to convert the data type downstream.

    Figure10

    We change the data type of the ID column to a four-byte signed integer [DT_I4] and “TextQualified” to False.

    Figure11

    We change the data type of the Amount column to currency [DT_CY] and “TextQualified” to False.

    MSSQLTips.com Sample Image

    We change the length of the Category column to 1 and “TextQualified” to False.

    change the length of the Category column

    We change the data type of the EntryDate column to database date [DT_DBDATE] and “TextQualified” to False.

    change the data type of the EntryDate column

    We leave the attributes of the Description column unchanged because we want the DataType to be string [DT_STR], the OutputColumnWidth to be 50, and TextQualified to be True.

    leave the attributes of the Description column unchanged

    Click on the “Preview” tab to review another preview page. This preview page differs from the one shown previously because the user has the capability to skip a specified number of rows to look into their file the way the SSIS Flat File Source views it.

    Click on OK when you are done.

    SSIS Flat File Source

    On the “Connection Manager” page of the Flat File Source Editor, make sure the newly created Flat File connection is selected.

    the Connection Manager page of the Flat File Source Editor

    On the “Columns” page of the Flat File Source Editor, the columns to be output from the Flat File Source are displayed.

    the Columns page of the Flat File Source Editor

    On the “Error Output” page of the Flat File Source Editor, we will leave the default values to have SSIS fail the Flat File Source component on truncation or an error. Click the OK button when you are finished.

    the Error Output page of the Flat File Source Editor

    When setup correctly, the SSIS window Data Flow window should appear as shown below.

    the SSIS window Data Flow window should appear as shown below

    Next Steps

  • Leave a Reply

    Your email address will not be published. Required fields are marked *