Tải bản đầy đủ - 0 (trang)
2-2. Importing a Delimited Text File

2-2. Importing a Delimited Text File

Tải bản đầy đủ - 0trang


Figure 2-3. Flat File Source Editor in SSIS


Click New to specify the file connection, which opens the File Connection Manager

Editor. Browse to select the source file (or type it in if you prefer). In this example,

it is C:\SQL2012DIRecipes\CH02\Invoices.Txt. Specify whether column names

are in the first row, enter the text qualifier, and select the row delimiter (or leave the

defaults). You should end up with something like what you see in Figure 2-4.



Chapter 2 ■ Flat File Data Sources

Figure 2-4.  Defining the basic parameters of a flat file in the Import/Export Wizard


Click Advanced. For each output column, define the data type and length. The dialog box

should look like that in Figure 2-5.



Chapter 2 ■ Flat File Data Sources

Figure 2-5.  Defining data types in SSIS for a flat file


Click OK twice to confirm and return to the Data Flow pane.


Add an OLEDB destination from the toolbox to the Data Flow pane, and connect the

output (the green arrow) from the text source to the OLEDB destination.


Double-click the OLEDB destination to edit it. Click New to add an OLEDB

connection manager that you configure to connect to the destination database

(CarSales_Staging in this example.) As an alternative, you can select an existing

connection manager.


Select the destination table (dbo.Invoice in this example). Alternatively click New to

create a new table, which is designed automatically to map to the perceived source

structure. You can change the destination table name from the name suggested by

SSIS if you prefer. Confirm with OK.


Click Mappings in the list on the left side of the OLEDB Connection Manager dialog

box. Ensure that the source data maps to the destination columns.



Chapter 2 ■ Flat File Data Sources


Click OK.


Click Debug ➤ Start Debugging to import the data from the text file.

SSIS will now import your data.

How It Works

In the real-world of data import, SSIS is probably the tool that you will use the most. Nonetheless, it always helps if

you can open the source file to get a look at its contents, or have been informed about its contents enough to know

Whether it contains column headers or not.

What the data types and lengths are for each column of data.

That the file always contains the same number of column delimiters.

If you do not have the core information about the data in the text file available, however, then you can glean

metadata for the source file from SSIS by previewing the data and getting SSIS to guess the data types—as you

will discover in Recipe 2-3. Assuming that you have this information, SSIS will help you through the process of

importing a text file. In the case of flat files that are not too huge, you can open the source in a text editor and

attempt to deduce the metadata directly.

■■Note If you do not specify any data types when setting up the Data Flow task, SSIS presumes that every column

is VARCHAR(50).

Defining and adjusting the data types as the pass-through SSIS can mean a lot of work. Fortunately, SSIS

has ways to help you in this, as you will see in Recipe 2-3. Interestingly, the real work is done in the Flat File

connection manager, not the Data Source task.

Should you encounter difficulties, the first thing to look at is probably the basic text file specifications. If you

look at the Flat File Connection Manager dialog box, shown in step 5 of this recipe, you will see the items listed in

Table 2-1 in the General pane.



Chapter 2 ■ Flat File Data Sources

Table 2-1.  Flat File Connection Manager General Pane Options






Allows you to select a locale for the source data. This is used

when importing and sorting date and time data types.


Code Page

Gives the code page of the source data file. You can select from

one of the available source code pages—if you are certain

about the correct one to use.



Specifies that the source file is in Unicode, which makes

specifying a code page superfluous.


Text Qualifier

You can enter the text qualifier here if the source data encloses

text data in double quotes (or single quotes). Column

delimiters can be set globally at the file (general) level and then

overwritten for each individual column, if required.


Header row delimiter

Allows you to select the delimiter for the header row.


Header rows to skip

Defines the number of rows at the start of the file to skip

(including the header row).


Column names in the first

data row

Allows you to specify that the source file’s first row of data

contain column headings. These will then be used by SSIS in the

data flow.


Reset Columns

Resets all column definitions to the SSIS standard (VARCHAR(50)).

So how are you likely to use these elements? In my experience, the following are the ones that you are most

likely to need:

Text qualifier. Indicates that the data (of whatever type) is enclosed in quotes. This could

be, for instance, because the textual data contains the column delimiter (frequently a

comma). This way, such characters are handled gracefully. In any case, the quotes are

removed during the import process, obviating the need to remove them after import.

Column names in the first data row. Indicates that the first row consists of column

headings. As the name implies, the first row of the data is taken to be the column

names, and, consequently, is not imported.

2-3. Automatically Determining Data Types


You want to deduce the structure of the data in a source flat file.


Use the SSIS Suggest Types option in the Flat File connection manager.

I will explain the simple approach to getting a reasonable approximation of the real data types and lengths

in a source text file. This too is done using the advanced pane of the Flat File Connection Manager Editor. As this

recipe builds on Recipe 2-2, to avoid repetition, you need to have read the preceding recipe before using this one.



Chapter 2 ■ Flat File Data Sources


Follow steps 1 to 4 in Recipe 2-2.


Click Advanced.


Click Suggest Types. The Suggest Column Types dialog box appears, as shown in

Figure 2-6.

Figure 2-6.  Determining column types automatically in SSIS


Alter any options you wish to tweak (for instance, the number of rows may be far too

small for an accurate sample of a large file). Click OK.

You will see that all the data types (and lengths, where appropriate) have been sampled and adjusted in the

advanced pane of the dialog box (Figure 2-5).



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

2-2. Importing a Delimited Text File

Tải bản đầy đủ ngay(0 tr)