Tải bản đầy đủ - 0 (trang)
5-4. Load Data Exported from SQL Server from the Command Line

5-4. Load Data Exported from SQL Server from the Command Line

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

Chapter 5 ■ SQL Server Sources



The following are things to note from the start:





The destination table must exist in the destination database.







It is normally best to drop any indexes before the data load and re-create them afterward.







You can use a URL (\\server\share) rather than a drive letter to reference the source

data file. Nonetheless, I suggest that you consider copying the data to a local disk before

loading it for the reasons given in the previous recipe.







Any of the options described in Table 5-3 can be used when running BCP.







If tweaking all these case-sensitive flags in a command window is less than unhallowed

pleasure for you, then you can always write the BCP command as a script (.cmd) file and

run it by double-clicking.



There are many options that you may be required to use when loading data using BCP. Table 5-3 lists the key

flags which you might need one day.

Table 5-3.  BCP Options



Argument



Definition



Comments



-m



Maximum number of errors



Default = 10.



-n



The native (database) data

types



SQL Server data types are used, and all

character fields are non-Unicode.



–N



The native (database) data

types and Unicode data type

for character data



SQL Server data types are used, and all

character fields are Unicode.



-S



The server or server\instance If no server is specified, BCP connects to the

name

default instance of SQL Server on the local

computer.



-U



User



The login ID used to connect to SQL Server.



-P



Password



The user password.



-T



Integrated security



If used, then no –P and –U flags can be used.



-e



An (optional) error file



The file name and full path for the error file

used to log errors – if needed.



-F



The first row to be loaded



The default is the first row.



-L



The last row to be loaded



The default is the last row.



-b



The number of rows per

batch of imported data



Each batch is imported and logged in a

separate transaction. The whole batch must

be successfully imported before anything is

committed.



-V



Version



See Table 5-6.



-q



Quoted identifiers



This is used to specify a database, owner, table,

or view name that contains a space or a single

quotation mark. You must enclose the entire

three-part table or view name in quotation marks.

(continued)



252

www.it-ebooks.info



Chapter 5 ■ SQL Server Sources



Table 5-3.  (continued)



Argument



Definition



Comments



-a



Packet size



PacketSize can be from 4096 to 65535 bytes;

the default is 4096. Increasing this can make a

difference to throughput. You should, however,

always test any changes you make to PacketSize

to ensure that you are truly accelerating the

load.



-k



Null value handling



Specifies that empty columns should retain

a null value during the operation, rather

than have any default values for the columns

inserted.



-E



Keep Identity



Keeps IDENTITY data during an import.



-h "ORDER Colname1 ASC,

Colname2 DESC"



Sort hint



Tells BCP what the sort order of the source file

is.



-h "ROWS_PER_BATCH=n"



Batch size hint



The number of rows committed in a

transaction.



-h "KILOBYTES_PER_BATCH=n" Batch size hint



Approximate number of kilobytes of data per

batch (can be used to generate a more efficient

query plan that accelerates the load).



-h "TABLOCK"



Tablock hint



A table-level lock is applied during the load.



-h "CHECK_CONSTRAINTS"



Check and foreign key

constraints are applied

during the load



Default is not check constraints or foreign key

constraints applied during data load.



-h "FIRE_TRIGGERS"



Triggers fire during the load



Triggers not fired during data load.



Hints, Tips, and Traps





Remember that these arguments are case-sensitive!







When loading BCP files in an SQL Server native (−n or -N) format, you will not need to use

a format file. These are described in the next recipe.







BCP can load a single table in parallel. This is explained in Chapter 13.







Multiple hints can be used; an example is –h"TABLOCK,ORDER Col1 DESC".







Unlike a QueryOut, there is no concept of a QueryIn with BCP. You have to load the entire

table structure contained in the BCP file unless you use a format file. Of course, once

loaded into a staging table, you can then transfer data into a final table, but this is clearly

going to be very costly in terms of server resources.







You cannot use the –b (batch size) and the ROWS_PER_BATCH or KILOBYTES_PER_BATCH

hints together.



253

www.it-ebooks.info



Chapter 5 ■ SQL Server Sources







Since SQL Server 2005, BCP.exe no longer performs implicit data type conversion.







When data types do not match between the file and the target table, BCP.exe raises an

error if there is any data that would have to be truncated to fit into the target table.



5-5. Loading SQL Server Data from a Native SQL Server File

Problem

You want to load SQL Server data from another SQL Server; however, you want to SELECT the records in the BCP

file, change the column order, sort the data, and use standard T-SQL commands to shape the data.



Solution

Use OPENROWSET (BULK) and a format file to apply column mapping.

The following process not only loads data from an SQL Server native file, it also tweaks it on its way into the

destination table.

1.



Prepare the following format file (C:\SQL2012DIRecipes\CH05\Clients.fmt in the

sample files), which corresponds to the BCP output file, in the old “classic” format:

11.0

6

1

2 SQLNC

3 SQLN

4 SQLNCHAR

5 SQLNCHAR

6 SQLDATETIM4



2.



0

2

2

2

2

1



4

300

100

100

20

4



""

""

""

""

""

""



1

2

3

4

5

6



ID

ClientName

Town

County

ClientSize

ClientSince



""

Latin1_General_CI_AS

Latin1_General_CI_AS

Latin1_General_CI_AS

Latin1_General_CI_AS

""



Run the following T-SQL (C:\SQL2012DIRecipes\CH05\OpenRowsetShaped.Sql):

USE DATABASE CarSales;

GO



SELECT CAST(ID AS VARCHAR(5)) AS IdentityNumber, LEFT(Town, 25) AS Town

INTO

MyTableForShapedBulkInserts

FROM

OPENROWSET(

BULK 'C:\SQL2012DIRecipes\CH05\Clients.bcp'

,FORMATFILE='C:\SQL2012DIRecipes\CH05\Clients.fmt'

) as Cl

WHERE ClientSize = 'M'

ORDER BY County DESC;



This loads the source data into the MyTableForShapedBulkInserts table with slightly modified column

order and data types.



254

www.it-ebooks.info



Chapter 5 ■ SQL Server Sources



How It Works

One of the four ways we look at in this chapter to load a BCP file (and the second which is T-SQL-based) is to use

the OPENROWSET (BULK) command. The major difference between this approach and BCP is that here you can

use T-SQL to sort and shape the data as it flows through a perfectly standard T-SQL-based Data Flow on its way to

the destination table. However, a format file is absolutely indispensable in this case, and without it, the source file

cannot be loaded. This is why the only two fundamental parameters that must be passed to OPENROWSET (BULK)

are the source data file and the format file. Creating a format file is explained in Recipe 5-6.

OPENROWSET (BULK) has a more limited set of options than the two other techniques discussed previously.

Table 5-4 presents the list of available possibilities.

Table 5-4.  OPENROWSET (BULK) Options



Argument



Definition



Comments



FORMATFILE



A (compulsory) format file



The file name and full path of any format file used.



ERRORFILE



An (optional) error file



The file name and full path of any error log file used.



FIRSTROW



The first row to be loaded



The default is the first row.



LASTROW



The last row to be loaded



The default is the last row.



MAXERRORS



The maximum permissible

number of errors



The maximum permissible number of errors before

the load fails.



ORDER



Sort hint



Indicates the sort order of the source file.



ROWS_PER_BATCH



Batch size hint



Specifies the number of rows per transaction to be

committed.



CODEPAGE



The codepage to be used



‘ACP’, ‘OEM’, ‘RAW’, or the 'code_page' itself.



The hints provided in Table 5-5 may also be used.

Table 5-5.  OPENROWSET (BULK) Hints



Hint



Comments



TABLOCK



Apply a table lock.



IGNORE_CONSTRAINTS



Do not apply table constraints.



IGNORE_TRIGGERS



Do not apply table triggers.



KEEPDEFAULTS



Apply defaults to NULL columns.



KEEPIDENTITY



Keep IDENTITY data from the source file.



255

www.it-ebooks.info



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

5-4. Load Data Exported from SQL Server from the Command Line

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

×