Export (0) Print
Expand All

bcp_control

Changes the default settings for various control parameters for a bulk copy between a file and SQL Server.

RETCODE bcp_control ( 
        HDBC hdbc, 
        INT eOption, 
        void* iValue);

hdbc

Is the bulk copy-enabled ODBC connection handle.

eOption

Is one of the following:

BCPABORT

Stops a bulk-copy operation that is already in progress. Call bcp_control with an eOption of BCPABORT from another thread to stop a running bulk copy operation. The iValue parameter is ignored.

BCPBATCH

Is the number of rows per batch. The default is 0, which indicates either all rows in a table, when data is being extracted, or all rows in the user data file, when data is being copied to an SQL Server. A value less than 1 resets BCPBATCH to the default.

BCPDELAYREADFMT

A Boolean, if set to true, will cause bcp_readfmt to read at execution. If false (the default), bcp_readfmt will immediately read the format file. A sequence error will occur if BCPDELAYREADFMT is true and you call bcp_columns or bcp_setcolfmt.

A sequence error will also occur if you call bcp_control(hdbc, BCPDELAYREADFMT, (void *)FALSE) after calling bcp_control(hdbc, BCPDELAYREADFMT, (void *)TRUE) and bcp_writefmt.

For more information, see Metadata Discovery.

BCPFILECP

iValue contains the number of the code page for the data file. You can specify the number of the code page, such as 1252 or 850, or one of these values:

BCPFILE_ACP: data in the file is in the Microsoft Windows® code page of the client.

BCPFILE_OEMCP: data in the file is in the OEM code page of the client (default).

BCPFILE_RAW: data in the file is in the code page of the SQL Server.

BCPFILEFMT

The version number of the data file format. This can be 80 (SQL Server 2000), 90 (SQL Server 2005), 100 (SQL Server 2008 or SQL Server 2008 R2), 110 (SQL Server 2012), or 120 (SQL Server 2014). 120 is the default. This is useful for exporting and importing data in formats that were supported by earlier version of the server. For example, to import data that was obtained from a text column in a SQL Server 2000 server into a varchar(max) column in a SQL Server 2005 or later server, you should specify 80. Similarly, if you specify 80 when exporting data from a varchar(max) column, it will be saved just like text columns are saved in the SQL Server 2000 format, and can be imported into a text column of a SQL Server 2000 server.

BCPFIRST

Is the first row of data to file or table to copy. The default is 1; a value less than 1 resets this option to its default.

BCPFIRSTEX

For BCP out operations, specifies the first row of the database table to copy into the data file.

For BCP in operations, specifies the first row of the data file to copy into the database table.

The iValue parameter is expected to be the address of a signed 64-bit integer containing the value. The maximum value that can be passed to BCPFIRSTEX is 2^63-1.

BCPFMTXML

Specifies that the format file generated should be in XML format. It is off by default.

XML format files provide greater flexibility but with some added constraints. For example, you can not specify the prefix and terminator for a field simultaneously, which was possible in older format files.

Note Note

XML format files are only supported when SQL Server is installed along with SQL Server Native Client.

BCPHINTS

iValue contains an SQLTCHAR character string pointer. The string addressed specifies either SQL Server bulk-copy processing hints or a Transact-SQL statement that returns a result set. If a Transact-SQL statement is specified that returns more than one result set, all result sets after the first are ignored. For more information about bulk-copy processing hints, see bcp Utility.

BCPKEEPIDENTITY

When iValue is TRUE, specifies that bulk copy functions insert data values supplied for SQL Server columns defined with an identity constraint. The input file must supply values for the identity columns. If this is not set, new identity values are generated for the inserted rows. Any data present in the file for the identity columns is ignored.

BCPKEEPNULLS

Specifies whether empty data values in the file will be converted to NULL values in the SQL Server table. When iValue is TRUE, empty values will be converted to NULL in the SQL Server table. The default is for empty values to be converted to a default value for the column in the SQL Server table if a default exists.

BCPLAST

Is the last row to copy. The default is to copy all rows; a value less than 1 resets this option to its default.

BCPLASTEX

For BCP out operations, specifies the last row of the database table to copy into the data file.

For BCP in operations, specifies the last row of the data file to copy into the database table.

The iValue parameter is expected to be the address of a signed 64-bit integer containing the value. The maximum value that can be passed to BCPLASTEX is 2^63-1.

BCPMAXERRS

Is the number of errors allowed before the bulk copy operation fails. The default is 10; a value less than 1 resets this option to its default. Bulk copy imposes a maximum of 65,535 errors. An attempt to set this option to a value larger than 65,535 results in the option being set to 65,535.

BCPODBC

When TRUE, specifies that datetime and smalldatetime values saved in character format will use the ODBC timestamp escape sequence prefix and suffix. The BCPODBC option only applies to BCP_OUT.

When FALSE, a datetime value representing January 1, 1997 is converted to the character string: 1997-01-01 00:00:00.000. When TRUE, the same datetime value is represented as: {ts '1997-01-01 00:00:00.000'}.

BCPROWCOUNT

Returns the number of rows affected by the current (or last) BCP operation.

BCPTEXTFILE

When TRUE, specifies that the data file is a text file, rather than a binary file. If the file is a text file, BCP determines whether or not it is Unicode by checking the Unicode byte marker in the first two bytes of the data file.

BCPUNICODEFILE

When TRUE, specifies the input file is a Unicode file.

iValue

Is the value for the specified eOption. iValue is an integer (LONGLONG) value cast to a void pointer to allow for future expansion to 64 bit values.

SUCCEED or FAIL.

This function sets various control parameters for bulk-copy operations, including the number of errors allowed before canceling a bulk copy, the numbers of the first and last rows to copy from a data file, and the batch size.

This function is also used to specify the SELECT statement when bulk copying out from SQL Server the result set of a SELECT. Set eOption to BCPHINTS and set iValue to have a pointer to an SQLTCHAR string containing the SELECT statement.

These control parameters are only meaningful when copying between a user file and an SQL Server table. Control parameter settings have no effect on rows copied to SQL Server with bcp_sendrow.

Example

// Variables like henv not specified.
SQLHDBC      hdbc;
DBINT      nRowsProcessed;
 
// Application initiation, get an ODBC environment handle, allocate the
// hdbc, and so on.
... 

// Enable bulk copy prior to connecting on allocated hdbc.
SQLSetConnectAttr(hdbc, SQL_COPT_SS_BCP, (SQLPOINTER) SQL_BCP_ON,
   SQL_IS_INTEGER);

// Connect to the data source, return on error.
if (!SQL_SUCCEEDED(SQLConnect(hdbc, _T("myDSN"), SQL_NTS,
   _T("myUser"), SQL_NTS, _T("myPwd"), SQL_NTS)))
   {
   // Raise error and return.
   return;
   }

// Initialize bulk copy. 
if (bcp_init(hdbc, _T("address"), _T("address.add"), _T("addr.err"),
   DB_IN) == FAIL)
   {
   // Raise error and return.
   return;
   }

// Set the number of rows per batch. 
if (bcp_control(hdbc, BCPBATCH, (void*) 1000) == FAIL)
   {
   // Raise error and return.
   return;
   }

// Set file column count. 
if (bcp_columns(hdbc, 1) == FAIL)
   {
   // Raise error and return.
   return;
   }

// Set the file format. 
if (bcp_colfmt(hdbc, 1, 0, 0, SQL_VARLEN_DATA, '\n', 1, 1)
   == FAIL)
   {
   // Raise error and return.
   return;
   }

// Execute the bulk copy. 
if (bcp_exec(hdbc, &nRowsProcessed) == FAIL)
   {
   // Raise error and return.
   return;
   }

printf_s("%ld rows processed by bulk copy.", nRowsProcessed);

Community Additions

ADD
Show:
© 2014 Microsoft