Tải bản đầy đủ - 0 (trang)
2-14. Executing BULK INSERT from SSIS

2-14. Executing BULK INSERT from SSIS

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

s



Figure 2-14. Bulk Insert connection details in SSIS

6.



Select Options in the left list to display the Options pane, as shown in Figure 2-15.



107

www.it-ebooks.info



Chapter 2 ■ Flat File Data Sources



Figure 2-15.  Bulk Insert connection options in SSIS

7.



Here you can set the BULK INSERT options, as described in Recipe 2-12 for T-SQL-based

BULK INSERT.



8.



Click OK to return to the Control Flow pane.



9.



Run the import.



How It Works

Using the SSIS Bulk Insert Task allows you to insert text data extremely fast into SQL Server, using most of the

options described in Recipe 2-12. Here you are essentially using SSIS as a “wrapper” for the T-SQL BULK INSERT

task. As with T-SQL BULK INSERT, a format file is not necessary—unless column mappings, skipping columns,

stripping out quotes, and so forth, is required.

In this recipe, I am presuming that you know how to create SSIS connection managers. If this is not the case,

then refer to other recipes on this book. You will need a destination table and a format file (if you need one, and/

or are using one).

The following BULK INSERT options can be selected from the Options drop-down menu:





Keep Nulls







Identity Insert



108

www.it-ebooks.info



Chapter 2 ■ Flat File Data Sources







Table Lock







Fire Triggers







Check Constraints



The Sorted option allows you to specify which source column(s) the source data is sorted on. This speeds up

the import if there is a clustered index on the sorted column(s) in the destination table.



Hints, Tips, and Traps





If you are not using a format file, select Specify, and then select the row and column

delimiters for a simple source file in the Connections pane.







To see how to use the Batch Size, First Row, and Last Row options, see Recipe 2-9.







As this is nothing more than an interface to the BULK INSERT command, the format file

creation and specification is identical to that described in Recipe 2-11.



2-15. Handling Complex Flat File Formats with a

Row Prefix in SSIS

Problem

You have a flat file that is not a simple set of pseudo-columns, but contains multiple row structures, each type

prefixed with a specific indicator. You need to load this file into SQL Server.



Solution

Use SSIS to identify and separate the record types into individual data flows. You then load each row structure

into appropriately structured separate destination tables.

I am assuming that you have a text file delivered with what are essentially multiple types of rows in it.

For this example, I will limit the extent of the file’s complexity to two different row types. We can consider

them as header rows and child rows. Here is a (very tiny) example

(C:\SQL2012DIRecipes\CH02\MultipleSubsets.Txt):

HDR:-3,3A9271EA-FC76-4281-A1ED-714060ADBA30,3,2011-04-01 00:00:00.000,500.00,250.00

LNE:-1,3,1,5000.00,0x00000000000007DB,,1

LNE:-2,3,2,12500.00,0x00000000000007DC,,2

HDR:-4,C9018CC1-AE67-483B-B1B7-CF404C296F0B,4,2011-09-01 00:00:00.000,0.00,500.00

LNE:-3,4,3,17250.00,0x00000000000007DD,,1

LNE:-4,4,4,52000.00,0x00000000000007DE,,2

LNE:-5,4,5,71000.00,0x00000000000007DF,,3

As you can see, each row begins with either HDR:- or LNE:-, which allows you (and SSIS) to determine which

type of record the row contains. The following explains how to handle this.



109

www.it-ebooks.info



Chapter 2 ■ Flat File Data Sources



1.



Create the two destination tables corresponding to the two types of records set in the

source file (C:\SQL2012DIRecipes\CH02\tblMultipleSubsets.sql):

CREATE TABLE InvoiceHeader

(

ID INT

,InvoiceNumber VARCHAR(50)

,ClientID INT

,InvoiceDate DATETIME

,TotalDiscount NUMERIC(18,2)

,DeliveryCharge

) ;

GO

CREATE TABLE InvoiceLine

(

ID INT

InvoiceID INT

StockID INT

SalePrice NUMERIC(18,2)

Timestamp BIGINT

DateUpdated DATETIME

LineItem INT

) ;

GO



2.



Create a new SSIS package and add an OLEDB connection manager named

CarSales_Staging to connect to the CarSales_Staging database. You may reuse an

existing package-level connection if you are using SQL Server 2012.



3.



Add a Flat File connection manager that you configure to connect to the source file

(C:\SQL2012DIRecipes\CH02\MultipleSubsets.Txt). While editing the connection

manager, and assuming that the row prefix is of fixed length, set the file format to

Ragged Right. In the Columns pane, set the column marker to divide the data into two

columns. The first will contain the row prefix, while the second will contain the row

data. This should look like what’s shown in Figure 2-16.



110

www.it-ebooks.info



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

2-14. Executing BULK INSERT from SSIS

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

×