Sunteți pe pagina 1din 13

WorkingwiththebcpCommandlineUtility

10December2009
byRobertSheldon
Eventhoughtherearemanyotherwaystogetdataintoadatabase,nothingworksquiteasfastas
BCP,onceitissetupwiththerightparametersandformatfile.Despiteitsusefulness,theartofusing
thecommandlineutilityhasalwaysseemedmoremagicthanmethodbutnowalongcomesRobert
Sheldontoshedlightonthemurkydetails.

hebcputilityisacommandlinetoolthatusestheBulkCopyProgram(BCP)APItobulkcopydatabetweenan
instanceofSQLServerandadatafile.Byusingtheutility,youcanexportdatafromaSQLServerdatabaseintoa
datafile,importdatafromadatafileintoaSQLServerdatabase,andgenerateformatfilesthatsupportimporting
andexportingoperations.
Tousethebcputilitytoperformthesetasks,youcanrunabcpcommand(alongwiththeappropriatearguments)ata
CommandPromptwindow.Thecommandshouldconformtothefollowingsyntax:>
bcp{table|view|"query"}
{out|queryout|in|format}
{data_file|nul}
{[optional_argument]...}

As you can see, a bcp command requires three arguments. The first (table|view|"query") represents the data
sourceordestinationinaSQLServerdatabase.Youcanusethebcputilitytoexportdatafromatableorviewor
throughaquery.Ifyouspecifyaquery,youmustencloseitinquotationmarks.Inaddition,youcanimportdataintoa
tableorview.Ifyouimportintoaview,allcolumnswithintheviewmustreferenceasingletable.(Notethat,whenyou
specifyatableorview,youmustqualifythenamewiththedatabaseorschemanamesasnecessary.)
Thesecondargumentinabcpcommand(out|queryout|in|format)determinesthecommandsmode(direction).
Whenyourunabcpcommand,youmustspecifyoneofthefollowingfourmodes:
out:Thecommandexportsdatafromatableorviewintoadatafile.
queryout:Thecommandexportsdataretrievedthroughaqueryintoadatafile.
in:Thecommandimportsdatafromadatafileintoatableorview.
format:Thecommandcreatesaformatfilebasedonatableorview.(Formatfilesareexplainedlaterinthe
article.)
Thethirdargumentinabcpcommand(data_file|nul)isthefullpathofthedatafileor,whenadatafileshouldnot
bespecified,thenulvalue.Ifyoureimportingdata,youmustspecifythefilethatcontainsthesourcedata.Ifyoure
exportingdata,youmustspecifythefilethatthedatawillbecopiedto.(Ifthefiledoesnotexist,itwillbecreated.)
Whenyoureusingthebcputilitytogenerateaformatfile,youdonotspecifyadatafile.Instead,youshouldspecify
nulinplaceofthedatafilename.
Inadditiontothethreerequiredarguments,youcanincludeoneormoreoptionalargumentswhenyouissueabcp
command. The bcp utility supports numerous optional arguments, and the ones you include often depend on the
mode you specify in the second argument. The remainder of this article provides examples that demonstrate how
manyoftheseargumentswork.Foradescriptionofalltheargumentssupportedbythebcputility,seethetopicbcp
UtilityinSQLServerBooksOnline.

ExportingDatafromaTableorView
Asmentionedabove,whenyouexportdataoutofatableorview,youmustspecifytheoutoption,alongwiththedata
source and destination file. The following bcp command copies data from the Employee table in the
AdventureWorks2008sampledatabaseandcopiesittotheEmployeeData.datfile.
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData.datS
localhost\SqlSrv2008T

As you would expect, the command includes the three required arguments: the source table

(AdventureWorks2008.HumanResources.Employee), the mode (out), and the full path name of the destination
datafile(C:\Data\EmployeeData.dat).Ifthedatafileexistswhenyourunthecommand,anydatawithinthefilewill
beoverwrittenwiththeexportedEmployeeinformation.Ifthefiledoesnotexist,itwillbecreatedandthedatawillbe
exported.
Thefourthargumentintheexample(Slocalhost\SqlSrv2008)specifiestheserverandinstanceofSQLServer.
You do not have to include the instance name if the source database is in the default instance. And if the source
databaseisinthedefaultinstanceonthelocalmachine,youdonothavetospecifytheSargumentatall,asinthe
followingexample:
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData.datT

Thelastargumentintheprecedingexamples(T)indicatesthatatrustedconnectionshouldbeusedtoconnectto
SQLServer.YoushouldusethisoptioniftheSQLServerinstanceusesintegratedsecurity.Ifintegratedsecurityisnot
used,youshouldinsteadspecifytheU argument, along with the login ID of an account that can access the SQL
Server instance. For example, the following bcp command specifies the login ID acct1 when accessing the SQL
Serverinstance:
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData.datS
localhost\SqlSrv2008Uacct1

Whenyourunthiscommand,youwillbepromptedforapassword.Alternatively,youcanincludethepasswordinthe
command as well by specifying the P argument, along with the accounts password (pw123), as shown in the
followingexample:
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData.datS
localhost\SqlSrv2008Uacct1Ppw123

However,MicrosoftgenerallyrecommendsthatyoudonotincludethePargumentandinsteadwaittobeprompted.
Buttheremightbecircumstancewhenyouwanttopassinthepasswordaspartofthecommand.
Whenyourunanyofthecommandsshownintheprecedingexamples,youwillbepromptedforinformationabout
each column in the source table or view. The following three prompts show you the type of data that you need to
supplyforeachcolumn:
EnterthefilestoragetypeoffieldBusinessEntityID[int]:
EnterprefixlengthoffieldBusinessEntityID[0]:
Enterfieldterminator[none]:

ThesepromptsarewhatyoureceivefortheBusinessEntityIDcolumnintheEmployeetable.Noticethateachprompt
includesarecommendedvalue,showninthebrackets.Toacceptthesuggestedsetting,pressEnterafteryoureceive
theprompt.OtherwiseenteravalueandthenpressEnter.Note,however,therecommendedsettingsaregenerally
yourbestoptions.(Formoreinformationabouteachprompt,seethetopicSpecifyingDataFormatsforCompatibility
byUsingbcpinSQLServerBooksOnline.)
Afteryourespondtoeachpromptforeachcolumn,youwillbeaskedwhetheryouwanttosavetheformatinformation
and,ifso,thefullpathnameoftheformatfile,asshowninthefollowingtwoprompts:
Doyouwanttosavethisformatinformationinafile?[Y/n]
Hostfilename[bcp.fmt]:

Asyoucanimagine,itcangetquiteannoyinghavingtosupplyformatinformationeachtimeyourunabcpcommand
whenexportingdata.Fortunately,thebcputilityincludesoptionsthatmakethisprocessmuchsimpler.Whendefining
your bcp command, you can include one of the following four arguments, which specify how the data should be
formatted:
n(nativeformat):Thebcputilityretainsthedatabasenativedatatypeswhenbulkcopyingthedatatothe
datafile.MicrosoftrecommendsthatyouusethisformattobulkcopydatabetweeninstancesofSQLServer.
However,youshouldusethisformatoptiononlywhenthedatafileshouldnotsupportextendedordouble
bytecharacterset(DBCS)characters.
N(Unicodenativeformat):Thebcputilityusesthedatabasenativedatatypesfornoncharacterdataand

usesUnicodeforcharacterdataforthebulkcopyoperation.Microsoftrecommendsthatyouusethisformatto
bulk copy data between SQL Server instances when the data file should support extended or DBCS
characters.
w(Unicodecharacterformat):ThebcputilityusesUnicodecharacterswhenbulkcopyingdatatothedata
file.ThisformatoptionisintendedforbulkcopyingdatabetweenSQLServerinstances.Note,however,that
theUnicodenativeformat(N)offersahigherperformancealternative.
c(characterformat):Thebcputilityusescharacterdataforthebulkcopyoperation.Microsoftrecommends
thatyouusethisformattobulkcopydatabetweenSQLServerandotherapplications,suchasMicrosoftExcel.
Whenyouincludeoneoftheseoptions,youarenotpromptedforformatinformation.Theformattingistakencareof
automatically.
Nowletslookatanexamplethatdemonstrateshowtheformatoptionswork.Thefollowingbcpcommandincludes
thenativeformatoption(n):
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData_n.datnS
localhost\SqlSrv2008T

Whenyouspecifythenargument,thedataisautomaticallycopiedtothefileinitsnativeformat.Inthenextexample,
thedataisalsoautomaticallysavedtothefilewithoutpromptingforformattinginformation:
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData_c.datcS
localhost\SqlSrv2008T

Notice that the c argument is specified, rather than n, as in the preceding example. As a result, the Employee
informationwillnowbesavedascharacterdata.
Whenthecharacterformat(c)isusedinabcpcommand,eachfield,bydefault,isterminatedwithatabcharacter,
andeachrowisterminatedwithanewlinecharacter.Youcanoverridethedefaultbehaviorbyusingthetargument
to specify a field terminator and the r argument to specify the row terminator. For example, the following bcp
commandspeciesthateachfieldbeterminatedwithacomma:
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData_c.datct,S
localhost\SqlSrv2008T

Asyoucansee,thecommandincludesthetargumentfollowingbyacomma,soeachfieldinthedatafilewillbe
terminatedwithacomma,ratherthanatab.Foracompletelistofthetypesofterminatorsyoucanuseandhowto
specifythem,seethetopicSpecifyingFieldandRowTerminatorsinSQLServerBooksOnline.
Uptothispoint,theexamplesIveshownyouhavecopieddatafromatableintoafile.However,youcanjustaseasily
copydatafromaview,asinthefollowingexample:
bcpAdventureWorks2008.HumanResources.vEmployeeoutC:\Data\EmployeeData_c.datct,S
localhost\SqlSrv2008T

Thiscommandisidenticaltotheprecedingexample,exceptthatitnowextractsdatafromthevEmployeeview,rather
thantheEmployeetable.
Whenyourbcpcommandretrievesdatafromatableorview,itcopiesallthedata.However,youhavesomecontrol
overwhichrowsarecopiedtothedatafile.Inabcpcommand,youcanusetheFargumenttospecifythefirstrowto
beretrievedandtheLargumenttospecifythelastrow.Inthefollowingexample,thefirstrowIretrieveis101andthe
lastrowis200:
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData_c.datcS
localhost\SqlSrv2008TF101L200
Nowthedatafilewillincludeonlythe100rowsthatfallwithinthespecifiedrange.
The bcp utility also supports arguments that are not specific to the data itself. For example, you can use the o
argumenttospecifyanoutputfile.Anoutputfilecapturestheinformationnormallyreturnedtothecommandprompt
afteryourrunabcpcommand.Inthefollowingexample,Iusetheoargumenttospecifythattheoutputbesavedto
theEmployeeOutput.txtfile:

bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData_c.datcS
localhost\SqlSrv2008ToC:\Data\EmployeeOutput.txt

Whenyourunthiscommand,anyoutputthatwouldhavebeendisplayedtotheconsoleisnowsavedtotheoutput
file.

ExportingDataReturnedbyaQuery
Alltheexamplesuptothispointhaveusedtheoutargumenttocopydatafromatableorview.Nowletslookatthe
queryoutargument,whichretrievesdatathroughaquery.Inthefollowingexample,IspecifyaSELECTstatement,
enclosedinquotationmarks,andthenspecifythequeryoutargument:
bcp"SELECT*FROMAdventureWorks2008.Person.Person"queryoutC:\Data\PersonData_n.datN
Slocalhost\SqlSrv2008TL100

Asyoucansee,thecommandretrievesdatafromthePersontableandusestheUnicodenativeformat(N)tosave
thedatatothefile.NoticethatthecommandalsoincludestheLargument,whichmeansthatonly100rowswillbe
retrieved from the table. However, when you use the queryout option rather than the out option, you can be as
specificinyourqueryasnecessaryyoucanincludemultipletablesandyoucanqualifyyourqueriesasnecessary.
Forexample,inthefollowingstatement,IincludetheTOP100clauseintheSELECTstatement,ratherthaninclude
theLargument:
bcp"SELECTTOP100*FROMAdventureWorks2008.Person.PersonORDERBYBusinessEntityID"
queryoutC:\Data\PersonData_c.datct,Slocalhost\SqlSrv2008T

NoticethatthequeryalsoincludesanORDERBYclausesothedatainthefilewillbeorderedbyBusinessEntityID.
This, of course, is still a very simple query, but it does demonstrate how to use the queryout argument in a bcp
commandandhowsimilarthismodeistoacommandthatcontainstheoutargument.Youstillspecifythedatafile,
theformat,andanyotherapplicableoptions.Nowletslookathowtoimportdata.

ImportingDataintoaTable
WhenyouusethebcputilitytoimportdataintoaSQLServertable,youmustspecifytheinmode,ratherthanoutor
queryout.Todemonstratehowtousetheinargumentinabcpcommand,IusedthefollowingTransactSQLtocreate
theEmployeestable:
USEAdventureWorks2008
IFOBJECT_ID('Employees','U')ISNOTNULL
DROPTABLEdbo.Employees
CREATETABLEdbo.Employees

(
BusinessEntityIDintNOTNULLIDENTITYPRIMARYKEY,
NationalIDNumbernvarchar(15)NOTNULL,
LoginIDnvarchar(256)NOTNULL,
OrganizationNodehierarchyidNOTNULL,
OrganizationLevelsmallintNULL,
JobTitlenvarchar(50)NOTNULL,br>BirthDatedateNOTNULL,
MaritalStatusnchar(1)NOTNULL,
Gendernchar(1)NOTNULL,
HireDatedateNOTNULL,
SalariedFlagFlagNOTNULL,
VacationHourssmallintNOTNULL,
SickLeaveHourssmallintNOTNULL,
CurrentFlagFlagNOTNULL,br>rowguiduniqueidentifierNOTNULL,
ModifiedDatedatetimeNOTNULL
)

IcreatedthetableintheAdventureWorks2008sampledatabaseinaSQLServer2008instance.AfterIcreatedthe
table,Iranthefollowingbcpcommandtocreateadatafilethatcontainsemployeetestdata:
bcpAdventureWorks2008.HumanResources.EmployeeoutC:\Data\EmployeeData_c.datct,S

localhost\SqlSrv2008T

WecannowusethisdatafiletodemonstratehowtoimportdataintotheEmployeestable.
Whenyouimportdataintoatable,youmustspecifythetable(orupdatableview)andtheinargument,asshownin
thefollowingexample:
bcpAdventureWorks2008.dbo.EmployeesinC:\Data\EmployeeData_c.datct,S
localhost\SqlSrv2008T

Noticethatyoumustalsospecifythesourcedatafile(C:\Data\EmployeeData_c.dat)andtheoptionsthatdefine
theformatofthedataasitwassavedtothefile:characterformat(c)andfieldterminator(t,).Ifyoudonotspecify
thecorrectformatoptions,youwillreceiveanerrorwhenyoutrytoimportthedata.
You might have noticed that the BusinessEntityID column in the Employees table is configured as an IDENTITY
column.Asaresult,whenyouimportthedata,thedatabaseengine,bydefault,ignorestheBusinessEntityIDvalues
thatareinthedatafileandgeneratesitsownIDs.However,youcanoverridethedefaultbehaviorbyspecifyingtheE
argument,asshowninthefollowingexample:
bcpAdventureWorks2008..EmployeesinC:\Data\EmployeeData_c.datct,S
localhost\SqlSrv2008TE

Nowwhenyouimportthedata,theBusinessEntityIDvaluesinthedatafilewillbeloadedintothetable,ratherthan
newvaluesbeinggenerated.
YoucanalsoorderthedatathatyouimportintothetablebyusingthehargumentalongwiththeORDERhint:
bcpAdventureWorks2008..EmployeesinC:\Data\EmployeeData_c.datct,S
localhost\SqlSrv2008TEhORDER(BusinessEntityID)

AfterIspecifyhORDER,Iprovidethenameofthecolumn(inparentheses)whosevaluesshouldbesorted.Ifyou
wanttosortmultiplecolumns,youmustseparatethecolumnswithacomma.
Thehargumentsupportsmultiplehints.Anotherone,forexample,istheTABLOCKhint,whichspecifiesthatabulk
update tablelevel lock should be acquired during the bulk operation. The following bcp command uses the
TABLOCKhint:
bcpAdventureWorks2008..EmployeesinC:\Data\EmployeeData_c.datct,S
localhost\SqlSrv2008ThTABLOCK

BecausethecommandincludestheTABLOCKhint,thedatabaseenginewillholdthelockforthedurationofthebulk
loadoperation,whichsignificantlyimprovesperformanceoverthedefaultrowlevellocks.
Whenyourunabcpcommand,youcanalsospecifythenumberofrowsperbatchofimporteddata.Tospecifythe
batch size, include the b argument, along with the number of rows per batch. For example, the following bcp
commandlimitseachbatchto100rows:
bcpAdventureWorks2008..EmployeesinC:\Data\EmployeeData_c.datct,S
localhost\SqlSrv2008Tb100

Thebcputilityalsoletsyouspecifyanerrorfilethatstoresanyrowsthattheutilitycannotcopyfromadatafileintoa
table.Tospecifyanerrorfile,usetheeargument,followedbythefullpathnameofthefile,asshowninthefollowing
example:
bcpAdventureWorks2008..EmployeesinC:\Data\EmployeeData_c.datct,S
localhost\SqlSrv2008Tec:\Data\EmployeeErrors.txt

When you run this command, any rows that cannot be imported into the Employees table will be saved to the
EmployeeErrors.txtfile.

UsingFormatFilestoImportandExportData
Thebcputilityletsyoucreateformatfilesthatstoreformatinformationthatcanbeusedtoimportandexportdata.
Eachformatfilecontainsformattinginformationaboutthedatainthedatafileaswellasinformationaboutthetarget
databasetable.Essentially,theformatfilemapsthefieldsinthedatafiletothecolumnsinthetable.Byusingformat
files,youhavefarmoreflexibilityinthetypeofdatafilesyoucanuse,allowingyoutoworkwithdatafilesthatcanbe
sharedbyotherapplicationsorusedfordifferentSQLServertables.
Youcanusethebcputilitytocreateaformatfile.Onceyouvecreatedthefile,youcanthenmodifyitasnecessaryto
supportbulkcopyoperations.Tocreateaformatfile,youmustspecifyformatforthemodeandnulforthedatafile.In
addition, you must also include the f argument, followed by the full path name of the format file, as shown in the
followingexample:
bcpAdventureWorks2008.Person.Personformatnulct,fC:\Data\PersonFormat_c.fmtS
localhost\sqlsrv2008T

Asyoucansee,theformatfileisbasedonthePersontable.Inaddition,thedatawillbesavedtothedatafilewiththe
characterformatandacommaasthefieldterminator.Theformatfileproducedbythiscommandwilllooksimilartothe
following:
10.0
13
1SQLCHAR012","1BusinessEntityID""
2SQLCHAR04","2PersonTypeSQL_Latin1_General_CP1_CI_AS
3SQLCHAR03","3NameStyle""
4SQLCHAR016","4TitleSQL_Latin1_General_CP1_CI_AS
5SQLCHAR0100","5FirstNameSQL_Latin1_General_CP1_CI_AS
6SQLCHAR0100","6MiddleNameSQL_Latin1_General_CP1_CI_AS
7SQLCHAR0100","7LastNameSQL_Latin1_General_CP1_CI_AS
8SQLCHAR020","8SuffixSQL_Latin1_General_CP1_CI_AS
9SQLCHAR012","9EmailPromotion""
10SQLCHAR00","10AdditionalContactInfo""
11SQLCHAR00","11Demographics""
12SQLCHAR037","12rowguid""
13SQLCHAR024"\r\n"13ModifiedDate""

The file includes such details as the data types of the data in the data file (second column), the field and row
terminators(fifthcolumn),andamappingbetweentablecolumnsandthefieldsinthedatafile(sixthcolumn).Note,
however,thatafullexplanationofformatfilesisbeyondthescopeofthisarticle,andyoushouldrefertothetopic
FormatFilesforImportingorExportingDatainSQLServerBooksOnlineforacompleteexplanationofhowthefiles
work.Themainfocusofthissectionistoshowyouhowtousethebcputilitytocreateformatfilesandusethemto
exportandimportdata.
Returningtotheformatfileabove,noticethatitlistsSQLCHARasthedatatypeforallfieldsinthedatafile.Thisis
becausethecharacterformat(c)isusedtocreatetheformatfile.However,youcanalsouseoneoftheotherformat
optionstocreatetheformatfile.Forexample,thefollowingbcpcommandincludesthenativeformat(n) argument,
ratherthanthecharacterformat:
bcpAdventureWorks2008.Person.PersonformatnulnfC:\Data\PersonFormat_n.fmtS
localhost\sqlsrv2008T
Nowthedatatypesforthedatafilewillshowthenativedatatypes,asshowninthefollowing:
10.0
13
1SQLINT04""1BusinessEntityID""
2SQLNCHAR24""2PersonTypeSQL_Latin1_General_CP1_CI_AS
3SQLBIT11""3NameStyle""
4SQLNCHAR216""4TitleSQL_Latin1_General_CP1_CI_AS
5SQLNCHAR2100""5FirstNameSQL_Latin1_General_CP1_CI_AS
6SQLNCHAR2100""6MiddleNameSQL_Latin1_General_CP1_CI_AS
7SQLNCHAR2100""7LastNameSQL_Latin1_General_CP1_CI_AS
8SQLNCHAR220""8SuffixSQL_Latin1_General_CP1_CI_AS
9SQLINT04""9EmailPromotion""
10SQLNCHAR80""10AdditionalContactInfo""
11SQLNCHAR80""11Demographics""
12SQLUNIQUEID116""12rowguid""
13SQLDATETIME08""13ModifiedDate""

Asyoucansee,becausethenativeformatisspecified,thefieldandrowterminatorsarenolongerrequired,butthe
prefixlength(thethirdcolumn)isnowspecified.Theprefixlengthindicateshowmuchspacethefieldrequires.Again,
refertoSQLServerBooksOnlineforacompletedescriptionofformatfiles.
ThefilecreatedaboveisonlyoneoftwotypesofformatfilessupportedbySQLServer2005and2008.Theformatfile
above is a nonXML format file, the original form of the file supported by earlier versions of SQL Server (and still
supportedinSQLServer2005and2008).However,anewertypeofformatfiletheXMLformatfilewasintroduced
inSQLServer2005.TheXMLformatfileismoreflexibleandpowerfulthantheoriginalnonXMLformatfile.
TocreateanXMLformatfile,yourunacommandsimilartotheprecedingtwoexampleshowever,youmustalso
includethexargument,asshowninthefollowingexample:
bcpAdventureWorks2008.Person.Personformatnulct,fC:\Data\PersonFormat_c.xmlS
localhost\sqlsrv2008xT

NowthedataformattinginformationisstoredasXML:
<?xmlversion="1.0"?>
<BCPFORMATxmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<RECORD>
<FIELDID="1"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="12"/>
<FIELDID="2"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="4"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="3"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="3"/>
<FIELDID="4"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="16"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="5"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="6"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="7"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="8"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="20"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="9"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="12"/>
<FIELDID="10"xsi:type="CharTerm"TERMINATOR=","/>
<FIELDID="11"xsi:type="CharTerm"TERMINATOR=","/>
<FIELDID="12"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="37"/>
<FIELDID="13"xsi:type="CharTerm"TERMINATOR="\r\n"MAX_LENGTH="24"/>
</RECORD>
<ROW>
<COLUMNSOURCE="1"NAME="BusinessEntityID"xsi:type="SQLINT"/>
<COLUMNSOURCE="2"NAME="PersonType"xsi:type="SQLNCHAR"/>
<COLUMNSOURCE="3"NAME="NameStyle"xsi:type="SQLBIT"/>
<COLUMNSOURCE="4"NAME="Title"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="5"NAME="FirstName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="6"NAME="MiddleName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="7"NAME="LastName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="8"NAME="Suffix"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="9"NAME="EmailPromotion"xsi:type="SQLINT"/>
<COLUMNSOURCE="10"NAME="AdditionalContactInfo"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="11"NAME="Demographics"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="12"NAME="rowguid"xsi:type="SQLUNIQUEID"/>
<COLUMNSOURCE="13"NAME="ModifiedDate"xsi:type="SQLDATETIME"/>
</ROW>
</BCPFORMAT>

In an XML format file, the <RECORD> element lists the fields in the data file, and the <ROW> element lists the
columns in the table. Notice that the xsi:type element in each field in the <RECORD> element lists the type as
CharTerm,whichisusedforeachfieldwhenthecharacterformat(c) is specified. However, as with the nonXML
formatfile,youcanspecifyadifferentformat,asshowninthefollowingexample:
bcpAdventureWorks2008.Person.PersonformatnulnfC:\Data\PersonFormat_n.xmlS
localhost\sqlsrv2008xT

Nowtheformatfilewillincludethenativetypesandaprefixlength,ratherthanaterminator:
<?xmlversion="1.0"?>
<BCPFORMATxmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<RECORD>
<FIELDID="1"xsi:type="NativeFixed"LENGTH="4"/>
<FIELDID="2"xsi:type="NCharPrefix"PREFIX_LENGTH="2"MAX_LENGTH="4"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="3"xsi:type="NativePrefix"PREFIX_LENGTH="1"/>
<FIELDID="4"xsi:type="NCharPrefix"PREFIX_LENGTH="2"MAX_LENGTH="16"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="5"xsi:type="NCharPrefix"PREFIX_LENGTH="2"MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="6"xsi:type="NCharPrefix"PREFIX_LENGTH="2"MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="7"xsi:type="NCharPrefix"PREFIX_LENGTH="2"MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="8"xsi:type="NCharPrefix"PREFIX_LENGTH="2"MAX_LENGTH="20"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="9"xsi:type="NativeFixed"LENGTH="4"/>
<FIELDID="10"xsi:type="NCharPrefix"PREFIX_LENGTH="8"/>
<FIELDID="11"xsi:type="NCharPrefix"PREFIX_LENGTH="8"/>
<FIELDID="12"xsi:type="NativePrefix"PREFIX_LENGTH="1"/>
<FIELDID="13"xsi:type="NativeFixed"LENGTH="8"/>
</RECORD>
<ROW>
<COLUMNSOURCE="1"NAME="BusinessEntityID"xsi:type="SQLINT"/>
<COLUMNSOURCE="2"NAME="PersonType"xsi:type="SQLNCHAR"/>
<COLUMNSOURCE="3"NAME="NameStyle"xsi:type="SQLBIT"/>
<COLUMNSOURCE="4"NAME="Title"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="5"NAME="FirstName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="6"NAME="MiddleName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="7"NAME="LastName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="8"NAME="Suffix"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="9"NAME="EmailPromotion"xsi:type="SQLINT"/>
<COLUMNSOURCE="10"NAME="AdditionalContactInfo"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="11"NAME="Demographics"xsi:type="SQLNVARCHAR"/>
<COLUMN<COLUMN</ROW>
</BCPFORMAT>

Afteryoucreatetheformatfile,youcanthenreferenceitinyourbcpcommandsbyspecifyingthefargument,along
withthenameoftheformatfile,asshowninthefollowingexample:
bcpAdventureWorks2008.Person.PersonoutC:\Data\PersonData_c.datf
C:\Data\PersonFormat_c.fmtSlocalhost\sqlsrv2008TL100

Notice that the command no longer needs a format option (such as c) because all the formatting information is
includedintheformatfile.Andyoucanspecifyanytypeofformatfile,aslongastheformatsofthespecifiedfieldsare
compatiblewiththecolumnsinthetable.Inthefollowingexample,Iusetothenativeformattoexportthedata:
bcpAdventureWorks2008.Person.PersonoutC:\Data\PersonData_n.datf
C:\Data\PersonFormat_n.fmtSlocalhost\sqlsrv2008TL100

AndyoucanjustaseasilyspecifytheXMLformatfile.ThefollowingexampleusesthecharacterformatXMLfileto
exportdata:
bcpAdventureWorks2008.Person.PersonoutC:\Data\PersonData_c.datf
C:\Data\PersonFormat_c.xmlSlocalhost\sqlsrv2008TL100

AndthenextexampleusesthenativeformatXMLfiletoexportdata:
bcpAdventureWorks2008.Person.PersonoutC:\Data\PersonData_n.datf
C:\Data\PersonFormat_n.xmlSlocalhost\sqlsrv2008TL100

Ofcourse,youcanalsouseformatfilestoimportdata.Todemonstratehowtousetheformatfiles,Iusedthefollowing
TransactSQLtocreatetheContacts1tableintheAdventureWorks2008database:
USEAdventureWorks2008
IFOBJECT_ID('Contacts1','U')ISNOTNULL
DROPTABLEdbo.Contacts1

SELECT*
INTOdbo.Contacts1
FROMAdventureWorks2008.Person.Person

WHERE1=2
BecauseIcreatedtheContacts1tablebasedonthePersontable,Icanuseoneoftheformatfilescreatedaboveto
importdataintotheContact1table.Inthefollowingexample,IusethecharacterformatXMLfiletoimportdatafrom
thePersonData_c.datfile:
bcpAdventureWorks2008..Contacts1inC:\Data\PersonData_c.datf
C:\Data\PersonFormat_c.xmlSlocalhost\sqlsrv2008T

Asyoucansee,Isimplyusethefargumenttospecifythenameoftheformatfile.AndIcandothesamethingforthe
nativeformatXMLfile:
bcpAdventureWorks2008..Contacts1inC:\Data\PersonData_n.datf
C:\Data\PersonFormat_n.xmlSlocalhost\sqlsrv2008T

Intheexampleswevelookedatsofar,thecolumnsintheSQLServertableshavematchedthefieldsinthedatafiles.
However,sometimestherearemorecolumnsinthedatafilethaninthetable,morecolumnsinthetablethaninthe
datafile,orthecolumnsareinadifferentorderbetweenthetwosources.Undersuchscenarios,formatfilesareideal
formappingthecolumnsandfieldstoeachother,soletslookatafewexamplesthatdemonstratehowthisisdone.

MoreColumnsinDataFilethaninTable
Whenimportingdatafromafilethatcontainsmorefieldsthantherearecolumnsinthetargettable,youcanconfigure
theformatfiletoaccommodatethesedifferences.First,however,todemonstratehowtoimportthedata,Iusedthe
followingTransactSQLtocreatetheContacts2table:
USEAdventureWorks2008

IFOBJECT_ID('Contacts2','U')ISNOTNULL
DROPTABLEdbo.Contacts2

SELECTBusinessEntityIDASContactID,
FirstName,
LastName,
Demographics,
rowguid,
ModifiedDate
INTOdbo.Contacts2
FROMAdventureWorks2008.Person.Person

WHERE1=2
ThenewtableisbasedonthePersontable,butdoesntuseallofthecolumns.Asaresult,thedatafilegenerated
fromthePersontablewillcontainmorefieldsthantheContacts2table.Toaddressthisissue,Imodifiedthecharacter
dataXMLfile(PersonFormat_c.xml)sothatonlythecolumnsintheContacts2tableareincluded,asshowninthe
followingfilecontents:
<?xmlversion="1.0"?>
<BCPFORMATxmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format"

xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<RECORD>
<FIELDID="1"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="12"/>
<FIELDID="2"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="4"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="3"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="3"/>
<FIELDID="4"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="16"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="5"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="6"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="7"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="8"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="20"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="9"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="12"/>
<FIELDID="10"xsi:type="CharTerm"TERMINATOR=","/>
<FIELDID="11"xsi:type="CharTerm"TERMINATOR=","/>
<FIELDID="12"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="37"/>
<FIELDID="13"xsi:type="CharTerm"TERMINATOR="\r\n"MAX_LENGTH="24"/>
</RECORD>
<ROW>
<COLUMNSOURCE="1"NAME="BusinessEntityID"xsi:type="SQLINT"/>
<COLUMNSOURCE="5"NAME="FirstName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="7"NAME="LastName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="11"NAME="Demographics"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="12"NAME="rowguid"xsi:type="SQLUNIQUEID"/>
<COLUMNSOURCE="13"NAME="ModifiedDate"xsi:type="SQLDATETIME"/>
</ROW>
</BCPFORMAT>

Noticethatthecolumnslistedinthe<ROW>elementnowmatchtheContacts2table.Tomatchthecolumnstothe
fieldsinthedatafile,youmustmakesurethattheSOURCEelementvaluematchestheappropriateIDvalueinthe
FIELDelements.Forexample,theBusinessEntityIDcolumnshowsaSOURCEvalueof1.Thismatchesthefirstfield
listedinthe<RECORD>element,whichisID1.
After I modified the PersonFormat_c.xml file, I renamed it PersonFormat_c2.xml. I can then use the file in my bcp
commandtoimportdataintotheContacts2table,asshowninthefollowingexample:
bcpAdventureWorks2008..Contacts2inC:\Data\PersonData_c.datf
C:\Data\PersonFormat_c2.xmlSlocalhost\sqlsrv2008T

Because the format file has been configured to include only the Contacts2 column and those columns map the
appropriatefields,onlythecorrectdatafromthedatafileisimportedintothetable.

MoreColumnsinTablethaninDataFile
Insomecases,yourtargettablewillincludemorevaluesthanareinthedatafile,asissometimesthecasewhenyou
havenullablecolumnsorcolumnswithdefaultorcomputedvalues.Insuchcases,youllneedtocreateaformatfile
thatreflectsthedifferencebetweenthesourceandtarget.
Todemonstratehowthisworks,IusedthefollowingTransactSQLtocreatetheContacts3tableandthentodefinea
defaultvalueontheSuffixcolumn:
USEAdventureWorks2008

IFOBJECT_ID('Contacts3','U')ISNOTNULL
DROPTABLEdbo.Contacts3

SELECTBusinessEntityIDASContactID,
FirstName,
LastName,
Suffix,
Demographics,
rowguid,

ModifiedDate
INTOdbo.Contacts3
FROMAdventureWorks2008.Person.Person
WHERE1=2

ALTERTABLEContacts3
ADDCONSTRAINTsuffix_defDEFAULT'unknown'FORSuffix

AfterIcreatedthetable,IexportedthedatafromContacts2,thetableIpopulatedinthepreviousexample:
bcpAdventureWorks2008..Contacts2outC:\Data\PersonData_c2.datct,S
localhost\sqlsrv2008T

TheresultisthattheContacts3tablecontainsaSuffixcolumn(withadefault),butthedatafiledoesnotcontaina
matchingfield.Next,IcreateaformatfilebasedontheContacts3table:
bcpAdventureWorks2008..Contacts3formatnulct,fC:\Data\PersonFormat_c3.fmtS
localhost\sqlsrv2008T

Notice that I created a nonXML format file. This is because the XML format files do not let you have more table
columnsthanfieldsinthedatafile.Everycolumnlistedinthe<ROW>elementoftheXMLfilemustcorrespondtoa
fieldinthe<RECORD>element.(Youcangetaroundthislimitationbycreatinganupdateableviewagainstthetarget
tableandincludingonlythosecolumnsthathavecorrespondingfieldsinthedatafile.)
Afteryoucreatetheformatfile,youcanmodifythefiletomapthecolumnstothedatafields.Inthiscase,Ideletedthe
Suffixrowandupdatedthenumbersinthefirstcolumn(thenumbersthatrepresentthedatafilefieldorder)sotheyre
inconsecutiveorder.Theformatfileshouldnowlooksimilartothefollowing:
10.0
6
1SQLCHAR012","1ContactID""
2SQLCHAR0100","2FirstNameSQL_Latin1_General_CP1_CI_AS
3SQLCHAR0100","3LastNameSQL_Latin1_General_CP1_CI_AS
4SQLCHAR00","5Demographics""
5SQLCHAR037","6rowguid""
6SQLCHAR024"\r\n"7ModifiedDate""

Onceyouvemodifiedtheformatfile,youcanusethefollowingbcpcommandtoimportthedata:
bcpAdventureWorks2008..Contacts3inC:\Data\PersonData_c2.datf
C:\Data\PersonFormat_c3.fmtSlocalhost\sqlsrv2008T

NoticethatImusingthePersonData_c2.datadatafileandthePersonFormat_c3.fmtformatfile.Whenyourunthis
command,thedatabaseenginewillautomaticallyinsertthedefaultvalueintotheSuffixcolumn.

ColumnsinDataFileinDifferentOrderthanTable
Insomecases,thecolumnsinatablewillbeinadifferentorderfromthefieldsinadatafile.Youcanmodifyeitherthe
nonXML or XML format files to accommodate these differences. In either case, you should make certain that the
columnsmaptothecorrectfieldsintheformatfiles.Todemonstratehowtomapthecolumns,Iusedthefollowing
TransactSQLtocreatetheContacts4table:
USE AdventureWorks2008

IF OBJECT_ID('Contacts4','U')ISNOTNULL
DROP TABLEdbo.Contacts4

SELECT BusinessEntityIDASContactID,

LastName,
FirstName,
Demographics,

rowguid,
ModifiedDate
INTO dbo.Contacts4
FROM AdventureWorks2008.Person.Person
WHERE 1=2

Inthiscase,IswitchedtheFirstNameandLastNamefieldsfromthesourcetable(Person).Nowletslookatthenon
XMLformatfile.Forthisexample,IcreatedaformatfileanddatafilebasedonthePersontable,aswesawinearlier
examples.IthenmodifiedtheformatfiletosupporttheContacts4table,asshowninthefollowing:
10.0
13
1SQLCHAR012","1BusinessEntityID""
2SQLCHAR04","0PersonTypeSQL_Latin1_General_CP1_CI_AS
3SQLCHAR03","0NameStyle""
4SQLCHAR016","0TitleSQL_Latin1_General_CP1_CI_AS
5SQLCHAR0100","3FirstNameSQL_Latin1_General_CP1_CI_AS
6SQLCHAR0100","0MiddleNameSQL_Latin1_General_CP1_CI_AS
7SQLCHAR0100","2LastNameSQL_Latin1_General_CP1_CI_AS<
8SQLCHAR020","0SuffixSQL_Latin1_General_CP1_CI_AS
9SQLCHAR012","0EmailPromotion""
10SQLCHAR00","0AdditionalContactInfo""
11SQLCHAR00","4Demographics""
12SQLCHAR037","5rowguid""
13SQLCHAR024"\r\n"6ModifiedDate""

ThefirstmodificationImadeistochangethenumberinginthesixthcolumnoftheformatfile(thenumberingforthe
servercolumnorder).Anyrowthatincludesafieldinthedatafilethatisnotacolumninthetableshouldbesetto0.In
addition,thenumbershouldbemodifiedtoreflecttheorderofthecolumnsinthetable.Forexample,theFirstName
columnisnowsetto3becauseitisthethirdcolumninthetable,andtheLastNamecolumnissetto2becauseitis
thesecondcolumninthetable.
AfterImadethesechanges,IsavedtheformatfileasPersonFormat_c4.fmt.Youcannowusetheformatfiletoload
dataintotheContacts4table,asshowninthefollowingbcpcommand:
bcpAdventureWorks2008..Contacts4inC:\Data\PersonData_c.datf
C:\Data\PersonFormat_c4.fmtSlocalhost\sqlsrv2008T

YoucanachievethesameresultsbycreatinganXMLformatfile.Thekeyistoincludeonlythosecolumnsinthe
<ROW>elementthatarealsocontainedintheContacts4tableandtolistthosecolumnsinthesameorderasthey
appearinthetable,asshowninthefollowing:
<?xmlversion="1.0"?>
<BCPFORMATxmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<RECORD>
<FIELDID="1"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="12"/>
<FIELDID="2"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="4"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="3"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="3"/>
<FIELDID="4"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="16"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="5"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="6"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="7"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="100"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="8"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="20"
COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELDID="9"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="12"/>
<FIELDID="10"xsi:type="CharTerm"TERMINATOR=","/>
<FIELDID="11"xsi:type="CharTerm"TERMINATOR=","/>
<FIELDID="12"xsi:type="CharTerm"TERMINATOR=","MAX_LENGTH="37"/>
<FIELDID="13"xsi:type="CharTerm"TERMINATOR="\r\n"MAX_LENGTH="24"/>
</RECORD>
<ROW>

<COLUMNSOURCE="1"NAME="BusinessEntityID"xsi:type="SQLINT"/>
<COLUMNSOURCE="7"NAME="LastName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="5"NAME="FirstName"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="11"NAME="Demographics"xsi:type="SQLNVARCHAR"/>
<COLUMNSOURCE="12"NAME="rowguid"xsi:type="SQLUNIQUEID"/>
<COLUMNSOURCE="13"NAME="ModifiedDate"xsi:type="SQLDATETIME"/>
</ROW>
</BCPFORMAT>

To create this file, I generated an XML format file based on the Person table, modified the file, and saved it as
PersonFormat_c4.xml.Notethat,afterIeliminatedtheextracolumnsfromthe<ROW>elementandensuredthatthey
were in the correct order, I modified the SOURCE attribute as necessary to map the columns to the fields. For
example,theLastNamecolumnhasaSOURCEvalueof7tomatchthefieldwithanIDvalueof7.
Afteryouvecreatedtheformatfile,youcanrunabcpcommandsimilartothefollowingtoimportthedataintothe
Contact4table:
bcpAdventureWorks2008..Contacts4inC:\Data\PersonData_c.datf
C:\Data\PersonFormat_c4.xmlSlocalhost\sqlsrv2008T

Whenyourunthiscommand,thedatawillbeimportedfromthePersonData_c.datfileintotheContacts4table,and
theimportoperationwillbebasedonthePersonFormat_c4.xmlformatfile.

UsingthebcpUtility
Asyouveseenfromtheexamplesinthisarticle,thebcpcommandlineutilityprovidesagreatdealofflexibilitywhen
importingandexportingdata.Youcancopydatainitsnativeformatinordertotransferdatabetweeninstancesof
SQLServer,oryoucancopyitascharacterdatainordertoworkwithapplicationsotherthanSQLServer.Andthebcp
utilitysupportsnumerousoptionsthatletyoufinetuneyourimportandexportoperations.Inaddition,youcanusethe
utilitytogenerateformatfilesandthenusethosefilesforyourbulkcopyoperations.Indeed,onceyouvelearnedto
workwiththebcputility,youshouldbeabletohandlemostofyourbulkcopyneeds.

SimpleTalk.com

S-ar putea să vă placă și