1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft SQL Server 2008 R2 Unleashed- P220 potx

10 78 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 209,05 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Enter the file storage type of field au_id [char]: Enter prefix length of field au_id [0]: Enter length of field au_id [11]: Enter field terminator [none]: or like this: Enter the file s

Trang 1

Enter the file storage type of field au_id [char]:

Enter prefix length of field au_id [0]:

Enter length of field au_id [11]:

Enter field terminator [none]:

or like this:

Enter the file storage type of field ProductID [int]:

Enter prefix length of field ProductID [0]:

Enter field terminator [none]:

By pressing the Enter key at the prompt, you accept the default Alternatively, you can

type your own value at the prompt if you know the new value and it is different from

the default

Creating a Format File by Using the format Switch

By using the formatoption, you can create a format file without actually transferring any

data Here is an example of creating a format file for the SalesOrderHeadertable in the

AdventureWorks2008database:

C:> BCP AdventureWorks2008.Sales.SalesOrderHeader format orders.dat

–S DBARCH-LT2\SQL08DE01

–U sa –P xyz –f orders.fmt –c

The format file created looks like this:

10.0

27

1 SQLCHAR 0 12 ““\t”” 1 SalesOrderID ““““

2 SQLCHAR 0 5 ““\t”” 2 RevisionNumber ““““

3 SQLCHAR 0 24 ““\t”” 3 OrderDate ““““

4 SQLCHAR 0 24 ““\t”” 4 DueDate ““““

5 SQLCHAR 0 24 ““\t”” 5 ShipDate ““““

6 SQLCHAR 0 5 ““\t”” 6 Status ““““

7 SQLCHAR 0 3 ““\t”” 7 OnlineOrderFlag ““““

8 SQLCHAR 0 50 ““\t”” 8 SalesOrderNumber SQL_

9 SQLCHAR 0 50 ““\t”” 9 PurchaseOrderNumber SQL_

10 SQLCHAR 0 30 ““\t”” 10 AccountNumber SQL_

11 SQLCHAR 0 12 ““\t”” 11 CustomerID ““““

12 SQLCHAR 0 12 ““\t”” 12 ContactID ““““

13 SQLCHAR 0 12 ““\t”” 13 SalesPersonID ““““

14 SQLCHAR 0 12 ““\t”” 14 TerritoryID ““““

15 SQLCHAR 0 12 ““\t”” 15 BillToAddressID ““““

16 SQLCHAR 0 12 ““\t”” 16 ShipToAddressID ““““

17 SQLCHAR 0 12 ““\t”” 17 ShipMethodID ““““

18 SQLCHAR 0 12 ““\t”” 18 CreditCardID ““““

19 SQLCHAR 0 15 ““\t”” 19 CreditCardApprovalCode SQL_

Trang 2

20 SQLCHAR 0 12 ““\t”” 20 CurrencyRateID ““““

21 SQLCHAR 0 30 ““\t”” 21 SubTotal ““““

22 SQLCHAR 0 30 ““\t”” 22 TaxAmt ““““

23 SQLCHAR 0 30 ““\t”” 23 Freight ““““

24 SQLCHAR 0 30 ““\t”” 24 TotalDue ““““

25 SQLCHAR 0 256 ““\t”” 25 Comment SQL_

26 SQLCHAR 0 37 ““\t”” 26 rowguid ““““

27 SQLCHAR 0 24 ““\r\n”” 27 ModifiedDate ““““

The following is a description of the lines and columns in the preceding format file example:

The first line shows the version of bcp

The second line shows the number of columns

The third line, first column shows the data field position

The third line, second column shows the data type

The third line, third column shows the prefix

The third line, fourth column shows the data file field length

The third line, fifth column shows the field or row terminator

The third line, sixth column shows the column position

The third line, seventh column shows the column name

The third line, eighth column shows the column collation

You get different format files depending on your table and whether you chose character,

native, or Unicode as the data type As you can see in the preceding example, only the last

two columns in the format file relate to the actual table; the remaining columns specify

properties of the data file

File Storage Types

The storage type is a description of how the data is stored in the data file Table 52.2 lists

the definitions used during interactivebcpand what appears in the format file The storage

type allows data to be copied as its base type (native format), as implicitly converted

between types (tinyinttosmallint), or as a string (in character or Unicode format).

TABLE 52.2 Storage Data Types

File Storage Type Interactive Prompt Host File Data Type

Trang 3

NOTE

If the table makes use of user-defined data types, these customized data types appear

in the format file as their base data type

If you are having problems loading certain fields into your table, you can try the

follow-ing tricks:

Copy the data in aschardata types and force SQL Server to do the conversion for you

Duplicate the table and replace all the SQL Server data types with charorvarcharof

a length sufficient to hold the value This trick allows you to further manipulate the

data with T-SQL after it is loaded

TABLE 52.2 Storage Data Types

File Storage Type Interactive Prompt Host File Data Type

uniqueidentifie

r

Trang 4

Prefix Lengths

To maintain compactness in native data files, bcpprecedes each field with a prefix length

that indicates the length of the data stored The space for storing this information is

speci-fied in characters and is called the prefix length.

Table 52.3 indicates the value to specify for prefix length for each of the data types

Prefix lengths are likely to exist only within data files created using bcp It is unlikely that

you will encounter a reason to change the defaults bcphas chosen for you

Field Lengths

When using either the native or character data format, you must specify the maximum

length of each field When converting data types to strings, bcpsuggests lengths large

enough to store the entire range of values for each particular data type Table 52.4 lists the

default values for each of the data formats

TABLE 52.3 Prefix Length Values

Prefix

Length

Data Types to Use

0 Non-null data of type bitor numerics (int,real, and so on) Use this value when

no prefix characters are wanted This value causes the field to be padded with

spaces to the size indicated for the field length

1 Non-null data of type binaryorvarbinaryor null data, with the exception of text,

ntext, and image Use this value for any data (except bit, binary, varbinary,

text, ntext, and image) that you want stored using a character-based data type.

2 When storing the data types binaryorvarbinaryas character-based data types,

2 bytes of charfile storage and 4 bytes of ncharfile storage are required for each

byte of binarytable data

4 For the data types text,ntext, and image.

TABLE 52.4 Default Field Lengths for Data Formats

Data Type Length (/c) Length (/n)

binary Column length × 2 Column length

Trang 5

NOTE

You must specify a field length that is long enough for the data being stored bcperror

messages regarding overflows indicate that the data value has been truncated in at

least one of the fields If the operation is a load, an overflow error usually results in

bcpterminating However, if you are dumping the data to a file, the data is truncated

without error messages

The field length value is used only when the prefix length is 0and you have specified no

terminators In essence, you are doing a fixed-length data copy bcpuses exactly the amount

of space stated by the field length for each field; unused space within the field is padded out

NOTE

Preexisting spaces in the data are not distinguished from added padding

Field Terminators

If you are not making use of fixed-width fields or length prefixes, you must use a field

terminator to indicate the character(s) that separates fields; for the last field in the data

row, you must also indicate which character(s) ends the line

bcprecognizes the indicators for special characters shown in Table 52.5

TABLE 52.4 Default Field Lengths for Data Formats

Data Type Length (/c) Length (/n)

TABLE 52.5 bcp Indicators for Special Characters

Trang 6

You cannot use spaces as terminators, but you can use any other printable characters You

should choose field and row terminators that make sense for your data Obviously, you

should not use any character you are trying to load You must combine the \rand\n

characters to get your data into an ASCII data file with each row on its own line

TIP

By specifying the –tand–rswitches, you can override the defaults that appear for the

prompts during interactive bcp

NOTE

You can specify terminators for data copied in native format You should be careful if

you decide to go this route; the accepted approach is to use lengthy prefixes

The prefix length, field length, and terminator values interact with one another In the

following examples, Tindicates the terminator character(s), Pindicates the prefix length,

andSindicates space padding

For data of type char, the data file has the following repeating pattern:

Prefix Length=0 Prefix Length=1,2,4

No Terminator stringSstringS PstringSPstringS

Terminator stringSTstringST PstringSTPstringST

Prefix Length=0 Prefix Length=1,2,4

No terminator stringSstringS PstringPstring

Terminator stringTstringT PstringTPstringT

For data of other types converted tochar, the data file has the following repeating pattern:

The next few sections examine how to load data into tables when there are differences in

column number and layout

Different Numbers of Columns in a File and Table

If you want to load data into tables when you have fewer fields in the data file than in the

table, you have to “dummy up” an extra line in your format file

Trang 7

Let’s suppose you want to load a data file that is missing most of the address information

for each customer (into a customer table of some kind that has full address columns in it)

To do this, you create a format file for this table by using theformatoption withbcp With

this format file, you can still load this abbreviated data easily Suppose that the data file

looks like this:

WELLI Wellington Importadora Jane Graham Sales (14)555-8122

(14)555-8111

WHITC White Clover Markets Donald Bertucci Owner (206)555-4112

(206)555-4113

To introduce a dummy value for the missing ones, in the format file, you need to make

the prefix and data lengths 0and set the field terminator to nothing (””) The modified

format file should look like this:

10.0

11

1 SQLCHAR 0 10 “\t” 1 CustomerID SQL_Latin1_General_

CP1_CI_AS

2 SQLCHAR 0 80 “\t” 2 CompanyName SQL_Latin1_General_

CP1_CI_AS

3 SQLCHAR 0 60 “\t” 3 ContactName SQL_Latin1_General_

CP1_CI_AS

4 SQLCHAR 0 60 “\t” 4 ContactTitle SQL_Latin1_General_

CP1_CI_AS

5 SQLCHAR 0 0 ““ 5 Address SQL_Latin1_General_

CP1_CI_AS

6 SQLCHAR 0 0 ““ 6 City SQL_Latin1_General_

CP1_CI_AS

7 SQLCHAR 0 0 ““ 7 Region SQL_Latin1_General_

CP1_CI_AS

8 SQLCHAR 0 0 ““ 8 PostalCode SQL_Latin1_General_

CP1_CI_AS

9 SQLCHAR 0 0 ““ 9 Country SQL_Latin1_General_

CP1_CI_AS

10 SQLCHAR 0 48 “\t” 10 Phone SQL_Latin1_General_

CP1_CI_AS

11 SQLCHAR 0 48 “\r\n” 11 Fax SQL_Latin1_General_

CP1_CI_AS

Nowbcpcan load the data file by using this new format file, with the Address,City,

Region, PostalCode, and Countrycolumns containing NULLvalues for the new rows

For data files that have more fields than the table has columns, you change the format file

to add additional lines of information Suppose that your customer data file contains an

additionalCreditStatusvalue at the end (shown here in bold italic):

Trang 8

WELLI Wellington Importadora Martin Sommer Sales Manager Rua do Mercado,

12 Resende SP 08737-363 Uraguay (14) 555-8122 NULL 1

WELP Well Drilling P Thierry Gerardin Sales Manager Rue de Vaugirard,

997 Paris FR 08737-363 France (11) 555-8122 NULL 1

WF WF Enterprises Yves Moison Sales Manager Rue de Sevres,

4123 Paris FR 08737-363 France (14) 555-8122 NULL 1

WGZR Wellsley Granite Jack McElreath Sales Manager Hillsboro,

131 Hillsboro MA 08737-363 USA (781) 555-8122 NULL 1

WHITC White Clover Markets Scott Smith Owner 305 - 14th Ave S.

Suite 3B Boston MA 98128 USA (508) 555-4112 (508) 555-4115 2

You need to modify a format file in two important areas: you change the second line to

reflect the actual number of values, and you add new lines for the extra column in the file

that is not in the table (from 11 to 12 entries) Notice that the column position has a

value of 0to indicate the absence of a column in the table The result is that your source

data file will import all data into the table, except the extra field (that is, the

CreditStatusfield)

Thus, the modified format file looks like this (where the bold italic indicates the

changes made):

10.0

12

1 SQLCHAR 0 10 “\t” 1 CustomerID SQL_Latin1_General_

CP1_CI_AS

2 SQLCHAR 0 80 “\t” 2 CompanyName SQL_Latin1_General_

CP1_CI_AS

3 SQLCHAR 0 60 “\t” 3 ContactName SQL_Latin1_General_

CP1_CI_AS

4 SQLCHAR 0 60 “\t” 4 ContactTitle SQL_Latin1_General_

CP1_CI_AS

5 SQLCHAR 0 120 “\t” 5 Address SQL_Latin1_General_

CP1_CI_AS

6 SQLCHAR 0 30 “\t” 6 City SQL_Latin1_General_

CP1_CI_AS

7 SQLCHAR 0 30 “\t” 7 Region SQL_Latin1_General_

CP1_CI_AS

8 SQLCHAR 0 20 “\t” 8 PostalCode SQL_Latin1_General_

CP1_CI_AS

9 SQLCHAR 0 30 “\t” 9 Country SQL_Latin1_General_

CP1_CI_AS

10 SQLCHAR 0 48 “\t” 10 Phone SQL_Latin1_General_

CP1_CI_AS

11 SQLCHAR 0 48 “\t” 11 Fax SQL_Latin1_General_

CP1_CI_AS

Trang 9

12 SQLCHAR 0 1 “\r\n” 0 CreditStatus SQL_Latin1_General_

CP1_CI_AS

These two examples show you the possibilities that the format file offers for customizing

the loading and unloading of data

Renumbering Columns

Using the techniques described in the section “Different Numbers of Columns in a File

and Table,” you can also handle data file fields that are in different orders than the target

tables All you need to do is change the column order number to reflect the desired

sequence of the columns in the table The fields are then automatically mapped to the

corresponding columns in the table

For example, suppose that a customer data file you got from another source system came

with the fields in this order:

1 Address

2 City

3 Country

4 PostalCode

5 Region

6 CompanyName

7 ContactName

8 ContactTitle

9 Fax

10 Phone

11 CustomerID

The SQL Server table has columns in a different order To load your data file into this

table, you modify the format file to look like this (where the bold italic indicates the

changes made):

10.0

11

1 SQLCHAR 0 10 “\t” 11 CustomerID SQL_Latin1_General_

CP1_CI_AS

2 SQLCHAR 0 80 “\t” 6 CompanyName SQL_Latin1_General_

CP1_CI_AS

3 SQLCHAR 0 60 “\t” 7 ContactName SQL_Latin1_General_

CP1_CI_AS

4 SQLCHAR 0 60 “\t” 8 ContactTitle SQL_Latin1_General_

CP1_CI_AS

5 SQLCHAR 0 120 “\t” 1 Address SQL_Latin1_General_

CP1_CI_AS

6 SQLCHAR 0 30 “\t” 2 City SQL_Latin1_General_

CP1_CI_AS

Trang 10

7 SQLCHAR 0 30 “\t” 5 Region SQL_Latin1_General_

CP1_CI_AS

8 SQLCHAR 0 20 “\t” 4 PostalCode SQL_Latin1_General_

CP1_CI_AS

9 SQLCHAR 0 30 “\t” 3 Country SQL_Latin1_General_

CP1_CI_AS

10 SQLCHAR 0 48 “\t” 10 Phone SQL_Latin1_General_

CP1_CI_AS

11 SQLCHAR 0 48 “\r\n” 9 Fax SQL_Latin1_General_

CP1_CI_AS

The principal point to remember with the format file is that all but the last three columns

deal with the data file The last three columns deal with the database table

Using Views

bcpcan use views to export data from a database This means an export of data can be a

result set of data from multiple tables (and with distributed queries, even multiple servers)

You can also use a view with bcpto load data back into tables However, as is the case with

normal T-SQL inserts, you can load into only one of the underlying tables at a time

Logged and Nonlogged Operations

Bulk-copy operations can occur in two modes: logged and nonlogged (also known as slow

and fastbcp, respectively) The ideal situation is to operate in nonlogged mode because this

arrangement dramatically decreases the load time and consumption of other system

resources, such as memory, processor use, and disk access However, the default runs the

load in logged mode, which causes the log to grow rapidly for large volumes of data

To achieve a nonlogged operation, the target table must not be replicated (the replication

log reader needs the log records to relay the changes made) The database holding the

target table must also have its SELECT INTO/BULK COPYoption set, and finally, the TABLOCK

hint must be specified

NOTE

Remember that setting the SELECT INTO/BULK COPYoption disables the capability to

back up the transaction log until a full database backup has been performed

Transaction log dumps are disabled because if the database had to be restored, the

transaction log would not contain a record of the new data

Although you can still perform fast loads against tables that have indexes, it is advisable to

drop and re-create the indexes after the data transfer operation is complete In other

words, the total load time includes the loading of the data and index creation time If

there is existing data in the table, the operation is logged; you achieve a nonlogged

opera-tion only if the table is initially empty

Ngày đăng: 05/07/2014, 02:20

TỪ KHÓA LIÊN QUAN