Bulk Copy Program (BCP)
BCP (Bulk Copy Program) is a command-line utility program for copying data between a SQL Server database and a text file.
Syntax for copying data from text file to SQL Server table:
bcp "database name..table name" in location of text file -c -q -S server name -U "login" - P "password"
-c = indicates data is in character form
-q = quoted identifiers are required - example "DCO88001..TCO88001"
-S = capital S - server name
-U = capital U - SQL Login
-P = capital P - password
Example:
bcp "DCO88001..TCO88001" in \\biwgdcfps01\corp\s88\customer.txt -c -q
-S biwgdcsql02 -U"CorpTest" -P "password"
Copies file customer.txt in directory corp\s88 on server biwgdcfps01 into table TCO88001 in database
DCO88001 on server biwgdcsql02 using SQL login CorpTest with password of password
Syntax for copying SQL Server table to text file:
bcp "database name..table name" out location of text file -c -q -S server name -U "login" - P "password"
-c = indicates data is in character form
-q = quoted identifiers are required - example "DCO88001..TCO88001"
-S = capital S - server name
-U = capital U - SQL Login
-P = capital P - password
Example:
Bcp "DCO88001..TCO88001" out \\biwgdcfps01\corp\s88\customer.txt -c -q
-S biwgdcsql02 -U"CorpTest" -P"password"
Extract table TCO88001 data from database DCO88001 on server biwgdcsql02 to text file customer.text on
directory corp\s88 on server biwgdcfps01 with SQL login CorpTest with password of password
BCP does not have the capability of encrypting login or password.
When BCP encounter a duplicate key condition, it will undo any copying it has already done.
Error message on duplicate key condition:
SQLState = 23000, NativeError = 2627
Error = {Microsoft}{ODBC SQL Server Driver}{SQL Server} Violation of PRIMARY KEY constraint
'primary key name' - Cannot insert duplicate key in object 'table name'
SQLState = 01000, NativeError = 3221
Warning = {Microsoft}{ODBC SQL Server Driver}{SQL Server}The statement has been terminated.