The bcp utility bulk copies data between an instance of Microsoft SQL Server and a data file in a user-specified format. The bcp utility can be used to import large numbers of new rows into SQL Server tables or to export data out of tables into data files.
This examples shows how to use BCP command to transfer data from table to File and then from file to table.
Here I have created empty BCP_ Customers table similar structure of Customers stable.
Now let’s see, how to use BCP utility to Export Data from Table to file using Trusted Connection or Windows Authorization.
>BCP TESTDB.dbo.Customers OUT C:\bcp\Customers.txt -T –c
Arguments
in data_file | out data_file | queryout data_file | format nul
Specifies the direction of the bulk copy, as follows:
- in copies from a file into the database table or view.
- out copies from the database table or view to a file. If you specify an existing file, the file is overwritten. When extracting data, note that the bcp utility represents an empty string as a null and a null string as an empty string.
- queryout copies from a query and must be specified only when bulk copying data from a query.
- format creates a format file based on the option specified (-n, -c, -w, or -N) and the table or view delimiters. When bulk copying data, the bcp command can refer to a format file, which saves you from re-entering format information interactively. The formatoption requires the -f option; creating an XML format file, also requires the -x option. For more information, see Create a Format File (SQL Server). You must specify nul as the value (format nul).
Performs the operation using a character data type. This option does not prompt for each field; it uses char as the storage type, without prefixes and with \t (tab character) as the field separator and \r\n (newline character) as the row terminator. -c is not compatible with -w.
-T
Specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security. The security credentials of the network user, login_id, and password are not required. If –T is not specified, you need to specify –U and –P to successfully log in.
You can find the detail arguments detils in the follwing link:
https://msdn.microsoft.com/en-us/library/aa337544.aspx
You can see the output file in the output folder
Now let’s see, how to use BCP utility to Export Data from Table to file using SQLServer Authentication or security credentials.
BCP utility will helps if it finds any appropriate command
>BCP TESTDB.dbo.Customers OUT C:\bcp\Customers1.txt -Slocalhost -Usa -P123 –c
-S: Server Name (localhost)
-U: Username (sa)
-P: Password (123)
Now let’s see, how to use BCP utility to Import Data from File to Table using Server Authentication or security credentials.
>BCP TESTDB.dbo.BCP_Customers IN C:\bcp\Customers.txt -T -c
>BCP TESTDB.dbo.BCP_Customers IN C:\bcp\Customers.txt -T -c
Now let’s see, how to use BCP utility to Export Data from File to Table using SQLServer Authentication or security credentials.
>BCP TESTDB.dbo.BCP_Customers IN C:\bcp\Customers1.txt -Slocalhost -Usa -P123 –c
Now let’s see, how to use BCP utility using SQL Query Export Data from Table to File using SQLServer Authentication or security credentials.
>BCP "SELECT firastname,lastname FROM TESTDB.dbo.BCP_Customers where id<4" queryout C:\bcp\Customersqueryout.txt -T –c
Cheers!
Uma
thanks for sharing this article
ReplyDeleteto know the basic about the bcp command line syntax, visit: http://sqltechtips.blogspot.com/2016/12/bcp-utility-in-sql-server.html
im getting the below error, what can be the reason
ReplyDeleteCTLIB Message: - L6/O8/S5/N3/5/0:
ct_connect(): directory service layer: internal directory control layer error: R
equested server name not found.
Establishing connection failed.
How do you specify servername while doing bcp import and using trusted connection. I have tried the IP address and servername\instance. It fails "server name not found"
ReplyDeleteThank you very much for the great article you have posted here.
ReplyDeleteIf someone ever faced any issue while connecting to MSSQL Server in another domain using windows authentication, Below article will help to resolve the problem.
https://techiewheel.blogspot.com/2020/12/connect-to-mssql-server-in-another.html